Increase the Constraints

定义两个等长的01字符串的汉明距离为它们字符不同的对应位置的个数。

给你两个01串S,T,现在有q个询问,每次指定S,T中两个定长的子串询问它们的汉明距离。

1≤|S|,|T|≤200000,1≤q≤400000

cz_xuyixuan的题解

字符不同=长度-字符相同。考虑到两个字符串的匹配问题可以用FFT处理,于是往FFT方面考虑。

分块FFT,令分块大小为B,进行O(\(\frac{n}{B}\))次FFT,处理出O(\(\frac{n}{B}\))个T的后缀与S的每个后缀能够匹配的位数。询问时容斥一下并加上边角暴力就好了。

这样的时间复杂度是O(\(\frac{n^2\log n}{B}\)+qB),取B=n\(\sqrt{\frac{\log n}{q}}\)=‭1,327.013205时,可以获得渐进意义下最优复杂度O(n\(\sqrt{q log n}\))。

有一种高妙的做法来解决01匹配问题。我们令0为1,1为-1,然后FFT。那么两个字符如果匹配,得数为1,否则为-1。我们给1和-1的总和加上长度,那么就变成了匹配得2,不匹配得0.

由于NTT常数大,所以程序取B=7200。

CO int N=524288;
int rev[N],omg[N]; void NTT(int a[],int lim,int dir){
for(int i=0;i<lim;++i)
if(i<rev[i]) swap(a[i],a[rev[i]]);
for(int i=1;i<lim;i<<=1)
for(int j=0;j<lim;j+=i<<1)
for(int k=0;k<i;++k){
int t=mul(omg[lim/(i<<1)*k],a[j+i+k]);
a[j+i+k]=add(a[j+k],mod-t),a[j+k]=add(a[j+k],t);
}
if(dir==-1){
int ilim=fpow(lim,mod-2);
for(int i=0;i<lim;++i) a[i]=mul(a[i],ilim);
}
} CO int B=7200;
char s[N],t[N];int ls,lt;
int index[N],l[N],r[N],tot;
int a[N],b[N],ans[N/B][N]; int query(int ps,int pt){
int ans=0;
if(ps+B>=ls or pt+B>=lt){
for(;ps<ls and pt<lt;++ps,++pt) ans+=s[ps]==t[pt];
return ans;
}
for(;index[pt]==index[pt-1];++ps,++pt) ans+=s[ps]==t[pt];
ans+=::ans[index[pt]][ps];
return ans;
}
int main(){
scanf("%s%s",s,t);
ls=strlen(s),lt=strlen(t);
for(int i=0;i<lt;++i){
if(i%B==0) l[++tot]=i;
index[i]=tot,r[tot]=i;
}
for(int p=1;p<=tot;++p){
memset(a,0,sizeof a);
for(int i=0;i<ls;++i) a[i]=s[i]=='0'?1:mod-1;
memset(b,0,sizeof b);
for(int i=l[p];i<lt;++i) b[lt-1-i]=t[i]=='0'?1:mod-1;
int n=lt-l[p]-1; int len=ceil(log2(ls+n)),lim=1<<len;
for(int i=0;i<lim;++i) rev[i]=rev[i>>1]>>1|(i&1)<<(len-1);
omg[0]=1,omg[1]=fpow(3,(mod-1)/lim);
for(int i=2;i<lim;++i) omg[i]=mul(omg[i-1],omg[1]);
NTT(a,lim,1),NTT(b,lim,1);
for(int i=0;i<lim;++i) a[i]=mul(a[i],b[i]);
omg[1]=fpow(omg[1],mod-2);
for(int i=2;i<lim;++i) omg[i]=mul(omg[i-1],omg[1]);
NTT(a,lim,-1); for(int i=0;i<ls;++i){
ans[p][i]=add(a[i+n],add(n+1,mod-max(0,i+n-ls+1))); // edit 1
ans[p][i]=mul(ans[p][i],i2);
}
}
for(int q=read<int>();q--;){
int ps=read<int>(),pt=read<int>(),n=read<int>();
printf("%d\n",n-query(ps,pt)+query(ps+n,pt+n));
}
return 0;
}

处理ans数组的时候还是要放到模意义下,因为1和-1的总和可能为负数。

