BZOJ 4259 [FFT]

Description

很久很久以前,在你刚刚学习字符串匹配的时候,有两个仅包含小写字母的字符串A和B,其中A串长度为m,B串长度为n。可当你现在再次碰到这两个串时,这两个串已经老化了,每个串都有不同程度的残缺。你想对这两个串重新进行匹配,其中A为模板串,那么现在问题来了,请回答,对于B的每一个位置i,从这个位置开始连续m个字符形成的子串是否可能与A串完全匹配?

Solution

将星号看成0;匹配就等价于 aibi(aibi)2==0 翻转后FFT即可。
为什么我的FFT这么sb卡常卡了好久,最后用最原始的FFT过掉了,难道这个有问题。。。

(这是过的)

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

inline char get(void) {
    static char buf[100000], *S = buf, *T = buf;
    if (S == T) {
        T = (S = buf) + fread(buf, 1, 100000, stdin);
        if (S == T) return EOF;
    }
    return *S++;
}
inline void read(int &x) {
    static char c; x = 0;
    for (c = get(); c < '0' || c > '9'; c = get());
    for (; c >= '0' && c <= '9'; c = get()) x = x * 10 + c - '0';
}
inline void read(char *s) {
    static int len; len = 0; static char c;
    for (c = get(); (c < 'a' || c > 'z') && c != '*'; c = get());
    for (; (c >= 'a' && c <= 'z') || c == '*'; c = get()) s[len++] = c;
}

const int N = 1203030;
const double PI = acos(-1);

struct C {
    double r, i;
    inline C(double R = 0, double I = 0):r(R), i(I) {}
    friend C operator +(const C &b, const C &a) {
        return C(b.r + a.r, b.i + a.i);
    }
    friend C operator -(const C &b, const C &a) {
        return C(b.r - a.r, b.i - a.i);
    }
    friend C operator *(const C &b, const C &a) {
        return C(b.r * a.r - b.i * a.i, b.r * a.i + b.i * a.r);
    }
    friend C operator /(const C &b, const double &a) {
        return C(b.r / a, b.i / a);
    }
};
int n1, n2, L, num, m, n, ans;
int a[N], b[N];
C A[N], B[N], F[N];
int R[N], Ans[N];
char s[N];

inline void FFT(C *a, int n, int f) {
    static C x, y;
    for (int i = 0; i < n; i++)
        if (R[i] > i) swap(a[R[i]], a[i]);
    for (int i = 1; i < n; i <<= 1) {
        C wn(cos(PI / i), f * sin(PI / i));
        for (int j = 0; j < n; j += (i << 1)) {
            C w(1, 0);
            for (int k = 0; k < i; k++) {
                x = a[j + k]; y = a[j + k + i] * w;
                a[j + k] = x + y; a[j + k + i] = x - y; w = w * wn;
            }
        }
    }
    if (f == -1) for (int i = 0; i < n; i++) a[i] = a[i] / n;
}

int main(void) {
    freopen("1.in", "r", stdin);
    freopen("1.out", "w", stdout);
    read(n1); read(n2);
    read(s); n1--;
    for (int i = 0; i <= n1; i++)
        a[i] = (s[i] == '*' ? 0 : s[i] - 'a' + 1);
    read(s); n2--;
    for (int i = 0; i <= n2; i++)
        b[i] = (s[i] == '*' ? 0 : s[i] - 'a' + 1);
    n = n2 * 2; n += 2; reverse(a, a + n1 + 1);
    for (m = 1; m <= n; m <<= 1) L++; L--;
    for (int i = 1; i < m; i++) R[i] = (R[i >> 1] >> 1) | ((i & 1) << L);
    for (int i = 0; i < m; i++) {
        A[i] = a[i] * a[i] * a[i]; B[i] = b[i];
    }
    FFT(A, m, 1); FFT(B, m, 1);
    for (int i = 0; i < m; i++) F[i] = A[i] * B[i];
    for (int i = 0; i < m; i++) {
        A[i] = a[i]; B[i] = b[i] * b[i] * b[i];
    }
    FFT(A, m, 1); FFT(B, m, 1);
    for (int i = 0; i < m; i++) F[i] = F[i] + A[i] * B[i];
    for (int i = 0; i < m; i++) {
        A[i] = a[i] * a[i]; B[i] = b[i] * b[i];
    }
    FFT(A, m, 1); FFT(B, m, 1);
    for (int i = 0; i < m; i++) F[i] = F[i] - A[i] * B[i] * 2;
    FFT(F, m, -1);
    for (int i = n1; i <= n2; i++)
        if ((int)(F[i].r + 0.4) == 0) Ans[++ans] = i;
    printf("%d\n", ans);
    for (int i = 1; i <= ans; i++) printf("%d ", Ans[i] - n1 + 1);
    putchar('\n');
}

