标签:
http://poj.org/problem?id=2752
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 14611 | Accepted: 7320 |
Description
Input
Output
Sample Input
ababcababababcabab aaaaa
Sample Output
2 4 9 18 1 2 3 4 5
#include<stdio.h> #include<string.h> #include<iostream> #include<stack> using namespace std; #define N 1000007 #define max(a,b) (a>b?a:b) int Next[N]; char S[N]; void FindNext(int Slen) { int i=0, j=-1; Next[0] = -1; while(i<Slen) { if(j==-1 || S[i]==S[j]) Next[++i] = ++j; else j = Next[j]; } } int main() { while(scanf("%s", S)!=EOF) { int Slen, n; Slen = strlen(S); n = Slen; FindNext(Slen); stack<int>Q; while(n) { Q.push(Next[n]); n = Next[n]; } Q.pop(); while(Q.size()) { printf("%d ", Q.top()); Q.pop(); } printf("%d\n", Slen); } return 0; }
(KMP)Seek the Name, Seek the Fame -- poj --2752
标签:
原文地址:http://www.cnblogs.com/YY56/p/4835492.html