据说这题松爷现场脑补hash实现kmp?吓尿了orz
如果不看超过一半那个限制的话
num[i]
实际上就是
i
<script type="math/tex" id="MathJax-Element-51">i</script>跳多少次到0,这样直接KMP一下,完了之后跳一下就可以了。
#include <bits/stdc++.h>
#define rep(i,a,b) for (int i = a , _ = b ; i <= _ ; i ++)
#define per(i,a,b) for (int i = a , _ = b ; i >= _ ; i --)
inline int rd() {
char c = getchar();
while (!isdigit(c)) c = getchar() ; int x = c - '0';
while (isdigit(c = getchar())) x = x * 10 + c - '0';
return x;
}
const int maxn = 1000007;
const int mod = 1000000007;
int n , nxt[maxn] , num[maxn];
char st[maxn];
void input() {
scanf("%s" , st + 1);
n = strlen(st + 1);
}
void solve() {
nxt[1] = 0 , num[1] = 1;
int k = 0;
rep (i , 2 , n) {
while (k && st[k + 1] != st[i]) k = nxt[k];
if (st[k + 1] == st[i]) k ++;
nxt[i] = k , num[i] = num[k] + 1;
}
k = 0;
int ans = 1;
rep (i , 2 , n) {
while (k && st[k + 1] != st[i]) k = nxt[k];
if (st[k + 1] == st[i]) k ++;
while (k > i / 2) k = nxt[k];
ans = 1ll * ans * (num[k] + 1) % mod;
}
printf("%d\n" , ans);
}
int main() {
#ifndef ONLINE_JUDGE
// freopen("data.txt" , "r" , stdin);
#endif
for (int T = rd();T;T--) {
input();
solve();
}
return 0;
}