http://www.lydsy.com/JudgeOnline/problem.php?id=3670
http://uoj.ac/problem/5
可以建出“KMP自动机”然后在树上二分或单调计算。
也可以不建树,每个位置维护fail指针和nxt指针。
nxt指针指的是在小于等于当前位置除以2的fail指针能跳到的最大的位置,都可以(O(n))维护。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 1000003;
char s[N];
int len, fail[N], sum[N], ans;
int main() {
int T, p, pp; scanf("%d", &T); sum[0] = 1; sum[1] = 2;
while (T--) {
scanf("%s", s + 1);
len = strlen(s + 1); p = pp = 0; ans = 1;
for (int i = 2; i <= len; ++i) {
while (p && s[p + 1] != s[i]) p = fail[p];
if (s[p + 1] == s[i]) fail[i] = ++p;
else fail[i] = 0;
sum[i] = sum[fail[i]] + 1;
while (((pp + 1) << 1) > i || pp && s[pp + 1] != s[i]) pp = fail[pp];
if (s[pp + 1] == s[i]) ans = 1ll * ans * sum[++pp] % 1000000007;
}
printf("%d
", ans);
}
return 0;
}