码迷,mamicode.com
首页 > 其他好文 > 详细

luogu4173 残缺的字符串

时间:2018-04-10 13:31:51      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:scan   tor   main   https   cos   eof   complex   abs   href   

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 残缺的字符串

标签:scan   tor   main   https   cos   eof   complex   abs   href   

原文地址:https://www.cnblogs.com/poorpool/p/8778078.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!