bzoj 4503 两个串 FFT

题面

题目传送门

解法

挺妙的一道题,思路比较精妙

带通配符之后好像字符串算法就不太好匹配了啊

设通配符的值为0,那么这两个字符相同当且仅当有一个是通配符或者就是相同的

\(dis(A,B)=\sum_{i=0}^{n-1}(A_i-B_i)^2A_iB_i\)

其中,若\(A_i=?\),那么\(A_i=0\)

如果\(dis(A,B)=0\),那么显然这两个字符串是匹配的

\(p_i\)表示\(B\)\(i\)结尾的字符串和\(A\)的匹配情况

翻转A串,然后可以得出以下式子:
$\(p_i=\sum_{j=0}^{n-1}A_j^3B_{i-j}-2\sum_{j=0}^{n-1}A_j^2B_{i-j}^2+\sum_{j=0}^{n-1}A_jB_{i-j}^3\)

显然,这是一个卷积的形式,可以FFT

分三次FFT即可

时间复杂度:\(O(n\ log \ n)\)

常数巨大

代码

#include <bits/stdc++.h>
#define N 1 << 21
using namespace std;
template <typename node> void chkmax(node &x, node y) {x = max(x, y);}
template <typename node> void chkmin(node &x, node y) {x = min(x, y);}
template <typename node> void read(node &x) {
    x = 0; int f = 1; char c = getchar();
    while (!isdigit(c)) {if (c == '-') f = -1; c = getchar();}
    while (isdigit(c)) x = x * 10 + c - '0', c = getchar(); x *= f;
}
struct Complex {
    double x, y;
    Complex (double tx = 0, double ty = 0) {x = tx, y = ty;}
} a[N], b[N], c[N];
const double pi = acos(-1);
int n, m, A[N], B[N], ans[N], rev[N];
Complex operator + (Complex a, Complex b) {return (Complex) {a.x + b.x, a.y + b.y};}
Complex operator - (Complex a, Complex b) {return (Complex) {a.x - b.x, a.y - b.y};}
Complex operator * (Complex a, Complex b) {return (Complex) {a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x};} 
Complex operator * (Complex a, double b) {return (Complex) {a.x * b, a.y * b};}
string tx, ty;
void getrev(int l) {
    for (int i = 0; i < (1 << l); i++)
        rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << l - 1);
}
void FFT(Complex *a, int n, int key) {
    for (int i = 0; i < n; i++)
        if (i < rev[i]) swap(a[i], a[rev[i]]);
    for (int i = 1; i < n; i <<= 1) {
        Complex wn(cos(pi / i), key * sin(pi / i));
        for (int r = i << 1, j = 0; j < n; j += r) {
            Complex w(1, 0);
            for (int k = 0; k < i; k++, w = w * wn) {
                Complex x = a[j + k], y = w * a[i + j + k];
                a[j + k] = x + y, a[i + j + k] = x - y;
            }
        }
    }
    if (key == -1)
        for (int i = 0; i < n; i++) a[i].x /= n;
}
int main() {
    cin >> tx >> ty;
    int m = tx.size(), n = ty.size();
    for (int i = 0; i < n; i++)
        if (ty[i] != '?') A[i] = ty[i] - 'a' + 1;
    for (int i = 0; i < m; i++)
        if (tx[i] != '?') B[i] = tx[i] - 'a' + 1;
    int len = 1, l = 0;
    while (len <= 2 * m) len <<= 1, l++; getrev(l); reverse(A, A + n);
    for (int i = 0; i < len; i++) a[i] = (Complex) {A[i] * A[i] * A[i], 0};
    for (int i = 0; i < len; i++) b[i] = (Complex) {B[i], 0};
    FFT(a, len, 1), FFT(b, len, 1);
    for (int i = 0; i < len; i++) c[i] = c[i] + (a[i] * b[i]);
    for (int i = 0; i < len; i++) a[i] = (Complex) {A[i] * A[i], 0};
    for (int i = 0; i < len; i++) b[i] = (Complex) {B[i] * B[i], 0};
    FFT(a, len, 1), FFT(b, len, 1);
    for (int i = 0; i < len; i++) c[i] = c[i] - (a[i] * b[i] * 2.0);
    for (int i = 0; i < len; i++) a[i] = (Complex) {A[i], 0};
    for (int i = 0; i < len; i++) b[i] = (Complex) {B[i] * B[i] * B[i], 0};
    FFT(a, len, 1), FFT(b, len, 1);
    for (int i = 0; i < len; i++) c[i] = c[i] + (a[i] * b[i]);
    FFT(c, len, -1); int tot = 0;
    for (int i = n - 1; i < m; i++)
        if (fabs(c[i].x) < 0.5) ans[++tot] = i - n + 1;
    cout << tot << "\n";
    for (int i = 1; i <= tot; i++) cout << ans[i] << ' ';
    return 0;
}

转载于:https://www.cnblogs.com/copperoxide/p/9476745.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值