Title Link: http://poj.org/problem?id=2752
Test instructions: give you a string to find the length of all prefix suffixes (both prefixes and suffixes)
Idea: First the entire string must be both a prefix and a suffix, the largest prefix suffix.
Suppose Next[len] = k, that is: s[1,k] = S[len-k+1,len] At this time s[1,k] is the prefix suffix.
After processing Next[len] jump to next[k+1], in this way you can get all the prefix suffix.
Code
1#include <cstdio>2#include <cstring>3 Const intMAXN =400005;4 CharSTR[MAXN];5 intNEXT[MAXN];6 intANS[MAXN];7 voidGetNext (intlen)8 {9 inti =0;Ten intj =-1; Onenext[0] = -1; A while(I <len) - { - if(-1= = J | | Str[i] = = Str[j]) Next[++i] = + +J; the Elsej =Next[j]; - } - } - + intMain () - { + while(SCANF ("%s", str) = =1) A { at intLen =strlen (str); - GetNext (len); - intt =-1; -ANS[++T] =Len; - while(Next[len] >0) - { inANS[++T] =Next[len]; -Len =Next[len]; to } + for(inti = t; i >0; -i) printf ("%d", Ans[i]); -printf"%d\n", ans[0]); the } * return 0; $}
POJ 2752 seek the Name, seek the Fame (all substring lengths that are both prefixes and suffixes)