题意:给你两个字符串,问你第一个在第二个中出现过多少次,并输出位置,匹配时是模糊匹配*可和任意一个字符匹配

题解:fft加速字符串匹配;

假设上面的串是s,s长度为m,下面的串是p,p长度为n,先考虑没有*的情况那么\(\sum_{j=1}^m(s_{i+j}-p_j)^2=0\)就表示能够从i开始匹配,现在考虑有*的情况,我们只需要让有*的和任意字符匹配即可,那么把公式变成\(\sum_{j=1}^m(s_{i+j}-p_j)^2*s_{i+j}*p_j)=0\),但是fft正向匹配太慢了,我们把方向变一下,\(\sum_{j=1}^m(s_{i+j}-p_{n-j})^2*s_{i+j}*p_{n-j}=0\),把式子分解一下得\(\sum_{j=1}^m(s_{i+j}^3*p_{n-j}+s_{i+j}*p_{n-j}^3-2*s_{i+j}^2*p_{n-j}^2)=0\),这样把这6个多项式跑一边fft,就得到了答案,

//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000007
#define ld long double
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
//#define cd complex<double>
#define ull unsigned long long
#define base 1000000000000000000
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define fio ios::sync_with_stdio(false);cin.tie(0)
template<typename T>
inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>
inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;} using namespace std; const double eps=1e-8;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=300000+10,maxn=50000+10,inf=0x3f3f3f3f; struct cd{
db x,y;
cd(db _x=0.0,db _y=0.0):x(_x),y(_y){}
cd operator +(const cd &b)const{
return cd(x+b.x,y+b.y);
}
cd operator -(const cd &b)const{
return cd(x-b.x,y-b.y);
}
cd operator *(const cd &b)const{
return cd(x*b.x - y*b.y,x*b.y + y*b.x);
}
cd operator /(const db &b)const{
return cd(x/b,y/b);
}
}a[N<<3],b[N<<3],c[N<<3],d[N<<3],e[N<<3],f[N<<3];
int rev[N<<3];
void getrev(int bit)
{
for(int i=0;i<(1<<bit);i++)
rev[i]=(rev[i>>1]>>1) | ((i&1)<<(bit-1));
}
void fft(cd *a,int n,int dft)
{
for(int i=0;i<n;i++)
if(i<rev[i])
swap(a[i],a[rev[i]]);
for(int step=1;step<n;step<<=1)
{
cd wn(cos(dft*pi/step),sin(dft*pi/step));
for(int j=0;j<n;j+=step<<1)
{
cd wnk(1,0);
for(int k=j;k<j+step;k++)
{
cd x=a[k];
cd y=wnk*a[k+step];
a[k]=x+y;a[k+step]=x-y;
wnk=wnk*wn;
}
}
}
if(dft==-1)for(int i=0;i<n;i++)a[i]=a[i]/n;
}
char s[N],p[N];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
scanf("%s%s",s+1,p+1);
int sz=0;
while((1<<sz)<m)sz++;
sz++,getrev(sz);
for(int i=0;i<=(1<<sz);i++)
a[i]=b[i]=c[i]=d[i]=e[i]=f[i]=0;
for(int i=1,te;i<=n;i++)
{
if(s[i]=='*')te=0;
else te=s[i]-'a'+1;
a[m-i]=te*te*te,b[m-i]=-2*te*te,c[m-i]=te;
}
for(int i=1,te;i<=m;i++)
{
if(p[i]=='*')te=0;
else te=p[i]-'a'+1;
d[i]=te*te*te,e[i]=te*te,f[i]=te;
}
fft(a,(1<<sz),1),fft(b,(1<<sz),1),fft(c,(1<<sz),1);
fft(d,(1<<sz),1),fft(e,(1<<sz),1),fft(f,(1<<sz),1);
for(int i=0;i<=(1<<sz);i++)
d[i]=a[i]*f[i]+b[i]*e[i]+c[i]*d[i];
fft(d,(1<<sz),-1);
// for(int i=0;i<=m-n;i++)
// printf("%d ",(int)((d[m+i].x+0.5)/(1<<sz)));
// puts("");
vi ans;
for(int i=0;i<=m-n;i++)
if((int)(d[m+i].x+0.5/(1<<sz))==0)
ans.pb(i+1);
printf("%d\n",ans.size());
for(int i=0;i<ans.size();i++)
printf("%d ",ans[i]);
puts("");
return 0;
}
/******************** ********************/

