Thread: Optimizing algorithm to find "distance" between two strings

  1. #1
    Registered User
    Join Date
    Apr 2018
    Posts
    10

    Optimizing algorithm to find "distance" between two strings

    Hi,
    I'm trying to solve this problem:

    The typical touchscreen keyboard looks like this:


    qwertyuiop
    asdfghjkl
    zxcvbnm




    You should use the distance between the letters to type a word: the distance is the sum of the horizontal and vertical distance between the typed and proposed letter. Assume you typed a w, the distance to e is 1, while the distance to z is 3.


    The typed word and the list of words from the spell checker all have the same length. The distance between two words is the sum of the letter distances. So the distance between ifpv and icpc is 3.
    Even if the code works it's of course pretty slow because everytime I'm searching for the position of the character in the table. So I'd like to know how could I improve it, even if it's needed to rewrite it from zero.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    
    
    typedef struct
    {
        int x;
        int y;
    } my_coordinate;
    
    
    static char *look_up_table[3] = {"qwertyuiop",
                                     "asdfghjkl",
                                     "zxcvbnm"  };
                     
    
    
    static my_coordinate search_ch(int ch)
    {
        int i;
        my_coordinate ch_pos = { .x = -1, .y = -1 };
        char *found_ch = NULL;
        for(i = 0; i < 3; ++i)
        {
            found_ch = strchr(look_up_table[i], ch);
            if(found_ch != NULL)
            {
                ch_pos.x = i;
                ch_pos.y = found_ch - look_up_table[i];     /*This difference return the index in the string of the charachter*/
            }
        }
    
    
        return ch_pos;
    }
    
    
    static int get_distance(const char *s, const char *p)
    {
        int distance = 0;
        my_coordinate ch_s;
        my_coordinate ch_p;
    
    
        while(*s != '\0')   /* s and p always have the same length*/
        {
            ch_s = search_ch(*s); 
            ch_p = search_ch(*p);
            distance += abs(ch_s.x - ch_p.x) + abs(ch_s.y - ch_p.y);
            s++; p++;
        }
    
    
        return distance;
    }
    
    
    
    
    
    
    int main(void)
    {
        const char *s = "ifpv";
        const char *p = "iopc";
    
    
        printf("%d\n", get_distance(s, p));
        
        return EXIT_SUCCESS;
    }

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,644
    You could create an lookup table that doesn't have to be searched.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct Point {
        int x, y;
    } Point;
    
    Point positions[26]; // positions of the characters
    
    void init_positions() {
        const char *s = "qwertyuiop/asdfghjkl/zxcvbnm";
        int y = 0, x = 0;
        for ( ; *s; s++)
            if (*s == '/') {
                ++y;
                x = 0;
            }
            else {
                positions[*s-'a'].y = y;
                positions[*s-'a'].x = x++;
            }
    }
    
    int calc_distance(char a, char b) {
        Point pa = positions[a-'a'], pb = positions[b-'a'];
        return abs(pa.x - pb.x) + abs(pa.y - pb.y);
    }
    
    int main() {
        init_positions();
    
        const char *s1 = "hello";
        const char *s2 = "teklp";
        int dist = 0;
        for (int i = 0; i < 5; i++)
            dist += calc_distance(s1[i], s2[i]);
        printf("%d\n", dist);
    
        //for (int i = 0; i < 26; i++)
        //    printf("%c, (%d,%d)\n", i+'a', positions[i].x, positions[i].y);
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Apr 2018
    Posts
    10
    Thank you, I'm going to try it with the full code and see how it goes!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-08-2014, 08:12 PM
  2. Replies: 0
    Last Post: 03-09-2009, 10:15 PM
  3. "Apply", "OK" and "CANCEL" strings
    By Joelito in forum Windows Programming
    Replies: 2
    Last Post: 05-12-2006, 04:07 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

Tags for this Thread