there

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
using namespace std;
int n, m, aa[300005], bb[300005], lim=1, tmpcnt, rev[1100005];
const double PI=acos(-1.0);
char ss[300005];
vector<int> vec;
struct Complex{
double x, y;
Complex(double xx=0.0, double yy=0.0){
x = xx;
y = yy;
}
Complex operator+(const Complex &u)const{
return Complex(x+u.x, y+u.y);
}
Complex operator-(const Complex &u)const{
return Complex(x-u.x, y-u.y);
}
Complex operator*(const Complex &u)const{
return Complex(x*u.x-y*u.y, x*u.y+y*u.x);
}
Complex operator*(double u)const{
return Complex(x*u, y*u);
}
}a[1100005], b[1100005], c[1100005];
void fft(Complex a[], int opt){
for(int i=0; i<lim; i++)
if(i<rev[i])
swap(a[i], a[rev[i]]);
for(int i=2; i<=lim; i<<=1){
int tmp=i>>1;
Complex wn=Complex(cos(PI*2.0/i), opt*sin(PI*2.0/i));
for(int j=0; j<lim; j+=i){
Complex w=Complex(1.0, 0.0);
for(int k=0; k<tmp; k++){
Complex tmp1=a[j+k], tmp2=w*a[j+k+tmp];
a[j+k] = tmp1 + tmp2;
a[j+k+tmp] = tmp1 - tmp2;
w = w * wn;
}
}
}
if(opt==-1)
for(int i=0; i<lim; i++)
a[i].x /= lim;
}
int main(){
cin>>m>>n;
while(lim<=n+m) lim <<= 1, tmpcnt++;
for(int i=0; i<lim; i++)
rev[i] = (rev[i>>1]>>1) | ((i&1)<<(tmpcnt-1));
scanf("%s", ss);
for(int i=0; i<m; i++)
if(ss[i]!='*')
aa[i] = ss[i] - 'a' + 1;
scanf("%s", ss);
for(int i=0; i<n; i++)
if(ss[i]!='*')
bb[i] = ss[i] - 'a' + 1;
reverse(aa, aa+m);
for(int i=0; i<m; i++)
a[i] = Complex(aa[i]*aa[i]*aa[i], 0.0);
for(int i=0; i<n; i++)
b[i] = Complex(bb[i], 0.0);
fft(a, 1);
fft(b, 1);
for(int i=0; i<lim; i++)
c[i] = a[i] * b[i];
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
for(int i=0; i<m; i++)
a[i] = Complex(aa[i]*aa[i], 0.0);
for(int i=0; i<n; i++)
b[i] = Complex(bb[i]*bb[i], 0.0);
fft(a, 1);
fft(b, 1);
for(int i=0; i<lim; i++)
c[i] = c[i] - a[i] * b[i] * 2.0;
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
for(int i=0; i<m; i++)
a[i] = Complex(aa[i], 0.0);
for(int i=0; i<n; i++)
b[i] = Complex(bb[i]*bb[i]*bb[i], 0.0);
fft(a, 1);
fft(b, 1);
for(int i=0; i<lim; i++)
c[i] = c[i] + a[i] * b[i];
fft(c, -1);
for(int i=m-1; i<n; i++)
if(fabs(c[i].x)<1e-6)
vec.push_back(i-m+2);
cout<<vec.size()<<endl;
for(int i=0; i<vec.size(); i++)
printf("%d ", vec[i]);
printf("\n");
return 0;
}

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

  1. Luogu4173 残缺的字符串 FFT

    传送门 考虑如何使用FFT计算两个子串是否匹配.如果字符集比较小可以把每个字符都拿出来暴力做一遍,但是字符集比较大的时候复杂度就会有问题.这个时候可以考虑匹配函数. 先考虑没有通配符的情况.将\(A\ ...

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

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

  3. 【BZOJ4259】残缺的字符串

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

  4. 【BZOJ4259】残缺的字符串(FFT)

    [BZOJ4259]残缺的字符串(FFT) 题面 给定两个字符串\(|S|,|T|\),两个字符串中都带有通配符. 回答\(T\)在\(S\)中出现的次数. \(|T|,|S|<=300000\ ...

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

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

  6. CF528D Fuzzy Search 和 BZOJ4259 残缺的字符串

    Fuzzy Search 给你文本串 S 和模式串 T,求 S 的每个位置是否能模糊匹配上 T. 这里的模糊匹配指的是把 T 放到 S 相应位置上之后,T 中每个字符所在位置附近 k 个之内的位置上的 ...

  7. luoguP4173 残缺的字符串 FFT

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

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

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

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

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

随机推荐

  1. css:focus伪类的使用

    css中:focus伪类的使用,即给已获取焦点的元素设置样式 示例一 <!DOCTYPE html> <html lang="en"> <head&g ...

  2. python之元组,列表和字典的区别

    Python语言包含6种内建的序列,其中,有两种主要的类型:列表和元组. 列表是可以修改的,而元组不可以,如果要添加或者删除某些元素,就只能用列表,为了限制某些元素,就会用到元组.一般来说,列表可以替 ...

  3. C#注册表操作类(完整版)

    下面贴出自己用C#写的注册表操作类,欢迎大家拍砖! 1.注册表基项静态域 1 /// <summary> 2 /// 注册表基项静态域 3 /// 4 /// 主要包括: 5 /// 1. ...

  4. 在一个另一个文件中 #include一个**dlg.h文件,会发生dlg的资源ID未定义的错误 :

    1    在一个另一个文件中 #include一个**dlg.h文件,会发生dlg的资源ID未定义的错误 : dlg1.h(23) : error C2065: 'IDD_DIALOG1' : und ...

  5. Codeforces Round #319 (Div. 2) C Vasya and Petya's Game (数论)

    因为所有整数都能被唯一分解,p1^a1*p2^a2*...*pi^ai,而一次询问的数可以分解为p1^a1k*p2^a2k*...*pi^aik,这次询问会把所有a1>=a1k &&am ...

  6. iOS开发笔记--关于 @synchronized,这儿比你想知道的还要多

    http://www.cocoachina.com/ios/20151103/14007.html 本文翻译自 Ryan Kaplan 的 More than you want to know abo ...

  7. Robot Framework(一)入门

    1.1简介 Robot Framework是一个基于Python的,可扩展的关键字驱动的测试自动化框架,用于端到端验收测试和验收测试驱动开发(ATDD).它可用于测试分布式异构应用程序,其中验证需要涉 ...

  8. 原型模式 -- JavaScript语言的灵魂

    原型模式就是将原型对象指向创建对象的类,使这些类共享原型对象的方法与属性.JS是基于原型链实现对象之间的继承,是对属性或者方法的共享,而不是对属性和方法的复制. // 图片轮播类 var LoopIm ...

  9. 2017乌鲁木齐网络赛 j 题

    题目连接 : https://nanti.jisuanke.com/t/A1256 Life is a journey, and the road we travel has twists and t ...

  10. SQLServer死锁

    死锁的四个必要条件:互斥条件(Mutual exclusion):资源不能被共享,只能由一个进程使用.请求与保持条件(Hold and wait):已经得到资源的进程可以再次申请新的资源.非剥夺条件( ...