Description
click me
直接就把pdf的图片放在那里,还不方便复制样例,差评
Solution
考虑用所有的位置对称的回文子序列的方案数减去回文串的数量,那么题目就变成了两个部分:
1. 回文串的数量直接用Manacher求即可。
2. 回文子序列可以用FFT来求,分别把a、b看做1,然后做FFT求卷积即可。
感觉用FFT解决字符串问题也是个套路吧,回文串直接FFT,字符串匹配倒过来FFT就行了。
Source
/************************************************
* Au: Hany01
* Date: Mar 28th, 2018
* Prob: [BZOJ3160] 万径人踪灭
* Email: hany[email protected]
************************************************/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia
template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, : ; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, : ; }
inline int read()
{
register int _, __; register char c_;
for (_ = , __ = , c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -;
for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << ) + (_ << ) + (c_ ^ );
return _ * __;
}
inline LL Pow(LL a, LL b) {
LL Ans = ;
for ( ; b; b >>= , (a *= a) %= Mod) if (b & ) (Ans *= a) %= Mod;
return Ans;
}
const double pi = acos(-);
const int maxn = ;
int n, p[maxn], A[maxn << ], B[maxn << ], N, cnt, rev[maxn << ];
char s1[maxn], s[maxn << ];
LL Ans;
struct cpx
{
double real, imag;
cpx(double real = , double imag = ): real(real), imag(imag) {}
}a[maxn << ];
inline cpx operator + (const cpx& A, const cpx& B) { return cpx(A.real + B.real, A.imag + B.imag); }
inline cpx operator - (const cpx& A, const cpx& B) { return cpx(A.real - B.real, A.imag - B.imag); }
inline cpx operator * (const cpx& A, const cpx& B) { return cpx(A.real * B.real - A.imag * B.imag, A.real * B.imag + A.imag * B.real); }
inline void Manacher(char* s, int len)
{
static int mx = , id;
rep(i, len) {
if (mx > i) p[i] = min(mx - i, p[(id << ) - i]); else p[i] = ;
while (s[i - p[i]] == s[i + p[i]]) ++ p[i];
if (chkmax(mx, i + p[i])) id = i;
}
}
inline void FFT(cpx A[], int type)
{
rep(i, N) if (i < rev[i]) swap(A[rev[i]], A[i]);
for (int i = ; i <= N; i <<= ) {
cpx wn = cpx(cos( * pi / i), type * sin( * pi / i));
int p = i / ;
for (int j = ; j < N; j += i) {
cpx w = cpx(, );
for (int k = ; k < p; ++ k, w = w * wn) {
cpx u = A[j + k], v = w * A[j + k + p];
A[j + k] = u + v, A[j + k + p] = u - v;
}
}
}
}
int main()
{
#ifdef hany01
File("bzoj3160");
#endif
//Init
scanf("%s", s1), n = strlen(s1);
rep(i, n) s[i << | ] = '#', s[(i + ) << ] = s1[i];
s[] = '#', s[n << | ] = '#', n = (n << ) + , s[] = '$';
//Get the number of continuity palindrome strings
Manacher(s, n);
//Init for FFT
for (N = ; N < (n << ); N <<= , ++ cnt) ;
rep(i, N) rev[i] = (rev[i >> ] >> ) | ((i & ) << (cnt - ));
//Get the number of schemes of palindrome subsequences
//a
rep(i, n) if (s[i] == 'a') a[i].real = ;
//rep(i, N) cout << a[i].real << ' ';
//cout << endl;
FFT(a, );
rep(i, N) a[i] = a[i] * a[i];
FFT(a, -);
rep(i, N) A[i] = floor(a[i].real / N + );
//rep(i, N) cout << A[i] << ' ';
//cout << endl;
//b
rep(i, N) a[i] = cpx(, );
rep(i, n) if (s[i] == 'b') a[i]= cpx(, );
FFT(a, );
rep(i, N) a[i] = a[i] * a[i];
FFT(a, -);
rep(i, N) B[i] = floor(a[i].real / N + );
//rep(i, N) cout << B[i] << ' ';
//cout << endl;
rep(i, n) {
int ctn = p[i] - ;
(Ans += Pow(, (A[i << ] + B[i << ] + ) >> ) - ) %= Mod;
//cout << ctn << ' ' << A[i << 1] << ' ' << B[i << 1] <<endl;
(Ans -= (ctn + ) >> ) %= Mod;
//cout << Ans << endl;
}
printf("%lld\n", (Ans + Mod) % Mod);
return ;
}
//登鸾车,侍轩辕,遨游青天中,其乐不可言。
// -- 李白《飞龙引二首·其一》