Description

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

你想对这两个串重新进行匹配,其中A为模板串,那么现在问题来了,请回答,对于B的每一个位置i,从这个位置开始连续m个字符形成的子串是否可能与A串完全匹配?

Input

第一行包含两个正整数m,n(1<=m<=n<=300000),分别表示A串和B串的长度。

第二行为一个长度为m的字符串A。

第三行为一个长度为n的字符串B。

两个串均仅由小写字母和号组成,其中号表示相应位置已经残缺。

Output

第一行包含一个整数k,表示B串中可以完全匹配A串的位置个数。

若k>0,则第二行输出k个正整数,从小到大依次输出每个可以匹配的开头位置(下标从1开始)。

Sample Input

3 7

a*b

aebr*ob

Sample Output

2

1 5

Solution

这就是BZOJ 4503 两个串的升级版

思路可以看那篇

这题的式子相较就只改变成 \(f(i)=\sum_{j=0}^ix_{i-j}^3y_j-2x_{i-j}^2y_j^2+x_{i-j}y_j^3\)

变成了3个卷积

一样用FFT求解就好了

#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
const int MAXN=1<<20;
const db Pi=acos(-1.0);
int n1,n2,n,m,cnt,rev[MAXN],ans[MAXN],nt;
char s1[MAXN],s2[MAXN];
struct Complex{
db real,imag;
inline Complex operator + (const Complex &A) const {
return (Complex){real+A.real,imag+A.imag};
};
inline Complex operator - (const Complex &A) const {
return (Complex){real-A.real,imag-A.imag};
};
inline Complex operator * (const Complex &A) const {
return (Complex){real*A.real-imag*A.imag,imag*A.real+real*A.imag};
};
};
Complex x[MAXN],x2[MAXN],x3[MAXN],y[MAXN],y2[MAXN],y3[MAXN];
template<typename T> inline void read(T &x)
{
T data=0,w=1;
char ch=0;
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')w=-1,ch=getchar();
while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
x=data*w;
}
template<typename T> inline void write(T x,char ch='\0')
{
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
if(ch!='\0')putchar(ch);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
inline void FFT(Complex *A,int tp)
{
for(register int i=0;i<n;++i)
if(i<rev[i])std::swap(A[i],A[rev[i]]);
for(register int l=2;l<=n;l<<=1)
{
Complex wn=(Complex){cos(2*Pi/l),sin(tp*2*Pi/l)};
for(register int i=0;i<n;i+=l)
{
Complex w=(Complex){1,0};
for(register int j=0;j<(l>>1);++j)
{
Complex A1=A[i+j],A2=w*A[i+j+(l>>1)];
A[i+j]=A1+A2,A[i+j+(l>>1)]=A1-A2;
w=w*wn;
}
}
}
}
int main()
{
read(n1);read(n2);
m=n1+n2-1;
scanf("%s",s1);scanf("%s",s2);
for(register int i=0;i<n1;++i)
{
x[n1-i-1].real=(s1[i]=='*'?0:s1[i]-'a'+1);
x2[n1-i-1].real=x[n1-i-1].real*x[n1-i-1].real*2;
x3[n1-i-1].real=x[n1-i-1].real*x[n1-i-1].real*x[n1-i-1].real;
}
for(register int i=0;i<n2;++i)
{
y[i].real=(s2[i]=='*'?0:s2[i]-'a'+1);
y2[i].real=y[i].real*y[i].real;
y3[i].real=y[i].real*y[i].real*y[i].real;
}
for(n=1;n<m;n<<=1)++cnt;
for(register int i=0;i<n;++i)rev[i]=(rev[i>>1]>>1)|((i&1)<<(cnt-1));
FFT(x,1);FFT(x2,1);FFT(x3,1);
FFT(y,1);FFT(y2,1);FFT(y3,1);
for(register int i=0;i<n;++i)x[i]=x3[i]*y[i]-x2[i]*y2[i]+x[i]*y3[i];
FFT(x,-1);
for(register int i=0;i<n2-n1+1;++i)
if((int)(x[n1+i-1].real/n+0.5)==0)ans[++nt]=i;
write(nt,'\n');
for(register int i=1;i<=nt;++i)write(ans[i]+1,' ');
puts("");
return 0;
}

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

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

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

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

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

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

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

  4. BZOJ 4259 残缺的字符串

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

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

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

  6. leecode刷题(11)-- 反转字符串

    leecode刷题(11)-- 反转字符串 反转字符串 描述: 编写一个函数,其作用是将输入的字符串反转过来. 示例 1: 输入: "hello" 输出: "olleh& ...

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

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

  8. bzoj 4259 4259: 残缺的字符串【FFT】

    和bzoj 4503 https://www.cnblogs.com/lokiii/p/10032311.html 差不多,就是再乘上一个原串字符 有点卡常,先在点值下算最后一起IDFT #inclu ...

  9. 刷题-力扣-541. 反转字符串 II

    541. 反转字符串 II 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/reverse-string-ii 著作权归领扣网络所有. ...

随机推荐

  1. docker社区的geodata/gdal镜像dockerfile分析

    对应从事遥感与地理信息的同仁来说,gdal应该是所有工具中使用频度最高的库了,那么在docker中使用gdal时,面临的第一步就是构建gdal基础镜像,社区中引用最多的就是geodata提供的gdal ...

  2. 软件测试工程师必备的SQL语句基础

    为一个软件测试工程师,我们在测试过程中往往需要对数据库数据进行操作,但是我们的操作大多以查询居多,有时会涉及到新增,修改,删除等操作,所以我们其实并不需要对数据库的操作有特别深入的了解,以下是我在工作 ...

  3. git基础(2)

    三.查看提交历史日志查看·提交历史:git log 命令一个常用的选项是 -p,用来显示每次提交的内容差异. 你也可以加上 -2 来仅显示最近两次提交如果你想看到每次提交的简略的统计信息,你可以使用 ...

  4. 82. Single Number [easy]

    Description Given 2*n + 1 numbers, every numbers occurs twice except one, find it. Example Given [1, ...

  5. LeetCode - 463. Island Perimeter - O(MN)- (C++) - 解题报告

    原题 原题链接 You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 ...

  6. 2.azkaban3.0安装

    安装规划安装azkban1.安装配置数据库2.下载安装web server3.安装mulit executor4.安装azkaban插件AZKABAN参数安装出现的问题 安装规划 IP 角色 端口 1 ...

  7. 【转】cpu的核心数与线程数的关系

    原文地址:http://www.dn580.com/dnzs/dncs/2013/10/08/172948914.html 我们在选购电脑的时候,CPU是一个需要考虑到核心因素,因为它决定了电脑的性能 ...

  8. Python中函数的参数-arguments

    归纳起来,Python中函数的定义形式和调用形式主要有如下几种形式: # 函数的定义形式 def func(name) # 匹配positional参数或者keyword参数 def func(nam ...

  9. java中对象和对象的引用

    1.何谓对象? 在Java中有一句比较流行的话,叫做“万物皆对象”,这是Java语言设计之初的理念之一.要理解什么是对象,需要跟类一起结合起来理解.下面这段话引自<Java编程思想>中的一 ...

  10. xml解析----java中4中xml解析方法(转载)

    转载:https://www.cnblogs.com/longqingyang/p/5577937.html 描述 XML是一种通用的数据交换格式,它的平台无关性.语言无关性.系统无关性.给数据集成与 ...