【BZOJ4259】残缺的字符串(FFT)

Description

辣鸡BZOJ


Solution

考虑将子串倒过来,对于每个字符,如果是 ,那么值为 0 ,否则为 A i 96
那么我们设 f ( i ) = j ( A j B i j ) 2 A j B i j ,如果 f ( i ) 0 ,则对应位置上可以匹配上。
f ( i ) 展开,发现是几个卷积的形式,FFT即可。


Code

/************************************************
 * Au: Hany01
 * Date: May 31st, 2018
 * Prob: [BZOJ4259] 残缺的字符串
 * Email: [email protected]
************************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read()
{
    register int _, __; register char c_;
    for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

const double PI = acos(-1.), eps = 1e-5;
const int maxn = 1200005;

int n1, n2, n, N, cnt, rev[maxn], S[maxn], T[maxn], ans[maxn];
char s1[maxn], s2[maxn];

struct Complex {
    double real, imag;
    Complex(double real = 0, double imag = 0): real(real), imag(imag) {}
}S1[maxn], S2[maxn], S3[maxn], T1[maxn], T2[maxn], T3[maxn], f[maxn];
Complex operator + (Complex A, Complex B) { return Complex(A.real + B.real, A.imag + B.imag); }
Complex operator - (Complex A, Complex B) { return Complex(A.real - B.real, A.imag - B.imag); }
Complex operator * (Complex A, Complex B) { return Complex(A.real * B.real - A.imag * B.imag, A.real * B.imag + A.imag * B.real); }
Complex operator * (double p, Complex A) { return Complex(A.real * p, A.imag * p); }

inline int dcmp(double x) {
    if (fabs(x) < eps) return 0;
    return x < 0 ? -1 : 1;
}

inline void FFT(Complex* a, int type)
{
    rep(i, N) if (i < rev[i]) swap(a[i], a[rev[i]]);
    for (int i = 2; i <= N; i <<= 1) {
        Complex wn = Complex(cos(2 * PI / i), sin(2 * PI / i) * type);
        for (int j = 0; j < N; j += i) {
            Complex w = Complex(1, 0);
            rep(k, i >> 1) {
                Complex x = a[j + k], y = a[j + k + (i >> 1)] * w;
                a[j + k] = x + y, a[j + k + (i >> 1)] = x - y;
                w = w * wn;
            }
        }
    }
}

int main()
{
#ifdef hany01
    File("bzoj4259");
#endif

    n1 = read(), n2 = read(), scanf("%s", s1), scanf("%s", s2), reverse(s1, s1 + n1);
    rep(i, n1) S[i] = s1[i] == '*' ? 0 : s1[i] - 96;
    rep(i, n2) T[i] = s2[i] == '*' ? 0 : s2[i] - 96;
    for (n = n1 + n2 - 1, N = 1; N < n; ++ cnt, N <<= 1);
    rep(i, N) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (cnt - 1));

    rep(i, n1) S1[i].real = S[i], S2[i].real = S[i] * S[i], S3[i].real = S2[i].real * S[i];
    rep(i, n2) T1[i].real = T[i], T2[i].real = T[i] * T[i], T3[i].real = T2[i].real * T[i];
    FFT(S1, 1), FFT(S2, 1), FFT(S3, 1), FFT(T1, 1), FFT(T2, 1), FFT(T3, 1);
    rep(i, N) f[i] = S3[i] * T1[i] - 2 * S2[i] * T2[i] + S1[i] * T3[i];
    FFT(f, -1), cnt = 0;
    For(i, n1 - 1, n2 - 1) if (!(int)(f[i].real / N + .5))
        ans[++ cnt] = i - n1 + 2;
    printf("%d\n", cnt);
    For(i, 1, cnt) printf("%d ", ans[i]);

    return 0;
}
//居高声自远,非是藉秋风。
//    -- 虞世南《蝉》

猜你喜欢

转载自blog.csdn.net/hhaannyyii/article/details/80522996