luogu P4173 incomplete string FFT

Tips: If the subscript see if you can try to enlarge.

If not, then you can use wildcards KMP engage in a practice.

Listen giant guy said wildcards can be used FFT engage in a practice.

Let's not think about how to engage wildcards. We set a = 1, b = 2, ..., then we construct such a function \ (\ displaystyle P_x = \ sum_ {i = 0} ^ {m-1} (A_i-B_ {x-m + 1 } I +) ^ 2 \) , but only when a and B at the position x and a mating completion P_x $ 0 $. As to why the square, mainly to prevent the positive and negative numbers cancel each other out.

As a wildcard, we set it to zero, we attempt to reconstruct what \ (\ displaystyle P_x = \ sum_ {i = 0} ^ {m-1} (A_i-B_ {x-m + 1 + i}) ^ 2A_iB_ { + +. 1-m X I} \) , so that we can "wildcard" satisfied this condition.

Then how do we quickly solving it? We will first expand the equation a bit,

\(\displaystyle P_x=\sum_{i=0}^{m-1}(A_i^3B_{x-m+1+i}-A_i^2B_{x-m+1+i}^2+A_iB_{x-m+1+i}^3)\)

\(\displaystyle=\sum_{i=0}^{m-1}A_i^3B_{x-m+1+i}-\sum_{i=0}^{m-1}A_i^2B_{x-m+1+i}^2+\sum_{i=0}^{m-1}A_iB_{x-m+1+i}^3\)

Or the old method, we try to set up what turned over A \ (mi-A_i = C_ {1} \) , into the original equation.

\(\displaystyle=\sum_{i=0}^{m-1}C_{m-i-1}^3B_{x-m+1+i}-\sum_{i=0}^{m-1}C_{m-i-1}^2B_{x-m+1+i}^2+\sum_{i=0}^{m-1}C_{m-i-1}B_{x-m+1+i}^3\)

I find anything? The subscripts C and B is equal to the sum of x, so we change a written.

\(\displaystyle=\sum_{i+j=x}C_{i}^3B_{j}-\sum_{i+j=x}C_{i}^2B_{j}^2+\sum_{i+j=x}C_{i}B_{j}^3\)

Is not a lot of good-looking? This is obviously a convolution ...

FFT wave away the rest of it.

#include<bits/stdc++.h>
#define LL long long
#define DB double
using namespace std;
int n, m, lim;
const int N = 1200010;
const DB PI = acos(-1);
int r[N], cnt[N];
DB a[N], b[N];
char s1[N], s2[N];
struct xu 
{
    DB x, y;
    xu(DB X = 0, DB Y = 0) {x = X, y = Y;}
    friend xu operator +(const xu &a, const xu &b)
    {return (xu) {a.x + b.x, a.y + b.y};}
    friend xu operator -(const xu &a, const xu &b)
    {return (xu) {a.x - b.x, a.y - b.y};}
    friend xu operator *(const xu &a, const xu &b)
    {return (xu) {a.x*b.x - a.y*b.y, a.x*b.y + a.y*b.x};}
} A[N], B[N], ans[N];
void FFT(xu *A, int lim, int opt) 
{
    for (int i = 0; i < lim; ++i)
        r[i] = (r[i >> 1] >> 1) | ((i & 1) ? (lim >> 1) : 0);
    for (int i = 0; i < lim; ++i)
        if (i < r[i])swap(A[i], A[r[i]]);
    int len;
    xu wn, w, x, y;
    for (int mid = 1; mid < lim; mid <<= 1) 
    {
        len = mid << 1;
        wn = (xu) {cos(PI / mid), opt*sin(PI / mid)};
        for (int j = 0; j < lim; j += len) 
        {
            w = (xu) {1, 0};
            for (int k = j; k < j + mid; ++k, w = w * wn) 
            {
                x = A[k]; y = A[k + mid] * w;
                A[k] = x + y; A[k + mid] = x - y;
            }
        }
    }
    if (opt == 1)return;
    for (int i = 0; i < lim; ++i)A[i].x /= lim;
}
int main() 
{
    cin >> n >> m;
    scanf("%s", s1); scanf("%s", s2);
    lim = 1;
    while (lim <= (n + m))lim <<= 1;
    reverse(s1, s1 + n);
    for (int i = 0; i < n; ++i)a[i] = (s1[i] == '*') ? 0 : s1[i] - 'a' + 1;
    for (int i = 0; i < m; ++i)b[i] = (s2[i] == '*') ? 0 : s2[i] - 'a' + 1;

    for (int i = 0; i < lim; ++i)A[i] = (xu) {a[i]*a[i]*a[i], 0}, B[i] = (xu) {b[i], 0};
    FFT(A, lim, 1); FFT(B, lim, 1);
    for (int i = 0; i < lim; ++i)ans[i] = ans[i] + A[i] * B[i];

    for (int i = 0; i < lim; ++i)A[i] = (xu) {a[i], 0}, B[i] = (xu) {b[i]*b[i]*b[i], 0};
    FFT(A, lim, 1); FFT(B, lim, 1);
    for (int i = 0; i < lim; ++i)ans[i] = ans[i] + A[i] * B[i];

    for (int i = 0; i < lim; ++i)A[i] = (xu) {a[i]*a[i], 0}, B[i] = (xu) {b[i]*b[i], 0};
    FFT(A, lim, 1); FFT(B, lim, 1);
    for (int i = 0; i < lim; ++i)ans[i] = ans[i] - A[i] * B[i] * (xu) {2, 0};
    FFT(ans, lim, -1);

    for (int i = n - 1; i < m; ++i)
        if (!(int)(ans[i].x + 0.5))cnt[++cnt[0]] = i - n + 2;
    cout << cnt[0] << endl;
    for (int i = 1; i <= cnt[0]; ++i)printf("%d ", cnt[i]);
    return 0;
}

Guess you like

Origin www.cnblogs.com/wljss/p/12020196.html
FFT