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

【算法】FFT

【题解】假设模板串的数组A用0~26代表所有字符,0为通配符,匹配串的数组B同理,那么用表示差异的经典套路:

$$C_n=\sum_{i=0}^{m-1}(A_{n+i}-B_i)^2*A_{n+i}*B_i$$

那么可以看出$C_n=0$当且仅当$S_A[n,n+m-1]=S_B[0,m-1]$。这里的通配符为0,所以当含有通配符时式子直接为0,即无差异。

将数组A反转,得到:

$$C_x=\sum_{i=0}^{m-1}(A'_{n-x-i-1}-B_i)^2*A'_{n-x-i-1}*B_i$$

尝试扩展上届:

$$C_x=D_{n-x-1}=\sum_{i=0}^{n-x-1}(A'_{n-x-i-1}-B_i)^2*A'_{n-x-i-1}*B_i$$

现在只需要计算Dx了,二项式拆分得到:

$$D_x=\sum_{i=0}^{x}A_{x-i}^3B_i-2A_{x-i}^2B_i^2+A_{x-i}B_i^3$$

(这里公式不知道A后为什么不能加单引号,不然latex会出错)

复杂度O(n log n)。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=,N=;
const double PI=acos(-);
int m,n,aa[N],bb[N],d[N];
char A[N],B[N];
struct cp{
double x,y;
cp(double a,double b){x=a;y=b;}
cp(){x=y=;}
cp operator + (cp a){return cp(x+a.x,y+a.y);}
cp operator - (cp a){return cp(x-a.x,y-a.y);}
cp operator * (cp a){return cp(x*a.x-y*a.y,x*a.y+y*a.x);}
}A1[maxn],A2[maxn],A3[maxn],B1[maxn],B2[maxn],B3[maxn];
void fft(cp *a,int n,int f){
int k=;
for(int i=;i<n;i++){
if(i<k)swap(a[i],a[k]);
for(int j=n>>;(k^=j)<j;j>>=);
}
for(int l=;l<=n;l<<=){
int m=l/;
cp wn(cos(*PI*f/l),sin(*PI*f/l));
for(cp *p=a;p!=a+n;p+=l){
cp w(,);
for(int i=;i<l/;i++){
cp t=w*p[i+m];
p[i+m]=p[i]-t;
p[i]=p[i]+t;
w=w*wn;
}
}
}
if(f==-)for(int i=;i<n;i++)a[i].x/=n;
}
int main(){
scanf("%d%d%s%s",&m,&n,B,A);
for(int i=;i<n;i++)aa[n-i-]=(A[i]=='*'?:A[i]-'a'+);
for(int i=;i<m;i++)bb[i]=(B[i]=='*'?:B[i]-'a'+);
for(int i=;i<n;i++)A1[i]=cp(aa[i],),A2[i]=cp(aa[i]*aa[i],),A3[i]=cp(aa[i]*aa[i]*aa[i],);
for(int i=;i<m;i++)B1[i]=cp(bb[i],),B2[i]=cp(bb[i]*bb[i],),B3[i]=cp(bb[i]*bb[i]*bb[i],);
int N=;while(N<n+m)N<<=;
fft(A1,N,);fft(B1,N,);
fft(A2,N,);fft(B2,N,);
fft(A3,N,);fft(B3,N,);
for(int i=;i<N;i++)A1[i]=A3[i]*B1[i]-cp(,)*A2[i]*B2[i]+A1[i]*B3[i];
fft(A1,N,-);
int cnt=;
for(int i=;i<=n-m;i++)if(!((int)(A1[n--i].x+0.1)))d[++cnt]=i+;
printf("%d\n",cnt);
for(int i=;i<=cnt;i++)printf("%d ",d[i]);
return ;
}

【BZOJ】4259: 残缺的字符串 FFT的更多相关文章

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

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

  2. BZOJ 4259 残缺的字符串 ——FFT

    [题目分析] 同bzoj4503. 只是精度比较卡,需要试一试才能行O(∩_∩)O 用过long double,也加过0.4.最后发现判断的时候改成0.4就可以了 [代码] #include < ...

  3. BZOJ 4259 残缺的字符串(FFT)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4259 [题目大意] 给出两个包含*和小写字母的字符串,*为适配符,可以和任何字符匹配, ...

  4. 【刷题】BZOJ 4259 残缺的字符串

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

  5. BZOJ 4259: 残缺的字符串 FFT_多项式

    Code: #include<bits/stdc++.h> #define maxn 1200000 using namespace std; void setIO(string s) { ...

  6. BZOJ 4259 残缺的字符串

    思路 同样是FFT进行字符串匹配 只不过两个都有通配符 匹配函数再乘一个\(A_i\)即可 代码 #include <cstdio> #include <algorithm> ...

  7. luoguP4173 残缺的字符串 FFT

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

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

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

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

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

随机推荐

  1. 蜗牛慢慢爬 LeetCode 7. Reverse Integer [Difficulty: Easy]

    题目 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have ...

  2. 移动web适配利器-rem

    移动web适配利器-rem 前言 提到rem,大家首先会想到的是em,px,pt这类的词语,大多数人眼中这些单位是用于设置字体的大小的,没错这的确是用来设置字体大小的,但是对于rem来说它可以用来做移 ...

  3. Windows 作为 openssl server端时的处理

    1. 跟上一个博客一样, 下载openssh 然后安装时 同时选择 server端. 2. 安装时设置密码 其他默认即可 3. xshell 创建连接. 注意 我使用的是 administrator ...

  4. lucene介绍

    1.https://blog.csdn.net/shuaicihai/article/details/65111523 2.https://www.cnblogs.com/rodge-run/p/65 ...

  5. 【大数据】Spark基础解析

    第1章 Spark概述 1.1 什么是Spark 1.2 Spark内置模块 Spark Core:实现了Spark的基本功能,包含任务调度.内存管理.错误恢复.与存储系统交互等模块.Spark Co ...

  6. 【大数据】关于Kafka的进一步理解

    前置: 文件host 192.168.11.13 192.168.11.14 192.168.11.30 脚本init_kafka.sh #!/bin/bash source /etc/profile ...

  7. Concise and clear CodeForces - 991F(dfs 有重复元素的全排列)

    就是有重复元素的全排列 #include <bits/stdc++.h> #define mem(a, b) memset(a, b, sizeof(a)) using namespace ...

  8. Musical Theme POJ - 1743(后缀数组+二分)

    求不可重叠最长重复子串 对于height[i]定义为sa[i]和 sa[i-1]的最长公共前缀 这个最长公共前缀的值肯定是最大的 证明: 设rank[j] < rank[k], 则不难证明后缀j ...

  9. JUnit中按照顺序执行测试方式

    很多情况下,写了一堆的test case,希望某一些test case必须在某个test case之后执行.比如,测试某一个Dao代码,希望添加的case在最前面,然后是修改或者查询,最后才是删除,以 ...

  10. 【poj3294】 Life Forms

    http://poj.org/problem?id=3294 (题目链接) 题意 给定 n 个字符串,求出现在不小于 k 个字符串中的最长子串. Solution 后缀数组论文题.. 将 n 个字符串 ...