BZOJ 4259: 残缺的字符串 [FFT]
4259: 残缺的字符串
题意:s,t,星号任意字符,匹配方案数
和上题一样
多乘上一个\(a_{j+i}\)就行了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const int N=(1<<20)+5;
const double PI=acos(-1);
inline int read(){
char c=getchar();int x=0,f=1;
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
return x*f;
}
struct meow{
double x, y;
meow(double a=0, double b=0):x(a), y(b){}
};
meow operator +(meow a, meow b) {return meow(a.x+b.x, a.y+b.y);}
meow operator -(meow a, meow b) {return meow(a.x-b.x, a.y-b.y);}
meow operator *(meow a, meow b) {return meow(a.x*b.x-a.y*b.y, a.x*b.y+a.y*b.x);}
meow conj(meow a) {return meow(a.x, -a.y);}
typedef meow cd;
namespace FFT{
int n, rev[N];
void ini(int lim) {
n=1; int k=0;
while(n<lim) n<<=1, k++;
for(int i=0; i<n; i++) rev[i] = (rev[i>>1]>>1) | ((i&1)<<(k-1));
}
void dft(cd *a, int flag) {
for(int i=0; i<n; i++) if(i<rev[i]) swap(a[i], a[rev[i]]);
for(int l=2; l<=n; l<<=1) {
int m=l>>1;
cd wn = meow(cos(2*PI/l), flag*sin(2*PI/l));
for(cd *p=a; p!=a+n; p+=l) {
cd w(1, 0);
for(int k=0; k<m; k++) {
cd t = w*p[k+m];
p[k+m] = p[k] - t;
p[k] = p[k] + t;
w=w*wn;
}
}
}
if(flag==-1) for(int i=0; i<n; i++) a[i].x/=n;
}
}using FFT::dft; using FFT::ini;
int n, m, lim;
cd a[N], b[N], a2[N], b2[N], a3[N], b3[N], c[N];
char s[N], t[N];
int ans, li[N];
int main() {
freopen("in","r",stdin);
m=read(); n=read(); lim=n+m-1; ini(lim);
scanf("%s%s",t,s);
for(int i=0; i<n; i++) s[i]= s[i]=='*' ? 0 : s[i]-'a'+1;
for(int i=0; i<m; i++) t[i]= t[i]=='*' ? 0 : t[i]-'a'+1;
for(int i=0; i<n; i++) a[i].x = s[i], a2[i].x = 2*s[i]*s[i], a3[i].x = s[i]*s[i]*s[i];
for(int i=0; i<m; i++) b[m-1-i].x = t[i], b2[m-1-i].x = t[i]*t[i], b3[m-1-i].x = t[i]*t[i]*t[i];
dft(a, 1); dft(a2, 1); dft(a3, 1); dft(b, 1); dft(b2, 1); dft(b3, 1);
for(int i=0; i<FFT::n; i++) c[i] = a3[i]*b[i] - a2[i]*b2[i] + a[i]*b3[i];
dft(c, -1);
for(int i=0; i<=n-m; i++) if(floor(c[m-1+i].x+0.5)==0) li[++ans]=i;
printf("%d\n",ans);
for(int i=1; i<=ans; i++) printf("%d ",li[i]+1);
}
BZOJ 4259: 残缺的字符串 [FFT]的更多相关文章
- BZOJ 4259 残缺的字符串 ——FFT
[题目分析] 同bzoj4503. 只是精度比较卡,需要试一试才能行O(∩_∩)O 用过long double,也加过0.4.最后发现判断的时候改成0.4就可以了 [代码] #include < ...
- BZOJ 4259 残缺的字符串(FFT)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4259 [题目大意] 给出两个包含*和小写字母的字符串,*为适配符,可以和任何字符匹配, ...
- 【BZOJ】4259: 残缺的字符串 FFT
[题意]给定长度为m的匹配串B和长度为n的模板串A,求B在A中出现多少次.字符串仅由小写字母和通配符" * "组成,其中通配符可以充当任意一个字符.n<=3*10^5. [算 ...
- 【刷题】BZOJ 4259 残缺的字符串
Description 很久很久以前,在你刚刚学习字符串匹配的时候,有两个仅包含小写字母的字符串A和B,其中A串长度为m,B串长度为n.可当你现在再次碰到这两个串时,这两个串已经老化了,每个串都有不同 ...
- BZOJ 4259: 残缺的字符串 FFT_多项式
Code: #include<bits/stdc++.h> #define maxn 1200000 using namespace std; void setIO(string s) { ...
- BZOJ 4259 残缺的字符串
思路 同样是FFT进行字符串匹配 只不过两个都有通配符 匹配函数再乘一个\(A_i\)即可 代码 #include <cstdio> #include <algorithm> ...
- luoguP4173 残缺的字符串 FFT
luoguP4173 残缺的字符串 FFT 链接 luogu 思路 和昨天做的题几乎一样. 匹配等价于(其实我更喜欢fft从0开始) \(\sum\limits_{i=0}^{m-1}(S[i+j]- ...
- Luogu P4173 残缺的字符串-FFT在字符串匹配中的应用
P4173 残缺的字符串 FFT在字符串匹配中的应用. 能解决大概这种问题: 给定长度为\(m\)的A串,长度为\(n\)的B串.问A串在B串中的匹配数 我们设一个函数(下标从\(0\)开始) \(C ...
- P4173 残缺的字符串(FFT字符串匹配)
P4173 残缺的字符串(FFT字符串匹配) P4173 解题思路: 经典套路将模式串翻转,将*设为0,设以目标串的x位置匹配结束的匹配函数为\(P(x)=\sum^{m-1}_{i=0}[A(m-1 ...
随机推荐
- Number Sequence(快速幂矩阵)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1005 Number Sequence Time Limit: 2000/1000 MS (Java/O ...
- JSON对象添加删除属性
假如目前我们有如下一个Json对象 var jsonObj={ 'param1':22, 'param2' :33 }; 增加属性: 我们现在向该对象jsonObj中添加一个新的属性字段:param3 ...
- c++extern关键字详解
1 基本解释 :extern可以置于变量或者函数 前,以标示变量或者函数的定义在别的文件中 ,提示编译器遇到此变量和函数时在其他模块中寻找其定义 .此外extern也可用来进行链接指定. 也就是说ex ...
- linux(CENTOS)系统各个目录的作用详解
Linux(CentOS)系统各个目录的作用详解 文件的类型 LINUX有四种基本文件系统类型:普通文件.目录文件.连接文件和特殊文件,可用file命令来识别. 普通文件:如文本文件.C语言元代码.S ...
- UEP-时间
时间戳转化为Date(or String) SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ...
- C++异常层次结构
#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; class MyArray { publi ...
- Linux命令之远程下载命令:wget
转自:http://www.cnblogs.com/peida/archive/2013/03/18/2965369.html Linux系统中的wget是一个下载文件的工具,它用在命令行下.对于Li ...
- ADO.NET复习总结(4)--访问SqlServer的类
1.连接SqlConnection 2. 3.执行命令SqlCommand 4.数据读取SqlDataReader 注意: ExecuteNonQuery() :执行非查询(增删改) Execute ...
- 用IDEA/WebStrom 提交本地项目到Git/码云等
以码云为例: 在码云上创建新的项目. webstrom/idea 打开本地项目
- Springmvc 并发访问的线程安全性问题
首先对于spring的IOC来说,对象是由Spring来帮我们管理,也就是在Spring启动的时候,在Spring容器中,由Spring给我们创建的,Spring会帮我们维护,一般都是单例的,也就是一 ...