(这有很大问题啊)

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

inline char get(void) {
    static char buf[100000], *S = buf, *T = buf;
    if (S == T) {
        T = (S = buf) + fread(buf, 1, 100000, stdin);
        if (S == T) return EOF;
    }
    return *S++;
}
inline void read(int &x) {
    static char c; x = 0;
    for (c = get(); c < '0' || c > '9'; c = get());
    for (; c >= '0' && c <= '9'; c = get()) x = x * 10 + c - '0';
}
inline void read(char *s) {
    static int len; len = 0; static char c;
    for (c = get(); (c < 'a' || c > 'z') && c != '*'; c = get());
    for (; (c >= 'a' && c <= 'z') || c == '*'; c = get()) s[len++] = c;
}

const int N = 1203030;
const double PI = acos(-1);

struct C {
    double r, i;
    inline C(double R = 0, double I = 0):r(R), i(I) {}
    friend C operator +(const C &b, const C &a) {
        return C(b.r + a.r, b.i + a.i);
    }
    friend C operator -(const C &b, const C &a) {
        return C(b.r - a.r, b.i - a.i);
    }
    friend C operator *(const C &b, const C &a) {
        return C(b.r * a.r - b.i * a.i, b.r * a.i + b.i * a.r);
    }
    friend C operator /(const C &b, const double &a) {
        return C(b.r / a, b.i / a);
    }
};
int n1, n2, L, num, m, n, ans;
int a[N], b[N];
C A[N], B[N], F[N];
int R[N], Ans[N];
char s[N];
C g;
C w[2][N];

void Prep(int n) {
    num = n; g = C(cos(PI * 2 / num), sin(PI * 2 / num));
    w[0][0] = w[1][0] = C(1, 0);
    for (int i = 1; i < num; i++) w[1][i] = w[1][i - 1] * g;
    for (int i = 1; i < num; i++) w[0][i] = w[1][num - i];
}
inline void FFT(C *a, int n, int f) {
    static C x, y;
    for (int i = 0; i < n; i++)
        if (R[i] > i) swap(a[R[i]], a[i]);
    for (int i = 1; i < n; i <<= 1)
        for (int j = 0; j < n; j += (i << 1))
            for (int k = 0; k < i; k++) {
                x = a[j + k]; y = a[j + k + i] * w[f][num / (i << 1) * k];
                a[j + k] = x + y; a[j + k + i] = x - y;
            }
    if (!f) for (int i = 0; i < n; i++) a[i] = a[i] / n;
}

int main(void) {
    freopen("1.in", "r", stdin);
    freopen("1.out", "w", stdout);
    read(n1); read(n2);
    read(s); n1--;
    for (int i = 0; i <= n1; i++)
        a[i] = (s[i] == '*' ? 0 : s[i] - 'a' + 1);
    read(s); n2--;
    for (int i = 0; i <= n2; i++)
        b[i] = (s[i] == '*' ? 0 : s[i] - 'a' + 1);
    n = n2 * 2; n += 2; reverse(a, a + n1 + 1);
    for (m = 1; m <= n; m <<= 1) L++; L--;
    Prep(m);
    for (int i = 1; i < m; i++) R[i] = (R[i >> 1] >> 1) | ((i & 1) << L);
    for (int i = 0; i < m; i++) {
        A[i] = a[i] * a[i] * a[i]; B[i] = b[i];
    }
    FFT(A, m, 1); FFT(B, m, 1);
    for (int i = 0; i < m; i++) F[i] = A[i] * B[i];
    for (int i = 0; i < m; i++) {
        A[i] = a[i]; B[i] = b[i] * b[i] * b[i];
    }
    FFT(A, m, 1); FFT(B, m, 1);
    for (int i = 0; i < m; i++) F[i] = F[i] + A[i] * B[i];
    for (int i = 0; i < m; i++) {
        A[i] = a[i] * a[i]; B[i] = b[i] * b[i];
    }
    FFT(A, m, 1); FFT(B, m, 1);
    for (int i = 0; i < m; i++) F[i] = F[i] - A[i] * B[i] * 2;
    FFT(F, m, 0);
    for (int i = n1; i <= n2; i++)
        if ((int)(F[i].r + 0.4) == 0) Ans[++ans] = i;
    printf("%d\n", ans);
    for (int i = 1; i <= ans; i++) printf("%d ", Ans[i] - n1 + 1);
    putchar('\n');
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值