• BZOJ4503 两个串 【fft】


    题目链接

    BZOJ4503

    题解

    水水题。
    和残缺的字符串那题几乎是一样的
    同样转化为多项式
    同样TLE
    同样要手写一下复数才A

    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<complex>
    #include<cmath>
    #include<map>
    #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 mp(a,b) make_pair<int,int>(a,b)
    #define cls(s) memset(s,0,sizeof(s))
    #define cp pair<int,int>
    #define LL long long int
    using namespace std;
    const int maxn = 400005,maxm = 100005,INF = 1000000000;
    inline int read(){
    	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;
    }
    struct E{
    	double a,b;
    	E(){}
    	E(double x,double y):a(x),b(y) {}
    	E(int x,int y):a(x),b(y) {}
    	inline E operator =(const int& b){
    		this->a = b; this->b = 0;
    		return *this;
    	}
    	inline E operator =(const double& b){
    		this->a = b; this->b = 0;
    		return *this;
    	}
    	inline E operator /=(const double& b){
    		this->a /= b; this->b /= b;
    		return *this;
    	}
    };
    inline E operator *(const E& a,const E& b){
    	return E(a.a * b.a - a.b * b.b,a.a * b.b + a.b * b.a);
    }
    inline E operator *=(E& a,const E& b){
    	return a = E(a.a * b.a - a.b * b.b,a.a * b.b + a.b * b.a);
    }
    inline E operator +(const E& a,const E& b){
    	return E(a.a + b.a,a.b + b.b);
    }
    inline E operator -(const E& a,const E& b){
    	return E(a.a - b.a,a.b - b.b);
    }
    const double pi = acos(-1);
    int R[maxn];
    void fft(E* a,int n,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),x,y;
    			for (int k = 0; k < i; k++,w = w * wn){
    				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;
    }
    E A[maxn],B[maxn];
    int N,M,L,ans[maxn],ansi;
    double C[maxn];
    char S[maxn],T[maxn];
    int main(){
    	scanf("%s",S); N = strlen(S);
    	scanf("%s",T); M = strlen(T);
    	reverse(T,T + M);
    	int n,m; double t;
    	m = N - 1 + M - 1; L = 0;
    	for (n = 1; n <= m; n <<= 1) L++;
    	for (int i = 1; i < n; i++) R[i] = (R[i >> 1] >> 1) | ((i & 1) << (L - 1));
    	for (int i = 0; i < N; i++){
    		t = S[i] - 'a' + 1;
    		A[i] = t * t;
    	}
    	for (int i = 0; i < M; i++)
    		if (T[i] == '?') B[i] = 0;
    		else B[i] = T[i] - 'a' + 1;
    	fft(A,n,1); fft(B,n,1);
    	for (int i = 0; i < n; i++) A[i] *= B[i];
    	fft(A,n,-1);
    	for (int i = 0; i < N; i++) C[i] += floor(A[i].a + 0.5);
    		
    	for (int i = 0; i < N; i++) A[i] = 1;
    	for (int i = N; i < n; i++) A[i] = 0;
    	for (int i = 0; i < M; i++)
    		if (T[i] == '?') B[i] = 0;
    		else {
    			t = T[i] - 'a' + 1;
    			B[i] = t * t * t;
    		}
    	for (int i = M; i < n; i++) B[i] = 0;
    	fft(A,n,1); fft(B,n,1);
    	for (int i = 0; i < n; i++) A[i] *= B[i];
    	fft(A,n,-1);
    	for (int i = 0; i < N; i++) C[i] += floor(A[i].a + 0.5);
    	
    	for (int i = 0; i < N; i++)
    		A[i] = S[i] - 'a' + 1;
    	for (int i = N; i < n; i++) A[i] = 0;
    	for (int i = 0; i < M; i++)
    		if (T[i] == '?') B[i] = 0;
    		else {
    			t = T[i] - 'a' + 1;
    			B[i] = t * t;
    		}
    	for (int i = M; i < n; i++) B[i] = 0;
    	fft(A,n,1); fft(B,n,1);
    	for (int i = 0; i < n; i++) A[i] *= B[i];
    	fft(A,n,-1);
    	for (int i = 0; i < N; i++) C[i] -= 2 * floor(A[i].a + 0.5);
    	for (int i = M - 1; i < N; i++)
    		if (fabs(C[i]) < 0.1) ans[++ansi] = i - M + 1;
    	printf("%d
    ",ansi);
    	REP(i,ansi) printf("%d
    ",ans[i]);
    	return 0;
    }
    
    
  • 相关阅读:
    xml的servlet配置
    python Matplotlib 系列教程(三)——绘制直方图和条形图
    https://blog.csdn.net/blmoistawinde/article/details/84329103
    机器学习——标准化/归一化的目的、作用和场景
    梯度提升决策树(GBDT)与XGBoost、LightGBM
    最容易理解的对卷积(convolution)的解释
    如何通俗易懂地解释卷积?
    卷积神经网络(CNN)之一维卷积、二维卷积、三维卷积详解
    Conv1D和Conv2D的区别
    卷积神经网络(CNN)张量(图像)的尺寸和参数计算(深度学习)
  • 原文地址:https://www.cnblogs.com/Mychael/p/9112828.html
Copyright © 2020-2023  润新知