简体   繁体   中英

Algorithm for string with one mismatch

Could someone suggest a better approach for a string comparison among two strings with atmost one mismatch.

Example:

  • A: abcdefghabX
  • B: abc
  • Output: 1 9

The above outputs are the positions of substring matches of abc in "A"

I tried the basic approach with two for loops but, it seems to be taking N*N time. This is a big factor for large inputs and is taking huge amount of time.

My algorithm is as such but, it isn't a best one for huge amount of data

int compare(string a,  int pos, string b)
{
    int count = 0;
    int length = b.length()-1;
    int mid = b.length() /2;

    if(pos+length >= a.length())
        return 0;
    if((length+1)%2 != 0)   {
        for(int i=0,j=pos;i<=mid;i++,j++)   {       
            if(i == mid)    {
                if(a[j] != b[i])
                    count ++;
            }
            else    {
                if(a[j] != b[i])
                    count ++;
                if(a[pos+length - i] != b[length -i]) 
                    count ++;
            }
            if(count >= 2) return 0;
        }
    }
    else    {
        for(int i=0,j=pos;i<mid;i++,j++)    {       
            if(i == mid)    {
                if(a[j] != b[i])
                    count ++;
            }
            else    {
                if(a[j] != b[i])
                    count ++;
                if(a[pos+length - i] != b[length -i]) 
                    count ++;
            }
            if(count >= 2) return 0;
        }
    }
    return 1;
}

你想要的是在agrep中实现的,所以看看它使用的算法

I think what you're after is the Boyer-Moore algorithm.

Specifically, the approximate version here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM