Skip to content

Commit

Permalink
PKU 2752 - Seek the Name, Seek the Frame
Browse files Browse the repository at this point in the history
  • Loading branch information
marioyc committed Sep 25, 2010
1 parent afe7385 commit bb41a3c
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions PKU/2752 - Seek the Name, Seek the Frame.cpp
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;
}

0 comments on commit bb41a3c

Please sign in to comment.