Skip to content

Instantly share code, notes, and snippets.

@hikariyo
Created December 7, 2025 16:00
Show Gist options
  • Select an option

  • Save hikariyo/f962aff182ecd444a2b4530a92743b41 to your computer and use it in GitHub Desktop.

Select an option

Save hikariyo/f962aff182ecd444a2b4530a92743b41 to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const int N = 1e6+10;
const i64 P = 1e9+7;
int nxt[N], dep[N];
int solve() {
string s;
cin >> s;
int n = s.size();
s = ' ' + s;
for (int i = 2; i <= n; i++) {
nxt[i] = nxt[i-1];
while (nxt[i] && s[nxt[i]+1] != s[i]) nxt[i] = nxt[nxt[i]];
if (s[nxt[i]+1] == s[i]) nxt[i]++;
}
for (int i = 1; i <= n; i++) dep[i] = dep[nxt[i]] + 1;
int j = 0;
i64 ans = 1;
for (int i = 1; i <= n; i++) {
while (j && s[j+1] != s[i]) j = nxt[j];
if (s[j+1] == s[i]) j++;
if (j > i/2) j = nxt[j];
ans = ans * (dep[j] + 1) % P;
}
return ans;
}
signed main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int T;
cin >> T;
while (T--) cout << solve() << '\n';
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment