judge:POJ 2752
Time Limit: 2000MS
Memory Limit: 65536K
The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give names to their newly-born babies. They seek the name, and at the same time seek the fame. In order to escape from such boring job, the innovative little cat works out an easy but fantastic algorithm:
Step1. Connect the father’s name and the mother’s name, to a new string
S
S
S.
Step2. Find a proper prefix-suffix string of
S
S
S (which is not only the prefix, but also the suffix of
S
S
S).
Example: Father=‘ala’
, Mother=‘la’
, we have
S
S
S = ‘ala’
+‘la’
= ‘alala’
. Potential prefix-suffix strings of
S
S
S are {‘a’
, ‘ala’
, ‘alala’
}. Given the string
S
S
S, could you help the little cat to write a program to calculate the length of possible prefix-suffix strings of
S
S
S? (He might thank you by giving your baby a name:)
http://poj.org/problem?id=2752
http://poj.org/problem?id=2752
ababcababababcabab
aaaaa
2 4 9 18
1 2 3 4 5
问你一个串分别可以分成几块可以使这几块完全一样,块之间可以重叠,也可以分成一块。
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstdio>
#include <cstring>
#define _for(i, a) for(int i = 0; i < (a); i++)
#define _rep(i, a, b) for(int i = (a); i <= (b); i++)
typedef unsigned long long ull;
const int maxn = 10005;
const int maxm = 1000005;
const int inf = 0x3f3f3f3f;
ull mod = 133;
using namespace std;
char a[maxm];
ull p[maxm], hs[maxm];
ull gethash(int l, int r) {
return hs[r] - hs[l - 1] * p[r - l + 1];
}
bool che(char a[], int alen, int len) {
return hs[len] == gethash(alen - len + 1, alen);
}
int main() {
p[0] = 1;
_rep(i, 1, maxm - 1) p[i] = p[i - 1] * mod;
while (scanf("%s", a + 1) != EOF) {
int alen = strlen(a + 1);
hs[0] = 0;
_rep(i, 1, alen) {
hs[i] = hs[i - 1] * mod + a[i];
}
vector<int> ans;
_rep(i, 1, alen) {
if (che(a, alen, i)) {
ans.push_back(i);
}
}
printf("%d", ans[0]);
_rep(i, 1, ans.size() - 1) {
printf(" %d", ans[i]);
}
printf("\n");
}
return 0;
}