1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| #include <bits/stdc++.h> #define pb push_back using namespace std; typedef long long ll; const int N = 200010; const int mod = 1e9 + 7; int n, m, RL[N + N], cnt[N + N], ba[N]; char s[N], t[N + N]; int ans = 0; vector<int> a, b, res;
inline int Min(const int &x, const int &y) { return x < y ? x : y; } inline int Max(const int &x, const int &y) { return x > y ? x : y; } namespace Poly { const int mod = 998244353; inline int add(const int &x, const int &y) { return x + y < mod ? x + y : x + y - mod; } inline int sub(const int &x, const int &y) { return x - y < 0 ? x - y + mod : x - y; } inline int mul(const int &x, const int &y) { return (int)((ll) x * y % mod); } int ksm(int x, int y = mod - 2) { int ss = 1; for(; y; y >>= 1, x = mul(x, x)) if(y & 1) ss = mul(ss, x); return ss; } int Get(int x) { int ss = 1; for(; ss <= x; ss <<= 1); return ss; } void ntt(vector<int> &a, int lmt, int opt) { a.resize(lmt); for(int i = 0, j = 0; i < lmt; i++) { if(i < j) swap(a[i], a[j]); for(int k = lmt >> 1; (j ^= k) < k; k >>= 1); } vector<int> w(lmt >> 1); for(int mid = 1; mid < lmt; mid <<= 1) { w[0] = 1; int w0 = ksm(opt == 1 ? 3 : (mod + 1) / 3, (mod - 1) / mid / 2); for(int i = 1; i < mid; i++) w[i] = mul(w[i - 1], w0); for(int j = 0; j < lmt; j += mid << 1) for(int k = 0; k < mid; k++) { int x = a[j + k], y = mul(a[j + mid + k], w[k]); a[j + k] = add(x, y), a[j + mid + k] = sub(x, y); } } if(opt == -1) for(int i = 0, inv = ksm(lmt); i < lmt; i++) a[i] = mul(a[i], inv); } vector<int> Mul(const vector<int> &A, const vector<int> &B) { vector<int> a = A, b = B; int lmt = Get(A.size() + B.size() - 2); ntt(a, lmt, 1), ntt(b, lmt, 1); for(int i = 0; i < lmt; i++) a[i] = mul(a[i], b[i]); ntt(a, lmt, -1); return a.resize(A.size() + B.size() - 1), a; } }
void Manacher() { int mx = 0, pos = 0; RL[0] = 1; for(int i = 0; i < m; i++) { RL[i] = mx > i ? Min(RL[pos * 2 - i], mx - i) : 1; for(; t[i - RL[i]] == t[i + RL[i]]; ++RL[i]); if(i + RL[i] > mx) mx = i + RL[i], pos = i; } for(int i = 0; i < m; i++) ans = (ans - RL[i] / 2 + mod) % mod; } int main() { scanf("%s", s), n = strlen(s); t[m++] = '*'; for(int i = 0; i < n; i++) t[m++] = '#', t[m++] = s[i]; t[m++] = '#', Manacher(); ba[0] = 1; for(int i = 1; i <= n; i++) ba[i] = (ba[i - 1] + ba[i - 1]) % mod; a.clear(), b.clear(), res.clear(); for(int i = 0; i < n; i++) a.pb(s[i] == 'a' ? 1 : 0); b = a, res = Poly::Mul(a, b), res.resize(n + n); for(int i = 0; i < n; i++) if(s[i] == 'a') --res[i + i]; for(int i = 0; i <= n + n - 2; i++) cnt[i] += res[i] / 2; a.clear(), b.clear(), res.clear(); for(int i = 0; i < n; i++) a.pb(s[i] == 'b' ? 1 : 0); b = a, res = Poly::Mul(a, b), res.resize(n + n); for(int i = 0; i < n; i++) if(s[i] == 'b') --res[i + i]; for(int i = 0; i <= n + n - 2; i++) cnt[i] += res[i] / 2;
for(int i = 0; i <= n + n - 2; i++) { if(i & 1) ans = ((ans + ba[cnt[i]]) % mod + mod - 1) % mod; else ans = ((ans + ba[cnt[i] + 1]) % mod + mod - 1) % mod; } printf("%d\n", ans); return 0; }
|