1. 程式人生 > >luogu4173 殘缺的字符串

luogu4173 殘缺的字符串

algorithm rev cout tps while complex () www i++

there

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
using namespace std;
int n, m, aa[300005], bb[300005], lim=1, tmpcnt, rev[1100005];
const double PI=acos(-1.0);
char ss[300005];
vector<int> vec;
struct Complex{
    double
x, y; Complex(double xx=0.0, double yy=0.0){ x = xx; y = yy; } Complex operator+(const Complex &u)const{ return Complex(x+u.x, y+u.y); } Complex operator-(const Complex &u)const{ return Complex(x-u.x, y-u.y); } Complex operator*(const Complex &u)const
{ return Complex(x*u.x-y*u.y, x*u.y+y*u.x); } Complex operator*(double u)const{ return Complex(x*u, y*u); } }a[1100005], b[1100005], c[1100005]; void fft(Complex a[], int opt){ for(int i=0; i<lim; i++) if(i<rev[i]) swap(a[i], a[rev[i]]); for(int i=2; i<=lim; i<<=1
){ int tmp=i>>1; Complex wn=Complex(cos(PI*2.0/i), opt*sin(PI*2.0/i)); for(int j=0; j<lim; j+=i){ Complex w=Complex(1.0, 0.0); for(int k=0; k<tmp; k++){ Complex tmp1=a[j+k], tmp2=w*a[j+k+tmp]; a[j+k] = tmp1 + tmp2; a[j+k+tmp] = tmp1 - tmp2; w = w * wn; } } } if(opt==-1) for(int i=0; i<lim; i++) a[i].x /= lim; } int main(){ cin>>m>>n; while(lim<=n+m) lim <<= 1, tmpcnt++; for(int i=0; i<lim; i++) rev[i] = (rev[i>>1]>>1) | ((i&1)<<(tmpcnt-1)); scanf("%s", ss); for(int i=0; i<m; i++) if(ss[i]!=‘*‘) aa[i] = ss[i] - ‘a‘ + 1; scanf("%s", ss); for(int i=0; i<n; i++) if(ss[i]!=‘*‘) bb[i] = ss[i] - ‘a‘ + 1; reverse(aa, aa+m); for(int i=0; i<m; i++) a[i] = Complex(aa[i]*aa[i]*aa[i], 0.0); for(int i=0; i<n; i++) b[i] = Complex(bb[i], 0.0); fft(a, 1); fft(b, 1); for(int i=0; i<lim; i++) c[i] = a[i] * b[i]; memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b)); for(int i=0; i<m; i++) a[i] = Complex(aa[i]*aa[i], 0.0); for(int i=0; i<n; i++) b[i] = Complex(bb[i]*bb[i], 0.0); fft(a, 1); fft(b, 1); for(int i=0; i<lim; i++) c[i] = c[i] - a[i] * b[i] * 2.0; memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b)); for(int i=0; i<m; i++) a[i] = Complex(aa[i], 0.0); for(int i=0; i<n; i++) b[i] = Complex(bb[i]*bb[i]*bb[i], 0.0); fft(a, 1); fft(b, 1); for(int i=0; i<lim; i++) c[i] = c[i] + a[i] * b[i]; fft(c, -1); for(int i=m-1; i<n; i++) if(fabs(c[i].x)<1e-6) vec.push_back(i-m+2); cout<<vec.size()<<endl; for(int i=0; i<vec.size(); i++) printf("%d ", vec[i]); printf("\n"); return 0; }

luogu4173 殘缺的字符串