传送门


考虑如何使用FFT计算两个子串是否匹配。如果字符集比较小可以把每个字符都拿出来暴力做一遍,但是字符集比较大的时候复杂度就会有问题。这个时候可以考虑匹配函数。

先考虑没有通配符的情况。将\(A\)串翻转,然后设匹配函数\(chk(i,j) = (A_i - B_j)^2\)。不难知道\(A_i = B_j \Leftrightarrow chk(i,j) = 0\)。

又设\(C(x) = \sum\limits_{i=1}^m chk(m + 1 - i , x + i - 1)\),那么\(B\)的以\(x\)为左端点的长度为\(m\)的子串能够跟\(A\)串匹配的充要条件就是\(C(x) = 0\)。

而\(C(x) = \sum\limits_{i=1}^m (A_{m+1-i} - B_{x+i-1})^2 = \sum\limits_{i=1}^m (A_{m+1-i}^2 + B_{x+i-1}^2) - 2\sum\limits_{i=1}^m A_{m+1-i}B_{x+i-1}\)。可以发现最后的式子是一个卷积的形式,而前面两项都可以前缀和。于是直接FFT算一下最后的卷积就可以\(O(NlogN)\)判断。

现在考虑通配符的情况,通配符可以与任意字符匹配,所以不妨设\(chk(i,j) = (A_i - B_j)^2 A_i B_j\),其中如果某一个字符为通配符就令它的值为\(0\),这样一个通配符匹配的贡献就永远都是\(0\)了。而

\[C(x) = \sum\limits_{i=1}^m chk(m + 1 - i , x+i-1) = \sum\limits_{i=1}^m (A_{m+1-i} - B_{x+i-1})^2 A_{m+1-i} B_{x+i-1} = \sum\limits_{i=1}^m A_{m+1-i}^3 B_{x+i-1} + \sum\limits_{i=1}^m A_{m+1-i}B_{x+i-1}^3 - 2\sum\limits_{i=1}^m A_{m+1-i}^2B_{x+i-1}^2\]

三个都用FFT算一遍就行了

#include<iostream>
#include<cstdio>
#include<cctype>
#include<algorithm>
#include<cstring>
#include<cmath>
//This code is written by Itst
using namespace std;

#define ld double
const int MAXN = (1 << 20) + 7;
struct comp{
    ld x , y;
    comp(ld _x = 0 , ld _y = 0) : x(_x) , y(_y){}
    comp operator +(comp a){return comp(x + a.x , y + a.y);}
    comp operator -(comp a){return comp(x - a.x , y - a.y);}
    comp operator *(comp a){return comp(x * a.x - y * a.y , x * a.y + y * a.x);}
}A[MAXN] , B[MAXN] , C[MAXN] , D[MAXN];
ld ans[MAXN];
const ld pi = acos(-1);
int M , N , need , dir[MAXN];
char s[MAXN];

inline void init(int len){
    need = 1;
    while(need < len)
        need <<= 1;
    for(int i = 1 ; i < need ; ++i)
        dir[i] = (dir[i >> 1] >> 1) | (i & 1 ? need >> 1 : 0);
}

void FFT(comp *arr , int type){
    for(int i = 1 ; i < need ; ++i)
        if(i < dir[i])
            swap(arr[i] , arr[dir[i]]);
    for(int i = 1 ; i < need ; i <<= 1){
        comp wn(cos(pi / i) , type * sin(pi / i));
        for(int j = 0 ; j < need ; j += i << 1){
            comp w(1 , 0);
            for(int k = 0 ; k < i ; ++k , w = w * wn){
                comp x = arr[j + k] , y = arr[i + j + k] * w;
                arr[j + k] = x + y; arr[i + j + k] = x - y;
            }
        }
    }
}

void work(){
    for(int i = 0 ; i < need ; ++i)
        C[i] = comp(A[i].x * A[i].x * A[i].x , 0);
    for(int i = 0 ; i < need ; ++i) D[i] = B[i];
    FFT(C , 1); FFT(D , 1);
    for(int i = 0 ; i < need ; ++i)
        C[i] = C[i] * D[i];
    FFT(C , -1);
    for(int i = M + 1 ; i <= N + 1 ; ++i)
        ans[i] += C[i].x / need;

    for(int i = 0 ; i < need ; ++i) C[i] = A[i];
    for(int i = 0 ; i < need ; ++i)
        D[i] = comp(B[i].x * B[i].x * B[i].x , 0);
    FFT(C , 1); FFT(D , 1);
    for(int i = 0 ; i < need ; ++i)
        C[i] = C[i] * D[i];
    FFT(C , -1);
    for(int i = M + 1 ; i <= N + 1 ; ++i)
        ans[i] += C[i].x / need;

    for(int i = 0 ; i < need ; ++i)
        C[i] = comp(A[i].x * A[i].x , 0);
    for(int i = 0 ; i < need ; ++i)
        D[i] = comp(B[i].x * B[i].x , 0);
    FFT(C , 1); FFT(D , 1);
    for(int i = 0 ; i < need ; ++i)
        C[i] = C[i] * D[i];
    FFT(C , -1);
    for(int i = M + 1 ; i <= N + 1 ; ++i)
        ans[i] -= 2 * C[i].x / need;
}

