题意:给你一个解密后的字符串,给你加密方式,加密过程可能出错,字符可能加减1,然后给你一个字符串,要求匹配个数(其实我也不太懂具体怎么加密解密,反正你把给你的前两个字符串用第三个加密一下,然后搞可以有一个ascaii码误差的字符串匹配即可,)
题解:fft加速字符串匹配
假设上面的串是s,长度是m,下面的串是p,长度是n,(详细讲解请看上一篇fft关于字符串匹配的博客)现在匹配方程变成(sum_{j=1}^m(p_{i+j}-s_{n-j})^2*((p_{i+j}-s{n-j})^2-1)=0),,把它拆开就变成了(sum_{j=1}^m(p_{i+j}^4-p_{i+j}^2+s_{n-j}^4-s_{n-j}^2+2*p_{i+j}*s_{n-j}+6*p_{i+j}^2*s_{n-j}^2-4*p_{i+j}^3*s_{n-j}-4*p_{i+j}*s_{n-j}^3)=0),然后对后四项进行fft算多项式,前四项颗直接前缀和求出,最后加起来看是不是0即可判断是否匹配
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000007
#define ld long double
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
//#define cd complex<double>
#define ull unsigned long long
#define base 1000000000000000000
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define fio ios::sync_with_stdio(false);cin.tie(0)
template<typename T>
inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>
inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}
using namespace std;
const double eps=1e-8;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=250000+10,maxn=50000+10,inf=0x3f3f3f3f;
struct cd{
db x,y;
cd(db _x=0.0,db _y=0.0):x(_x),y(_y){}
cd operator +(const cd &b)const{
return cd(x+b.x,y+b.y);
}
cd operator -(const cd &b)const{
return cd(x-b.x,y-b.y);
}
cd operator *(const cd &b)const{
return cd(x*b.x - y*b.y,x*b.y + y*b.x);
}
cd operator /(const db &b)const{
return cd(x/b,y/b);
}
}a[N<<3],b[N<<3],c[N<<3],d[N<<3],e[N<<3],f[N<<3],g[N<<3];
int rev[N<<3];
void getrev(int bit)
{
for(int i=0;i<(1<<bit);i++)
rev[i]=(rev[i>>1]>>1) | ((i&1)<<(bit-1));
}
void fft(cd *a,int n,int dft)
{
for(int i=0;i<n;i++)
if(i<rev[i])
swap(a[i],a[rev[i]]);
for(int step=1;step<n;step<<=1)
{
cd wn(cos(dft*pi/step),sin(dft*pi/step));
for(int j=0;j<n;j+=step<<1)
{
cd wnk(1,0);
for(int k=j;k<j+step;k++)
{
cd x=a[k];
cd y=wnk*a[k+step];
a[k]=x+y;a[k+step]=x-y;
wnk=wnk*wn;
}
}
}
if(dft==-1)for(int i=0;i<n;i++)a[i]=a[i]/n;
}
char s[N],p[N],ch[N];
ll sums[N],sump[N];
int main()
{
scanf("%s%s%s",s+1,p+1,ch+1);
int n=strlen(s+1),m=strlen(p+1);
int sz=0;
while((1<<sz)<m)sz++;
sz++,getrev(sz);
for(int i=0;i<=(1<<sz);i++)
a[i]=b[i]=c[i]=d[i]=e[i]=f[i]=0;
for(int i=n,te;i>=1;i--)
{
s[i]=ch[s[i]-'a'+1];
te=s[i]-'a'+1;
a[m-i]=-4*te*te*te,b[m-i]=6*te*te,c[m-i]=-4*te,d[m-i]=2*te;
sums[i]=sums[i+1]+1ll*te*te*te*te-1ll*te*te;
}
for(int i=1,te;i<=m;i++)
{
p[i]=ch[p[i]-'a'+1];
te=p[i]-'a'+1;
e[i]=te*te*te,f[i]=te*te,g[i]=te;
sump[i]=sump[i-1]+1ll*te*te*te*te-1ll*te*te;
}
fft(a,(1<<sz),1),fft(b,(1<<sz),1),fft(c,(1<<sz),1);
fft(d,(1<<sz),1),fft(e,(1<<sz),1),fft(f,(1<<sz),1);fft(g,(1<<sz),1);
for(int i=0;i<=(1<<sz);i++)
d[i]=a[i]*g[i]+b[i]*f[i]+c[i]*e[i]+d[i]*g[i];
fft(d,(1<<sz),-1);
// for(int i=0;i<=m-n;i++)
// printf("%d ",(int)((d[m+i].x+0.5)/(1<<sz)));
// puts("");
vi ans;
for(int i=0;i<=m-n;i++)
if((int)(d[m+i].x+sump[i+n]-sump[i]+sums[1])==0)
ans.pb(i+1);
printf("%d
",ans.size());
for(int i=0;i<ans.size();i++)
printf("%d ",ans[i]);
puts("");
return 0;
}
/********************
********************/