P4173 残缺的字符串 fft的更多相关文章

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

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

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

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

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

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

  4. luoguP4173 残缺的字符串 FFT

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

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

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

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

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

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

    传送门 话说为什么字符串会和卷积扯上关系呢……到底得脑洞大到什么程度才能想到这种东西啊……大佬太珂怕了…… 因为通配符的关系,自动机已经废了 那么换种方式考虑,如果两个字符串每一位对应的编码都相等,那 ...

  8. [Luogu P4173]残缺的字符串 ( 数论 FFT)

    题面 传送门:洛咕 Solution 这题我写得脑壳疼,我好菜啊 好吧,我们来说正题. 这题.....emmmmmmm 显然KMP类的字符串神仙算法在这里没法用了. 那咋搞啊(或者说这题和数学有半毛钱 ...

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

    [Luogu4173] 题解 \(1.\)定义匹配函数 \(2.\)定义完全匹配函数 \(3.\)快速计算每一位的完全匹配函数值 #include<cstdio> #include< ...

随机推荐

  1. Linux基础命令---unzip

    unzip 解压zip指令压缩过的文件.unzip将列出.测试或从ZIP存档中提取文件,这些文件通常在MS-DOS系统中找到.默认行为(没有选项)是将指定ZIP存档中的所有文件提取到当前目录(及其下面 ...

  2. tmocat ssl 证书 生成 过程

    . "%JAVA_HOME%\bin\keytool" -genkey -alias tomcat -keyalg RSA -keystore D:\Youxun\keystore ...

  3. C/C++笔记 #035# Makefile

    相关资料: Understanding roles of CMake, make and GCC GCC and Make ( A simple tutorial, teaches u how to ...

  4. 09: TemplateView , ListView ,DetailView三种常用类视图用法

    1.1 视图混合介绍 1.Mixin和View的职能区分 1. Mixin提供数据,View提供模板和渲染,所以一般get_context_data在Mixin中,get(),post(),head( ...

  5. 20145334赵文豪 WEB基础实践

    实验问题回答 1.什么是表单 表单在网页中主要负责数据采集功能 一个表单有三个基本组成部分: 表单标签 表单域:包含了文本框.密码框.隐藏域.多行文本框.复选框.单选框.下拉选择框和文件上传框等 表单 ...

  6. 20145335郝昊《网络攻防》Exp7 网络欺诈技术防范

    20145335郝昊<网络攻防>Exp7 网络欺诈技术防范 实验内容 本次实践本实践的目标理解常用网络欺诈背后的原理,以提高防范意识,并提出具体防范方法. 简单应用SET工具建立冒名网站. ...

  7. 20145208 蔡野《网络对抗》shellcode注入&Return-to-libc攻击深入

    20145208 蔡野<网络对抗>shellcode注入&Return-to-libc攻击深入 Shellcode注入 shellcode的获取代码 我使用了许心远同学博客中的代码 ...

  8. C++ 表示一个区间值得方法

    C++中不允许这样的写法 85<= score <=100;你要想表示85<=score<=100的话只能这么写score>=85&&score<= ...

  9. 维特比算法Python实现

    前言 维特比算法是隐马尔科夫问题的一个基本问题算法.维特比算法解决的问题是已知观察序列,求最可能的标注序列. 什么是维特比算法? 维特比算法尽管是基于严格的数学模型的算法,但是维特比算法毕竟是算法,因 ...

  10. 试着用React写项目-利用react-router解决跳转路由等问题(二)

    转载请注明出处:王亟亟的大牛之路 这一篇还是继续写react router相关的内容,废话之前先安利:https://github.com/ddwhan0123/Useful-Open-Source- ...