Test instructions
Give you a string T, find the substring of the string T, the string is both the T prefix and the suffix of T. From small to large output all strings that meet the required length.
Analysis:
The first thing to know is that the value of the KMP Next[i] array is the maximum matching length of the suffix of [1,i-1] in the string T and the [0,i-2] prefix in the string T. so Next[m] (M is a string length and the string from 0 to m-1 subscript) is the maximum prefix length to match the suffix (think about it).
NEXT[NEXT[M]] is also a prefix length that matches the suffix,,, and so on.
The puzzle is from Rauzi: http://blog.csdn.net/u013480600/article/details/23024781
//Author: 1085422276#include <cstdio>#include<cmath>#include<string>#include<cstring>#include<ctime>#include<iostream>#include<algorithm>#include<Set>#include<vector>#include<sstream>#include<queue>#include<typeinfo>//#include <bits/stdc++.h>#include <map>#include<stack>typedefLong Longll;using namespacestd;Const intINF =10000000; inline ll read () {ll x=0, f=1; CharCh=GetChar (); while(ch<'0'|| Ch>'9') { if(ch=='-') f=-1; CH=GetChar (); } while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; CH=GetChar (); } returnx*F;} ll EXGCD (ll A,ll b,ll&x,ll &y) {ll temp,p; if(b==0) {x=1; Y=0; returnA; } P=EXGCD (b,a%b,x,y); Temp=x; X=y; Y=temp-(A/b) *y; returnp;}//*******************************Chara[400005]; intmaxl[400005],p[400005];intMain () { while(Gets (a)! =NULL) { intn=strlen (a); Memset (P,0,sizeof(p)); intj=0; for(intI=1; i<n;i++) { while(j>0&&a[j]!=a[i]) j=P[j]; if(A[j]==a[i]) J + +; P[i+1]=J; } intCnt=0; maxl[++cnt]=N; intI=N; while(P[n]) {maxl[++cnt]=P[n]; N=P[n]; } for(intI=cnt;i>1; i--) {printf ("%d", Maxl[i]); } printf ("%d\n", maxl[1]); } return 0;}
Code
POJ 2752 seek the Name, seek the Fame kmp (suffix with prefix)