[2016/12/19]kmp算法

上学期上数据结构的时候就学了这个算法,可是没好好听(摊手)。这次刷题的时候遇到了一个字符串匹配题,于是学会了这个算法。

看了这么多博客,觉得对原理讲得比较好的就是这篇:戳这里

如果想理解,重点还是前后缀的概念。还有中间怎么跳的,这个自己写两个字符串,模拟一下就行。

下面的代码写得非常好,对理解很有帮助。为了帮助理解,加入了一些输出。

#include <iostream>
#include <stdio.h> 
#define MAX_SIZE 1024
#define ElemType char
using namespace std;
void CptPfFunc( ElemType Pattern[], int PrefixFunc[] )                
{      
        register int iLen = 0;    // Length of Pattern[]            
        while( '\0' != Pattern[iLen] )            
                iLen++;            

        int LOLP = 0;     // Lenth of longest prefix            
        PrefixFunc[1] = 0;            

        for( int NOCM=2; NOCM<iLen+1; NOCM++ )     // NOCM represent the number of characters matched            
        {            
                while( LOLP>0 && (Pattern[LOLP] != Pattern[NOCM-1]) )            
                        LOLP = PrefixFunc[LOLP];            
                if( Pattern[LOLP] == Pattern[NOCM-1] )            
                        LOLP++;            
                PrefixFunc[NOCM] = LOLP;     
                printf("%d ",PrefixFunc[NOCM]);       
        }           
        printf("\n");
}            

void KMPstrMatching(char Target[], char Pattern[])            
{            
        int PrefixFunc[MAX_SIZE];            
        register int TarLen = 0;            
        register int PatLen = 0;            

        // Compute the length of array Target and Pattern            
        while( '\0' != Target[TarLen] )            
                TarLen++;            

        while( '\0' != Pattern[PatLen] )            
                PatLen++;            

        // Compute the prefix function of Pattern            
        CptPfFunc(Pattern, PrefixFunc);            

        int NOCM = 0;     // Number of characters matched            

        for( int i=0; i<TarLen; i++ )            
        {            
                while( NOCM>0 && Pattern[NOCM] != Target[i] )            
                        NOCM = PrefixFunc[NOCM];            
                if( Pattern[NOCM] == Target[i] )            
                        NOCM++;            
                if( NOCM == PatLen )            
                {            
                        cout<<"KMP String Matching,pattern occurs with shift "<<i - PatLen + 1<<endl;            
                        NOCM = PrefixFunc[NOCM];            
                }            
        }            
}
int main(){
    KMPstrMatching("ABCVDFAB","AB");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值