kmp才能保证不超时

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <stack>
#include <iostream>
using namespace std;

void getNext(int next[],char str[]){
int j = 0;
next[0] = -1;
int k = -1;
int len = strlen(str);
while(j < len){
if(k == -1 || str[k] == str[j]){
next[++j] = ++k;
}
else k = next[k];
}
}

int main(){
//freopen("in.txt","r",stdin);
char str[400001];
int next[400001];

int j;
while(scanf("%s",str) != EOF){
memset(next,0,strlen(str));
getNext(next,str);
stack <int> q;
j =strlen(str);
while(j != 0){
q.push(j);
j = next[j];
}
while(!q.empty()){
printf("%d ",q.top());
q.pop();

}
printf("\n");
}

return 0;
}