CF472G Increase the Constraints的更多相关文章

  1. 【CF472G】Design Tutorial: Increase the Constraints

    Description 给出两个01序列\(A\)和\(B\) 要求回答\(q\)个询问每次询问\(A\)和\(B\)中两个长度为\(len\)的子串的哈明距离 ​ 哈明距离的值即有多少个位置不相等 ...

  2. cf 472G Design Tutorial: Increase the Constraints 分块+压位/FFT

    题目大意 给出两个\(01\)序列\(A\)和\(B\) 哈明距离定义为两个长度相同的序列中,有多少个对应位置上的数字不一样 "00111" 和 "10101" ...

  3. CF数据结构练习

    1. CF 438D The Child and Sequence 大意: n元素序列, m个操作: 1,询问区间和. 2,区间对m取模. 3,单点修改 维护最大值, 取模时暴力对所有>m的数取 ...

  4. Propagation of Visual Entity Properties Under Bandwidth Constraints

    1. Introduction The Saga of Ryzom is a persistent massively-multiplayer online game (MMORPG) release ...

  5. iOS Programming Auto Layout: Programmatic Constraints 自动布局:通过编程限制

    iOS Programming  Auto Layout: Programmatic Constraints  1.  However, if your views are created in co ...

  6. States of Integrity Constraints

    States of Integrity Constraints As part of constraint definition, you can specify how and when Oracl ...

  7. Go build constraints

    Go语言有一个不(奇)错(葩)的设计,就是build constraints(构建约束).可以在源码中通过注释的方式指定编译选项,比如只允许在linux下,或者在386的平台上编译啊之类的:还可以通过 ...

  8. Unable to simultaneously satisfy constraints.

    在进行版本的迭代更新时,新功能需求需要对主页面的UI进行重新的布局,但是,报了错误,出了好多约束方面的问题: Unable to simultaneously satisfy constraints. ...

  9. Drop all the tables, stored procedures, triggers, constraints and all the dependencies in one SQL statement

    Is there any way in which I can clean a database in SQl Server 2005 by dropping all the tables and d ...

随机推荐

  1. 转载:Linux命令行快捷键

    常用 Ctrl + 左右键:在单词之间跳转 Ctrl + A:跳到本行的行首 Ctrl + E:跳到页尾 Ctrl + U:删除当前光标前面的所有文字(还有剪切功能) Ctrl + K:删除当前光标后 ...

  2. idea 跳转提示多个实现类

  3. C#子线程执行完后,调用主线程的方法

    private delegate void CheckVersionNumber_CallBack(string str);//定义一个为委托 用于 检测版本 //检测版本private void m ...

  4. VS2019+EF6连接Mysql

    开发环境:Win10 + VS2019Mysql服务器版本:8.0.16 一.下载并安装插件(必备) MySQL-Connector-net-6.9.12  链接https://cdn.mysql.c ...

  5. CMS-headless or non-headless, page-based or object-based storage?

    内容管理系统对于很多在线教育企业来说都是至关重要的,他不仅可以用于内容的创作,编辑,发布,撤销,展示也可以用于运营或者市场产生他们需要的页面. 传统上,Wordpress是一个非常成功的CMS,他将内 ...

  6. nlp学习杂记

    什么是 token embedding? 输入一个word,在字典里查找得到它对应的下标就是token,然后用该数字下标去lookup表查找得到该词对应的词向量(词嵌入)就是embedding wor ...

  7. webpack 里的 import, exports 实现原理

    在使用 webpack 对脚本进行打包, 在开发中, 每个文件中都会使用 import 语句来导入一些功能,又会使用 export 语句导出一些功能,为了研究 import 和 export 原理,研 ...

  8. 阿里巴巴Java开发手册更新了!

    自2017年,<阿里巴巴Java开发手册>发布,现已有超过260万位工程师下载及查阅手册,在数以千计的企业应用,手册成为受业界认可的开发规范. 昨天,<Java开发手册>再次更 ...

  9. Unity手游汉化笔记②:使用UABE替换TTF字体

    总的笔记:https://www.cnblogs.com/guobaoxu/p/12055930.html 目录 一.分析 二.思路 三.具体实践 四.总结 Unity版本:2018.4.5f1 工具 ...

  10. Centos7 rsync+inotify实现实时同步更新

    inotify slave部署      把master上指定文件下载到本地的主机指定目录 yum install rsync –y [root@localhost ~]# useradd rsync ...