Gym - 101667H

2017-2018 ACM-ICPC, Asia Daejeon Regional Contest

#include<bits/stdc++.h>
using namespace std;
#define maxn 4000005
const double pi=acos(-1.0);
struct com
{
double x,y;
com(double X=,double Y=)
{
x=X,y=Y;
}
}a[maxn],b[maxn];
com operator + (com a,com b) {return com(a.x+b.x,a.y+b.y);}
com operator - (com a,com b) {return com(a.x-b.x,a.y-b.y);}
com operator * (com a,com b) {return com(a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x);}
int S,T,n,m,L,R[maxn],ans[maxn];
long long F[maxn];
char s[maxn],t[maxn];
double f[maxn],g[maxn];
void FFT(com a[maxn],int opt)
{
for (int i=;i<n;++i)
if (i<R[i]) swap(a[i],a[R[i]]);
for (int k=;k<n;k<<=)
{
com wn=com(cos(pi/k),opt*sin(pi/k));
for (int i=;i<n;i+=(k<<))
{
com w=com(,);
for (int j=;j<k;++j,w=w*wn)
{
com x=a[i+j],y=w*a[i+j+k];
a[i+j]=x+y,a[i+j+k]=x-y;
}
}
}
}
void calc(int opt)
{
FFT(a,);FFT(b,);
for (int i=;i<=n;++i) a[i]=a[i]*b[i];
FFT(a,-);
for (int i=S-;i<T;++i)
{
F[i]+=(long long)(a[i].x/n+0.5)*opt;//对于每种匹配位置累计赢的次数
}
}
int main()
{
scanf("%d%d",&T,&S);
scanf("%s%s",t,s);//S短,T长
for(int i=;i<S/;++i) swap(s[i],s[S-i-]);//短串逆置
T=T+S;
for(int i=T-S;i<T;i++)t[i]='B';
t[T]=;
m=S+T-;
for(int i=;i<T;i++)
{
if(t[i]=='S')t[i]='R';
else if(t[i]=='R')t[i]='P';
else if(t[i]=='P')t[i]='S';
}
for (n=;n<=m;n<<=) ++L;
for (int i=;i<n;++i)
R[i]=(R[i>>]>>)|((i&)<<(L-));
for (int i=;i<T;++i) f[i]=(t[i]=='S');
for (int i=;i<S;++i) g[i]=(s[i]=='S');
for (int i=;i<=n;++i) a[i]=com(,),b[i]=com(,);
for (int i=;i<T;++i) a[i].x=f[i];
for (int i=;i<S;++i) b[i].x=g[i];
calc();
for (int i=;i<T;++i) f[i]=(t[i]=='R');
for (int i=;i<S;++i) g[i]=(s[i]=='R');
for (int i=;i<=n;++i) a[i]=com(,),b[i]=com(,);
for (int i=;i<T;++i) a[i].x=f[i];
for (int i=;i<S;++i) b[i].x=g[i];
calc();
for (int i=;i<T;++i) f[i]=(t[i]=='P');
for (int i=;i<S;++i) g[i]=(s[i]=='P');
for (int i=;i<=n;++i) a[i]=com(,),b[i]=com(,);
for (int i=;i<T;++i) a[i].x=f[i];
for (int i=;i<S;++i) b[i].x=g[i];
calc();
long long ans=;
for (int i=S-;i<T;++i)//这个范围自己考虑一下就好了
ans=max(ans,F[i]);//所有位置取max
printf("%lld\n",ans);
}

模仿这个题目在TOJ出了一个题,还无人AC 烟村四五家

Fuzzy Search

 CodeForces - 528D

Leonid works for a small and promising start-up that works on decoding the human genome. His duties include solving complex problems of finding certain patterns in long strings consisting of letters 'A', 'T', 'G' and 'C'.

Let's consider the following scenario. There is a fragment of a human DNA chain, recorded as a string S. To analyze the fragment, you need to find all occurrences of string T in a string S. However, the matter is complicated by the fact that the original chain fragment could contain minor mutations, which, however, complicate the task of finding a fragment. Leonid proposed the following approach to solve this problem.

Let's write down integer k ≥ 0 — the error threshold. We will say that string Toccurs in string S on position i (1 ≤ i ≤ |S| - |T| + 1), if after putting string Talong with this position, each character of string T corresponds to the some character of the same value in string S at the distance of at most k. More formally, for any j (1 ≤ j ≤ |T|) there must exist such p (1 ≤ p ≤ |S|), that |(i + j - 1) - p| ≤ k and S[p] = T[j].

For example, corresponding to the given definition, string "ACAT" occurs in string "AGCAATTCAT" in positions 2, 3 and 6.

