forked from marioyc/Online-Judge-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PKU 2752 - Seek the Name, Seek the Frame
- Loading branch information
marioyc
committed
Sep 25, 2010
1 parent
afe7385
commit bb41a3c
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include <cstdio> | ||
#include <cstring> | ||
|
||
using namespace std; | ||
|
||
#define MAX_L 400000 | ||
|
||
char P[MAX_L+1]; | ||
int f[MAX_L]; | ||
|
||
void prefixFunction(){ | ||
int n = strlen(P), k = 0; | ||
f[0] = 0; | ||
|
||
for(int i = 1;i<n;++i){ | ||
while(k>0 && P[k]!=P[i]) k = f[k-1]; | ||
if(P[k]==P[i]) ++k; | ||
f[i] = k; | ||
} | ||
} | ||
|
||
int main(){ | ||
int ans[MAX_L],sz; | ||
|
||
while(scanf("%s",P)==1){ | ||
prefixFunction(); | ||
|
||
int L = strlen(P); | ||
sz = 0; | ||
|
||
while(L!=0){ | ||
ans[sz] = L; | ||
++sz; | ||
L = f[L-1]; | ||
} | ||
|
||
printf("%d",ans[sz-1]); | ||
for(int i = sz-2;i>=0;--i) printf(" %d",ans[i]); | ||
printf("\n"); | ||
} | ||
|
||
return 0; | ||
} |