CF472G Increase the Constraints
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的更多相关文章
- 【CF472G】Design Tutorial: Increase the Constraints
Description 给出两个01序列\(A\)和\(B\) 要求回答\(q\)个询问每次询问\(A\)和\(B\)中两个长度为\(len\)的子串的哈明距离 哈明距离的值即有多少个位置不相等 ...
- cf 472G Design Tutorial: Increase the Constraints 分块+压位/FFT
题目大意 给出两个\(01\)序列\(A\)和\(B\) 哈明距离定义为两个长度相同的序列中,有多少个对应位置上的数字不一样 "00111" 和 "10101" ...
- CF数据结构练习
1. CF 438D The Child and Sequence 大意: n元素序列, m个操作: 1,询问区间和. 2,区间对m取模. 3,单点修改 维护最大值, 取模时暴力对所有>m的数取 ...
- Propagation of Visual Entity Properties Under Bandwidth Constraints
1. Introduction The Saga of Ryzom is a persistent massively-multiplayer online game (MMORPG) release ...
- iOS Programming Auto Layout: Programmatic Constraints 自动布局:通过编程限制
iOS Programming Auto Layout: Programmatic Constraints 1. However, if your views are created in co ...
- States of Integrity Constraints
States of Integrity Constraints As part of constraint definition, you can specify how and when Oracl ...
- Go build constraints
Go语言有一个不(奇)错(葩)的设计,就是build constraints(构建约束).可以在源码中通过注释的方式指定编译选项,比如只允许在linux下,或者在386的平台上编译啊之类的:还可以通过 ...
- Unable to simultaneously satisfy constraints.
在进行版本的迭代更新时,新功能需求需要对主页面的UI进行重新的布局,但是,报了错误,出了好多约束方面的问题: Unable to simultaneously satisfy constraints. ...
- 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 ...
随机推荐
- 适合 ASP.NET Core 的超级-DRY开发
作者 Thomas Hansen DRY 是那些非常重要的软件体系结构缩写之一.它的意思是“不要自我重复”,并向维护旧源代码项目的任何用户阐明了一个重要原则.也就是说,如果你在代码中自我重复,会发现每 ...
- 新博客 https://k8gege.org
新博客 https://k8gege.org 于2019/12/3启用,忘了发 由于博客园长期被Google误报屏蔽,导致Firefox/Chrome等浏览器无法访问博客 发现将被Google误报的文 ...
- [转帖]知新之--12-factors
知新之--12-factors https://blog.csdn.net/weixin_34233421/article/details/85819756 12-factors I. 基准代码 一份 ...
- What Is HLS (HTTP Live Streaming)?
HTTP Live Streaming (HLS) Executive Summary HTTP Live Streaming (or HLS) is an adaptive streaming c ...
- Github 上优秀的 Java 项目推荐
1.JavaGuide 地址:Snailclimb/JavaGuide [Java学习+面试指南] 一份涵盖大部分Java程序员所需要掌握的核心知识. 2.DoraemonKit 地址:didi/Do ...
- TCP和UDP的差异
TCP建立连接 如果有人问TCP如何建立连接?大部分的回答都是三次握手成功,就建立连接了. 那么握手的目的是什么呢? 1)告知对方自己的序号 2)初始化资源 例如以下握手案例,握手两次由客户端主动发起 ...
- mysql给某个用户单个表权限
CREATE USER systemselect IDENTIFIED BY 'Zbank123456';#只给查询权限 GRANT SELECT ON szkitil.zbank_businesss ...
- mybatis中用注解如何处理存储过程返回的多个结果集?
sql代码: create procedure sptest.getnamesanditems() reads sql data dynamic result sets 2 BEGIN ATOMIC ...
- springboot整合docker部署
环境安装 首先,需要安装Docker(例如:docker for windows) 下载地址:https://download.docker.com/win/stable/Docker%20for%2 ...
- springboot使用 @Transactional 注解配置事务管理
介绍 springboot对数据库事务的使用非常的方便,只需要在方法上添加@Transactional注解即可.Spring 为事务管理提供了丰富的功能支持.Spring 事务管理分为编程式和声明式的 ...