KMP经典习题

KMP 算法经典题目:

pku2406 Power Strings

http://162.105.81.212/JudgeOnline/problem?id=2406


pku1961 Period

http://162.105.81.212/JudgeOnline/problem?id=1961

 这个题目是1961的升级版,考察队next数组的应用,附上代码:

#include 
#include 
#include 

using namespace std;

const int ma=1e6+5;
char s[ma];
int nex[ma];

void Pre_next(int len)
{
    int i=0,j=-1;
    nex[0]=-1;
    while(i1)
                printf("%d %d\n",i,res);
        }
        printf("\n");
    }

    return 0;
}


pku2752 Seek the Name, Seek the Fame

http://162.105.81.212/JudgeOnline/problem?id=2752

 

pku3461 Oulipo

http://162.105.81.212/JudgeOnline/problem?id=3461

 

pku2185 Milking Grid

http://162.105.81.212/JudgeOnline/problem?id=2185

 

zjut1349 掷硬币

http://acm.zjut.edu.cn/ShowProblem.aspx?ShowID=1349

 

几道非常经典的KMP题目,很巧的利用了kmp中pre数组(前缀数组)的特性。

留题号备忘,附get_next和匹配模板。

void Get_next(int len) //目标串的长度
{
    int i=0,j=-1;
    nex[0]=-1;
    while(i

int KMP_Count(int m,int n) //m:模式串长度,n:待匹配串长度
{
    int i,j,ans=0;
    i=j=0;
    Get_nex(m); //计算next[]数组
    while(i=m)
        {
            ans++;
            j=nex[j];
        }
    }
    return ans;
}



你可能感兴趣的:(字符串)