Note that at k = 0 the given definition transforms to a simple definition of the occurrence of a string in a string.

Help Leonid by calculating in how many positions the given string T occurs in the given string S with the given error threshold.

Input

The first line contains three integers |S|, |T|, k (1 ≤ |T| ≤ |S| ≤ 200 000, 0 ≤ k ≤ 200 000) — the lengths of strings S and T and the error threshold.

The second line contains string S.

The third line contains string T.

Both strings consist only of uppercase letters 'A', 'T', 'G' and 'C'.

Output

Print a single number — the number of occurrences of T in S with the error threshold k by the given definition.

Examples

Input
10 4 1
AGCAATTCAT
ACAT
Output
3

Note

If you happen to know about the structure of the human genome a little more than the author of the problem, and you are not impressed with Leonid's original approach, do not take everything described above seriously.

#include<bits/stdc++.h>
using namespace std;
typedef double db;
const db PI=acos(-1.0);
struct Complex
{
db x,y;
Complex(db _x=0.0,db _y=0.0):x(_x),y(_y){}
Complex operator + (const Complex &b)const
{
return Complex(x+b.x,y+b.y);
}
Complex operator - (const Complex &b)const
{
return Complex(x-b.x,y-b.y);
}
Complex operator * (const Complex &b)const
{
return Complex(x*b.x-y*b.y,x*b.y+y*b.x);
}
};
void change(Complex y[],int len)
{
for(int i=,j=len/;i<len-;i++)
{
if(i<j)swap(y[i],y[j]);
int k=len/;
while(j>=k)
{
j-=k;
k/=;
}
if(j<k)j+=k;
}
}
void fft(Complex y[],int len,int on)
{
change(y,len);
for(int h=;h<=len;h<<=)
{
Complex wn(cos(-on**PI/h),sin(-on**PI/h));
for(int j=;j<len;j+=h)
{
Complex w(,);
for(int k=j;k<j+h/;k++)
{
Complex u=y[k];
Complex t=w*y[k+h/];
y[k]=u+t;
y[k+h/]=u-t;
w=w*wn;
}
}
}
if(on==-)
for(int i=;i<len;i++)
y[i].x/=len;
}
const char ch[]={'A','C','G','T'};
int lens,lent,k;
char s[],t[];
int es[],ps[];
Complex ss[],tt[];
int cnt[][];
void work(int id)
{
memset(es,,sizeof(es));
memset(ps,,sizeof(ps));
memset(ss,,sizeof(ss));
memset(tt,,sizeof(tt));
for(int i=;i<lens;i++)
if(s[i]==ch[id])
{
es[max(i-k,)]++;
es[min(i+k+,lens)]--;
}
ps[]=es[];
for(int i=;i<lens;i++)ps[i]=es[i]+ps[i-];
for(int i=;i<lens;i++)ss[i].x=(ps[i]>);
for(int i=;i<lent;i++)tt[i].x=(t[i]==ch[id]);
reverse(tt,tt+lent);
int len=;
while(len<*lens || len<*lent)len<<=;
fft(ss,len,);
fft(tt,len,);
for(int i=;i<len;i++)ss[i]=ss[i]*tt[i];
fft(ss,len,-);
for(int i=;i<lens;i++)cnt[id][i]=(int)(ss[i+lent-].x+0.5);
}
int main()
{
scanf("%d%d%d",&lens,&lent,&k);
scanf("%s%s",s,t);
for(int i=;i<;i++)work(i);
int ans=;
for(int i=;i<lens;i++)
{
int tot=;
for(int j=;j<;j++)tot+=cnt[j][i];
ans+=(tot==lent);
}
printf("%d\n",ans);
return ;
}

