kmp算法

分两个部分:
1. 子串预处理表
2. 搜索算法

子串W预处理表T[]的建立:
1. A为W的子串,并且其字首的子串和字尾的子串同为B。
2. W中字符,若其前面的所有字符构成子串A,则该字符在T中对应的值为最大B的字长。
3. 否则,该字符在T中对应的值为0
4. T[0] = -1, T[1] = 0

搜索算法:
1. i 为当前正在比较的字符在W中的位置(从0开始),j为主串S中开始比较的字符的位置
2. 若比较成功,则i+1
3. 若比较失败,S中下一次开始比较的位置为当前位置回退T[i],W中下一次开始比较的位置为0 (若上一次比较为失败),或是T[i](若上一次比较为成功)。
4.重复步骤2.

 

 

#include  < iostream >
using   namespace  std;

#ifdef _UNICODE
#define   _tcout wcout
#else
#define   _tcout cout
#endif

void  BuildKmpTable(TCHAR *  psz_string,  int  kmp_table[])
{
    
int n_len = _tcslen(psz_string);
    
    
int n_str_index = 0;
    
int n_table_index = 2;
    kmp_table[
0= -1;
    kmp_table[
1= 0// self reference are assumed to be zero

    
int n_prefix_len = 0;

    
for (int i = n_table_index, j = n_str_index; i < n_len; )
    
{
        
// computer the value in the kmp_table[i]
        if (psz_string[i - 1== psz_string[j])
        
{
            n_prefix_len
++;
            kmp_table[i] 
= n_prefix_len;
            j
++;
            i
++;
        }

        
else
        
{
             
if (j > 0)
             
{
                 j 
= kmp_table[j];
             }

             
else
             
{
                 kmp_table[i] 
= 0;
                 i
++;
             }

        }

    }

}


int  KmpSearch(TCHAR *  psz_main, TCHAR *  psz_sub)
{
    
int n_sub_len = _tcslen(psz_sub);

    
int* kmp_table = new int[n_sub_len];
    BuildKmpTable(psz_sub, kmp_table);

    
int n_main_index = 0;
    
int n_sub_index = 0;

    
int n_main_len = _tcslen(psz_main);

    
while ((n_main_index + n_sub_index) < n_main_len)
    
{
        
if (psz_main[n_main_index + n_sub_index] == psz_sub[n_sub_index])
        
{
            n_sub_index
++;
            
if (n_sub_index == n_sub_len)
            
{
                
return n_main_index + 1;
            }

        }

        
else
        
{
            n_main_index 
+= n_sub_index - kmp_table[n_sub_index];
            
if (n_sub_index > 0)
            
{
                n_sub_index 
= kmp_table[n_sub_index];
            }

        }

    }


    
return -1;
}


int  _tmain( int  argc, _TCHAR *  argv[])
{

    
int n_index = KmpSearch(argv[1], argv[2]);

    _tcout 
<< _T("search ""<< argv[2<< _T("" in ""<< argv[1<< _T("""<< endl;
    _tcout 
<< _T("it matched in "<< n_index << endl;

    
return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值