题意:
给你一个串,如果这个串存在一个长度为n的前缀串,和长度为n的后缀串,并且这两个串相等,则输出他们的长度n。求出所有的长度n。
解析:
此题考察kmp next[]数组的应用,不难。
AC代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cstdlib>
using namespace std;
typedef long long ll;
const int N = 400005;
char str[N];
int jump[N];
vector<int> ans;
void getNext() {
int j = 0, k = -1;
int len = strlen(str);
jump[0] = -1;
while(j < len) {
if(k == -1 || str[j] == str[k])
jump[++j] = ++k;
else k = jump[k];
}
}
int main() {
while(scanf("%s", str) != EOF) {
ans.clear();
getNext();
int cur = strlen(str);
while(cur > 0) {
ans.push_back(cur);
cur = jump[cur];
}
sort(ans.begin(), ans.end());
printf("%d", ans[0]);
for(int i = 1; i < ans.size(); i++) {
printf(" %d", ans[i]);
}puts("");
}
return 0;
}