# BZOJ4259 残缺的字符串 【fft】

3 7

a*b

aebr*ob

2

1 5

## 题解

$\sum\limits_{i=1}^{len} (A_i - B_i)^2 * A_i * B_i$

#include<iostream>
#include<cstdio>
#include<cmath>
#include<complex>
#include<cstring>
#include<algorithm>
#define eps 1e-9
#define LL long long int
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define BUG(s,n) for (int i = 1; i <= (n); i++) cout<<s[i]<<' '; puts("");
using namespace std;
const int maxn = 1200005,maxm = 100005,INF = 1000000000;
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
const double pi = acos(-1);
struct E{
double r,i;
E(){}
E(double a,double b):r(a),i(b){}
E operator =(const int& b){
r = b; i = 0;
return *this;
}
};
inline E operator +(const E& a,const E& b){
return E(a.r + b.r,a.i + b.i);
}
inline E operator -(const E& a,const E& b){
return E(a.r - b.r,a.i - b.i);
}
inline E operator *(const E& a,const E& b){
return E(a.r * b.r - a.i * b.i,a.r * b.i + b.r * a.i);
}
inline E operator *=(E& a,const E& b){
return (a = a * b);
}
inline E operator /(E& a,const double& b){
return E(a.r / b,a.i / b);
}
inline E operator /=(E& a,const double& b){
return (a = a / b);
}
int n,m,L,R[maxn];
E A[maxn],B[maxn];
void fft(E* a,int f){
for (int i = 0; i < n; i++) if (i < R[i]) swap(a[i],a[R[i]]);
for (int i = 1; i < n; i <<= 1){
E wn(cos(pi / i),f * sin(pi / i));
for (int j = 0; j < n; j += (i << 1)){
E w(1,0);
for (int k = 0; k < i; k++,w *= wn){
E x = a[j + k],y = w * a[j + k + i];
a[j + k] = x + y; a[j + k + i] = x - y;
}
}
}
if (f == -1) for (int i = 0; i < n; i++) a[i] /= n;
}
char P[maxn],T[maxn];
int len,lm,ans[maxn],ansi;
double C[maxn];
void solve(){
int tmp;
for (int i = 0; i < n; i++) A[i] = B[i] = 0;
for (int i = 0; i < len; i++){
if (P[i] != '*'){
tmp = (P[i] - 'a' + 1);
A[i] = tmp * tmp * tmp;
}
}
for (int i = 0; i < lm; i++){
if (T[i] != '*'){
tmp = (T[i] - 'a' + 1);
B[i] = tmp;
}
}
fft(A,1); fft(B,1);
for (int i = 0; i < n; i++) A[i] *= B[i];
fft(A,-1);
for (int i = 0; i < n; i++) C[i] = floor(A[i].r + 0.5);

for (int i = 0; i < n; i++) A[i] = B[i] = 0;
for (int i = 0; i < len; i++){
if (P[i] != '*'){
tmp = (P[i] - 'a' + 1);
A[i] = tmp * tmp;
}
}
for (int i = 0; i < lm; i++){
if (T[i] != '*'){
tmp = (T[i] - 'a' + 1);
B[i] = tmp * tmp;
}
}
fft(A,1); fft(B,1);
for (int i = 0; i < n; i++) A[i] *= B[i];
fft(A,-1);
for (int i = 0; i < n; i++) C[i] -= 2 * floor(A[i].r + 0.5);

for (int i = 0; i < n; i++) A[i] = B[i] = 0;
for (int i = 0; i < len; i++){
if (P[i] != '*'){
tmp = (P[i] - 'a' + 1);
A[i] = tmp;
}
}
for (int i = 0; i < lm; i++){
if (T[i] != '*'){
tmp = (T[i] - 'a' + 1);
B[i] = tmp * tmp * tmp;
}
}
fft(A,1); fft(B,1);
for (int i = 0; i < n; i++) A[i] *= B[i];
fft(A,-1);
for (int i = 0; i < n; i++) C[i] += floor(A[i].r + 0.5);
}
int main(){
scanf("%s",P);
scanf("%s",T);
for (int i = 0; i < (len >> 1); i++) swap(P[i],P[len - i - 1]);
m = len + lm - 2;
for (n = 1; n <= m; n <<= 1) L++;
for (int i = 0; i < n; i++) R[i] = (R[i >> 1] >> 1) | ((i & 1) << (L - 1));
solve();
for (int i = 0; i <= m; i++){
if (fabs(C[i]) < eps && i - len + 2 > 0 && i - len + 2 <= lm - len + 1){
ans[++ansi] = i - len + 2;
}
}
printf("%d\n",ansi);
for (int i = 1; i <= ansi; i++) printf("%d ",ans[i]);
return 0;
}


posted @ 2018-04-09 14:08  Mychael  阅读(223)  评论(0编辑  收藏  举报