字符串匹配算法---Brute force、KMP、Sunday

字符串匹配---輸入原字符串(String)和子串(又稱模式),輸出爲子串在原字符串中的首次出現的位置。在這兒,我們介紹用於字符串匹配的Brute force、KMP和Sunday方法。

1.Brute force

該方法又稱暴力搜索。

匹配時間複雜度O(N*M)

主要過程:從原字符串開始搜索,若出現不能匹配,則從下一個位置繼續。

int bf(const char *text, const char *find)  
{  
    if (text == '/0' || find == '/0')  
        return -1;  
    int find_len = strlen(find);  
    int text_len = strlen(text);  
    if (text_len < find_len)  
        return -1;  
    char *s = text;  
    char *p = s;  
    char *q = find;  
    while (*p != '/0')  
    {  
        if (*p == *q)  
        {  
            p++;  
            q++;  
        }  
        else  
        {  
            s++;  
            p = s;  
            q = find;  
        }  
        if (*q == '/0')  
        {  
            return (p - text) - (q - find);  
        }  
    }  
    return -1;  
}  

2.KMP

匹配時間複雜度:O(M+N)

主要過程:通過對字串進行預處理,找到next數組(子串首尾重合長度數組);當發現不能匹配時,可以不進行回溯。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 100

void cal_next( char * str, int * next, int len )
{
    int i, j;

    next[0] = -1;
    for( i = 1; i < len; i++ )
    {
        j = next[ i - 1 ];
        while( str[ j + 1 ] != str[ i ] && ( j >= 0 ) )
        {
            j = next[ j ];
        }
        if( str[ i ] == str[ j + 1 ] )
        {
            next[ i ] = j + 1;
        }
        else
        {
            next[ i ] = -1;
        }
    }
}

int KMP( char * str, int slen, char * ptr, int plen, int * next )
{
    int s_i = 0, p_i = 0;

    while( s_i < slen && p_i < plen )
    {
        if( str[ s_i ] == ptr[ p_i ] )
        {
            s_i++;
            p_i++;
        }
        else
        {
            if( p_i == 0 )
            {
                s_i++;
            }
            else
            {
                p_i = next[ p_i - 1 ] + 1;
            }
        }
    }
    return ( p_i == plen ) ? ( s_i - plen ) : -1;
}

int main()
{
    char str[ N ] = {0};
    char ptr[ N ] = {0};
    int slen, plen;
    int next[ N ];

    while( scanf( "%s%s", str, ptr ) )
    {
        slen = strlen( str );
        plen = strlen( ptr );
        cal_next( ptr, next, plen );
        printf( "%d\n", KMP( str, slen, ptr, plen, next ) );
    }
    return 0;
}

3.Sunday

匹配時間複雜度O(N*M)

主要過程:從尾部出發,判斷原串字符在目標串中是否存在,根據判斷結果,可以一次往後移動較大偏移量。

int in_dst(const char word, const char *dst)
 {
     int i = strlen(dst) - 1;

     while (dst[i] != '\0')
     {
         if (dst[i] == word)
           return (strlen(dst) - i);
        i--;
     }
     return -1;
}
 
 
int sunday(const char *src, const char *dst)
{
     int SrcLenth = strlen(src), DstLenth = strlen(dst);
     int j = 0, i = 0, pos;
 
     while (j < SrcLenth)
     {
         i = 0;
         while (src[j+i] == dst [i])	i++;
         if (dst[i] == '\0')   printf("%d~%d\n", j, j+DstLenth-1);  
 
         if ((pos = in_dst(src[j+DstLenth], dst)) != -1) j += pos;
         else j += (DstLenth + 1); 
     }
}
發佈了37 篇原創文章 · 獲贊 17 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章