int main(){
#ifndef ONLINE_JUDGE
    freopen("in","r",stdin);
    //freopen("out","w",stdout);
#endif
    scanf("%d %d" , &M , &N);
    scanf("%s" , s + 1);
    reverse(s + 1 , s + M + 1);
    for(int i = 1 ; i <= M ; ++i)
        A[i].x = s[i] == '*' ? 0 : s[i] - 'a' + 1;
    scanf("%s" , s + 1);
    for(int i = 1 ; i <= N ; ++i)
        B[i].x = s[i] == '*' ? 0 : s[i] - 'a' + 1;
    init(M + N + 1);
    work();
    int cnt = 0;
    for(int i = M + 1 ; i <= N + 1 ; ++i)
        cnt += ans[i] <= 0.5;
    cout << cnt << endl;
    for(int i = M + 1 ; i <= N + 1 ; ++i)
        if(ans[i] <= 0.5)
            cout << i - M << ' ';
    return 0;
}

Luogu4173 残缺的字符串 FFT的更多相关文章

  1. luoguP4173 残缺的字符串 FFT

    luoguP4173 残缺的字符串 FFT 链接 luogu 思路 和昨天做的题几乎一样. 匹配等价于(其实我更喜欢fft从0开始) \(\sum\limits_{i=0}^{m-1}(S[i+j]- ...

  2. Luogu P4173 残缺的字符串-FFT在字符串匹配中的应用

    P4173 残缺的字符串 FFT在字符串匹配中的应用. 能解决大概这种问题: 给定长度为\(m\)的A串,长度为\(n\)的B串.问A串在B串中的匹配数 我们设一个函数(下标从\(0\)开始) \(C ...

  3. P4173 残缺的字符串(FFT字符串匹配)

    P4173 残缺的字符串(FFT字符串匹配) P4173 解题思路: 经典套路将模式串翻转,将*设为0,设以目标串的x位置匹配结束的匹配函数为\(P(x)=\sum^{m-1}_{i=0}[A(m-1 ...

  4. BZOJ 4259: 残缺的字符串 [FFT]

    4259: 残缺的字符串 题意:s,t,星号任意字符,匹配方案数 和上题一样 多乘上一个\(a_{j+i}\)就行了 #include <iostream> #include <cs ...

  5. 【BZOJ4259】残缺的字符串 FFT

    [BZOJ4259]残缺的字符串 Description 很久很久以前,在你刚刚学习字符串匹配的时候,有两个仅包含小写字母的字符串A和B,其中A串长度为m,B串长度为n.可当你现在再次碰到这两个串时, ...

  6. 洛谷 P4173 残缺的字符串 (FFT)

    题目链接:P4173 残缺的字符串 题意 给定长度为 \(m\) 的模式串和长度为 \(n\) 的目标串,两个串都带有通配符,求所有匹配的位置. 思路 FFT 带有通配符的字符串匹配问题. 设模式串为 ...

  7. BZOJ4259:残缺的字符串(FFT)

    Description 很久很久以前,在你刚刚学习字符串匹配的时候,有两个仅包含小写字母的字符串A和B,其中A串长度为m,B串长度为n.可当你现在再次碰到这两个串时,这两个串已经老化了,每个串都有不同 ...

  8. P4173 残缺的字符串 fft

    题意:给你两个字符串,问你第一个在第二个中出现过多少次,并输出位置,匹配时是模糊匹配*可和任意一个字符匹配 题解:fft加速字符串匹配; 假设上面的串是s,s长度为m,下面的串是p,p长度为n,先考虑 ...

  9. 【BZOJ】4259: 残缺的字符串 FFT

    [题意]给定长度为m的匹配串B和长度为n的模板串A,求B在A中出现多少次.字符串仅由小写字母和通配符" * "组成,其中通配符可以充当任意一个字符.n<=3*10^5. [算 ...

随机推荐

  1. 【代码笔记】Web-HTML-列表

    一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  2. python之约束, 异常处理, md5

    1. 类的约束 1. 写一个父类. 父类中的某个方法要抛出一个异常 NotImplementedError (重点) 2. 抽象类和抽象方法 # 语法 # from abc import ABCMet ...

  3. 【转】pam_mysql - MySQL error (Can't connect to local MySQL server through socket

    转自:http://350201.blog.51cto.com/340201/1034672 参照 http://wjw7702.blog.51cto.com/5210820/936244博 主做的p ...

  4. 2018最新大厂Android面试真题

    前言 又到了金三银四的面试季,自己也不得不参与到这场战役中来,其实是从去年底就开始看,android的好机会确实不太多,但也还好,3年+的android开发经历还是有一些面试机会的,不过确实不像几年前 ...

  5. 模拟开户接口,使用shell脚本实现批量用户开通

    1.目的 通过模拟接口方法,实现批量用户开通 2.分析 A.接口含body和head部分,其中body中的某些变量为必填字段,包含用户的信息,接口可整理成body.xml.head.xml文件. B. ...

  6. MVC| 路由测试代码

    using System; using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; using System.Web; using ...

  7. mysqlreport工具

    进行MySQL的配置优化,首先必须找出MySQL的性能瓶颈所在:而SHOW STATUS输出的报告正是用来计算性能瓶颈的参考数据.mysqlreport不像SHOW STATUS那样简单的罗列数据,而 ...

  8. 最大公约数&&最小公倍数

    //最大公约数(greatest common divisor),运用递归 int gcd(int a,int b){//注意a要求大于b return !b?a:gcd(b,a%b); } //最小 ...

  9. fedora添加ntfs文件系统支持

    ntfs支持(安装后不能打开,重启) 如果没有换源先看一下换源. 查找库中是否有ntfs-3g. [root@bogon zhujikuan]# yum search ntfs 上次元数据过期检查:0 ...

  10. Alpha冲刺! Day6 - 砍柴

    Alpha冲刺! Day6 - 砍柴 今日已完成 晨瑶:讨论确定/解决了:网络判断使用广播方式.密集光点排布问题.丢失.db/记录html/多媒体文件的处理方式. 昭锡:Android工具包接口文档编 ...