Title Link: https://cn.vjudge.net/problem/POJ-2752
Test instructions
Give a string the number of prefix strings with the same prefix string as the suffix string
Example: Alala
Output: A, Ala, Alala
Ideas
Think about it, the return value of Fail[len] is actually the maximum number of successful suffix strings.
After getting this suffix string, the string smaller than this string must still be included in this new suffix string.
Iteration can
Submission Process
Code
#include <cstring> #include <cstdio>const int Maxm=4e5+20;char p[maxm];int fail[ Maxm];void getfail (int m) {fail[0]=fail[1]=0; for (int i=1; i<m; i++) {int j=fail[i]; while (J && P[j]!=p[i]) j=fail[j]; Fail[i+1]= ((p[i]==p[j))? j+1:0); }}int Main (void) {int len, ANS[MAXM], kase=0; while (scanf ("%s", P) ==1 &&! ( p[0]== '. ' &&! P[1]) {Getfail (Len=strlen (P))); int Size=len, ptr=1; Ans[0]=len; while (size>0) ans[ptr++]= (Size=fail[size]); for (int i=ptr-2; i>=0; i--) printf ("%d%c", ans[i], "\ n" [!! I]); } return 0;
| Time
Memory |
Length |
Lang |
submitted |
485ms |
3840kB |
547 |
g++ |
2018-08-02 11:27:13 |
POJ-2752 seek the Name, seek the Fame string problem KMP algorithm to find the same number of wood before and after the prefix