以前刷过的FFT的更多相关文章

  1. 点分治Day1

    树套树Day2暂且搁置...因为Day1的题我各种不会做... 唯一过了一道还是整体二分过的... 我们来一点愉快的算法,先不考虑数据结构这种骚东西了 毕竟还在发烧,就先码码这几天在搞的点分治吧 hx ...

  2. 【刷题】BZOJ 2179 FFT快速傅立叶

    Description 给出两个n位10进制整数x和y,你需要计算x*y. Input 第一行一个正整数n. 第二行描述一个位数为n的正整数x. 第三行描述一个位数为n的正整数y. Output 输出 ...

  3. [暑假的bzoj刷水记录]

    (这篇我就不信有网站来扣) 这个暑假打算刷刷题啥的 但是写博客好累啊  堆一起算了 隔一段更新一下.  7月27号之前刷的的就不写了 , 写的累 代码不贴了,可以找我要啊.. 2017.8.27upd ...

  4. 洛谷P1919 【模板】A*B Problem升级版 题解(FFT的第一次实战)

    洛谷P1919 [模板]A*B Problem升级版(FFT快速傅里叶) 刚学了FFT,我们来刷一道模板题. 题目描述 给定两个长度为 n 的两个十进制数,求它们的乘积. n<=100000 如 ...

  5. ZJOI2019一轮停课刷题记录

    Preface 菜鸡HL终于狗来了他的省选停课,这次的时间很长,暂定停到一试结束,不过有机会二试的话还是可以搞到4月了 这段时间的学习就变得量大而且杂了,一般以刷薄弱的知识点和补一些新的奇怪技巧为主. ...

  6. 【BZOJ 3451】Tyvj1953 Normal 思维题+期望概率+FFT+点分治

    我感觉是很强的一道题……即使我在刷专题,即使我知道这题是fft+点分治,我仍然做不出来……可能是知道是fft+点分治限制了我的思路???(别做梦了,再怎样也想不出来的……)我做这道题的话,一看就想单独 ...

  7. 多项式乘法,FFT与NTT

    多项式: 多项式?不会 多项式加法: 同类项系数相加: 多项式乘法: A*B=C $A=a_0x^0+a_1x^1+a_2x^2+...+a_ix^i+...+a_{n-1}x^{n-1}$ $B=b ...

  8. 洛谷P1919 A*B problem 快速傅里叶变换模板 [FFT]

    题目传送门 A*B problem 题目描述 给出两个n位10进制整数x和y,你需要计算x*y. 输入输出格式 输入格式: 第一行一个正整数n. 第二行描述一个位数为n的正整数x. 第三行描述一个位数 ...

  9. bzoj 4503 两个串 快速傅里叶变换FFT

    题目大意: 给定两个\((length \leq 10^5)\)的字符串,问第二个串在第一个串中出现了多少次.并且第二个串中含有单字符通配符. 题解: 首先我们从kmp的角度去考虑 这道题从字符串数据 ...

随机推荐

  1. 新建博客第一天,随意来一发Win8运行命令大全

    1.calc:启动计算器 2.appwiz.cpl:程序和功能   3.certmgr.msc:证书管理实用程序 4.charmap:启动字符映射表 5.chkdsk.exe:Chkdsk磁盘检查(管 ...

  2. jsp另外五大内置对象之-out获取缓冲区大小

    <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding= ...

  3. 洛谷 P2176 [USACO14FEB]路障Roadblock

    题目描述 每天早晨,FJ从家中穿过农场走到牛棚.农场由 N 块农田组成,农田通过 M 条双向道路连接,每条路有一定长度.FJ 的房子在 1 号田,牛棚在 N 号田.没有两块田被多条道路连接,以适当的路 ...

  4. db2新添用户

    --1.新添用户  -目录 /XX/XX  -组  XX 用户名useradd -d /home/xx -g users xx--2.修改密码passwd xx--3.在QC中grant权限.新添表空 ...

  5. UVA12906 Maximum Score (组合)

    对于每个元素,最理想的情况就是都在它的左边或者右边,那么sort一下就可以得到一个特解了,然后大的中间不能有小的元素,因为如果有的话,那么无论选小的还是选大的都不是最优.对小的元素来说,比它大的元素在 ...

  6. Android(java)学习笔记109:Java中输入和输出流概念

     程序在内存中运行,文件在磁盘上,把文件从磁盘上读入内存中来,当然是输入流了,   反之,把内存中的数据写到磁盘上的文件里面去就是输出.通常都是这样的,用了过滤流的情况复杂一些,则另当别论.

  7. Android(java)学习笔记128:xml文件生成

    1.xml文件: 用元素描述数据,跨平台. 2.利用传统的方式创建xml文件,下面是一个案例: 设计思路:建立一个学生管理系统,创建xml文件保存学生信息: (1)首先是布局文件activity_ma ...

  8. 01_5_删除指定id的单个对象

    01_5_删除指定id的单个对象 1. 配置相应的映射文件内容 <delete id="deleteStudent" parameterClass="int&quo ...

  9. cocos2dx for lua 简单的翻牌动画

    local x = 20 local y = display.height/2 for i = 1,16 do--创建16张 local cardFg = display.newSprite(&quo ...

  10. 哈希表(Hash Table)/散列表(Key-Value)

    目录 1. 哈希表的基本思想 2. 哈希表的相关基本概念 1.概念: 2.哈希表和哈希函数的标准定义: 1)冲突: 2)安全避免冲突的条件: 3)冲突不可能完全避免 4)影响冲突的因素 3. 哈希表的 ...