查找汉明距离最短的字符串

时间:2016-05-01 11:12:39

标签: string algorithm hamming-distance

我有一个(字母)字符串列表,我想比较一个字符串和其他字符串列表,并找出哪个字符串与该字符串相比具有最低的汉明距离。此外,用于比较的字符串列表在编译时是已知的 我想到了这样的事情:

char* compare[10][10]; // Assume this array is filled with random strings, every string of length n is in row n - 1. 
int hamming_distance(char* x, char* y, int len); // I don't know of any fast ways to compute the Hamming distance between two alphabetical strings
int hamming_distance(char* x, char* y, int len) {
    int hamming_distance = 0;
    for(i = 0; i <= len; i++) {
        if(x[i] != y[i]) {
            hamming_distance++;
        }
    }
    return hamming_distance;
}
char* lowest_hamming_distance(char* x);
char* lowest_hamming_distance(char* x) {
    int minhammingdistance = INT_MAX;
    int currenthammingdistance;
    int len = strlen(x) - 1;
    for( i = 0; i < 10; i++) {
        currenthammingdistance = hamming_distance(compare[len][i], x, len);
        if(minhammingdistance > currenthammingdistance) {
            minhammingdistance = currenthammingdistance;
            int min_hamming_distance_string = i;
        }
    }
    return compare[len][min_hanmming_distance_string];
}

0 个答案:

没有答案