典型的字符串哈希题

字符串hash[i]:子串s[1-i]代表的值

要截取串s[l-r]  求hash[r]-hash[l-1]*F[len]即可

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define P 131
#define mod 1000000007
#define maxn 3000005
char s[maxn],t[maxn];
ll cnt0,cnt1,len1,len2,n,F[maxn],ash[maxn]; int check(int a,int b){
int last=;
ll h1,f1=,h2,f2=;
for(int i=;i<=len1;i++){
if(s[i]==''){
if(f1==)h1=(ash[last+a]-ash[last]*F[a]%mod+mod)%mod,f1=;
else if(h1!=(ash[last+a]-ash[last]*F[a]%mod+mod)%mod) return ;
last+=a;
}
else {
if(f2==)h2=(ash[last+b]-ash[last]*F[b]%mod+mod)%mod,f2=;
else if(h2!=(ash[last+b]-ash[last]*F[b]%mod+mod)%mod) return ;
last+=b;
}
}
if((h1+mod)%mod==(h2+mod)%mod)return ;
return ;
} int main(){
F[]=;
for(int i=;i<=;i++)F[i]=F[i-]*P%mod;
cin>>s+>>t+;
len1=strlen(s+);len2=strlen(t+);
for(int i=;i<=len1;i++)
if(s[i]-'')cnt1++;else cnt0++;
for(int i=;i<=len2;i++)
ash[i]=t[i]-'a'++ash[i-]*P,ash[i]%=mod; int ans=;
for(int i=;i<=len2;i++){
int t0=i,t1;
if(t0*cnt0>=len2)break;
if((len2-t0*cnt0)%cnt1==){
t1=(len2-t0*cnt0)/cnt1;
if(check(t0,t1))ans++;
}
}
cout<<ans<<endl;
}

字符串哈希——1056E的更多相关文章

  1. HDU 1880 魔咒词典(字符串哈希)

    题目链接 Problem Description 哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一 ...

  2. 洛谷P3370 【模板】字符串哈希

    P3370 [模板]字符串哈希 143通过 483提交 题目提供者HansBug 标签 难度普及- 提交  讨论  题解 最新讨论 看不出来,这题哪里是哈希了- 题目描述 如题,给定N个字符串(第i个 ...

  3. HDU2594 Simpsons’ Hidden Talents 字符串哈希

    最近在学习字符串的知识,在字符串上我跟大一的时候是没什么区别的,所以恶补了很多基础的算法,今天补了一下字符串哈希,看的是大一新生的课件学的,以前觉得字符串哈希无非就是跟普通的哈希没什么区别,倒也没觉得 ...

  4. LA 6047 Perfect Matching 字符串哈希

    一开始我用的Trie+计数,但是不是计多了就是计少了,后来暴力暴过去的…… 看了别人的代码知道是字符串哈希,但是仍有几个地方不理解: 1.26^500溢出问题 2.没考虑哈希碰撞? 跪求指点! #in ...

  5. AC日记——【模板】字符串哈希 洛谷 3370

    题目描述 如题,给定N个字符串(第i个字符串长度为Mi,字符串内包含数字.大小写字母,大小写敏感),请求出N个字符串中共有多少个不同的字符串. 友情提醒:如果真的想好好练习哈希的话,请自觉,否则请右转 ...

  6. 从Hash Killer I、II、III论字符串哈希

    首先,Hash Killer I.II.III是BZOJ上面三道很经典的字符串哈希破解题.当时关于II,本人还琢磨了好久,但一直不明白为啥别人AC的代码都才0.3kb左右,直到CYG神犇说可以直接随机 ...

  7. 【NOIP模拟】Grid(字符串哈希)

    题目背景 SOURCE:NOIP2016-RZZ-1 T3 题目描述 有一个 2×N 的矩阵,矩阵的每个位置上都是一个英文小写字符. 现在需要从某一个位置开始,每次可以移动到一个没有到过的相邻位置,即 ...

  8. 洛谷 P3370 【模板】字符串哈希

    洛谷 P3370 [模板]字符串哈希 题目描述 如题,给定N个字符串(第i个字符串长度为Mi,字符串内包含数字.大小写字母,大小写敏感),请求出N个字符串中共有多少个不同的字符串. 友情提醒:如果真的 ...

  9. cf_514C(字符串哈希)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=121897#problem/G Watto and Mechanism Time ...

随机推荐

  1. Redis基于主从复制的RCE(redis4.x 5.x)复现

    使用docker建立redis 拉取镜像 运行 查看 可以连接,存在未授权 https://github.com/Ridter/redis-rce 发送poc i:正向连接 r:反弹 反弹成功

  2. 实现读取文本数据,在将数据导入mysql

    import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.File; import java ...

  3. 获取从天气预报接口返回回来的json数据

    搬迁到了我的新博客中 ==> http://www.suanliutudousi.com/2017/08/26/%E8%8E%B7%E5%8F%96%E4%BB%8E%E5%A4%A9%E6%B ...

  4. FILE_OBJECT

    https://msdn.microsoft.com/en-us/library/windows/hardware/ff545834(v=vs.85).aspx The FILE_OBJECT str ...

  5. JS事件 鼠标经过事件(onmouseover)鼠标经过事件,当鼠标移到一个对象上时,该对象就触发onmouseover事件,并执行onmouseover事件调用的程序。

    鼠标经过事件(onmouseover) 鼠标经过事件,当鼠标移到一个对象上时,该对象就触发onmouseover事件,并执行onmouseover事件调用的程序. 现实鼠标经过"确定&quo ...

  6. Centos7 pxe

    yum install dnsmasq mv /etc/dnsmasq.conf /etc/dnsmasq.conf.backup # vim /etc/dnsmasq.conf interface= ...

  7. linux zip,tar压缩文件夹 忽略 .git 文件夾

    linux zip 忽略 .git 文件夾 # zip 命令 zip -r bitvolution.zip bitvolution -x *.git* # tar命令压缩文件夹忽略 .git文件夹 t ...

  8. python读文件判断是否已到EOF

    python读文件判断是否已到EOF,也即结尾,一般其它语言都是以EOF直接来判断的,比如 if ( fp.read(chunk_size) == EOF), 但python到结尾后是返回空字符串的, ...

  9. ubuntu下安装git提示无root权限

    apt-get install git 获取git指令 sudo passwd root 重置unix密码 su root 键入密码 参考链接 https://www.cnblogs.com/2she ...

  10. Python: 生成器与迭代 generators and iteration

    https://eastlakeside.gitbooks.io/interpy-zh/content/Generators/ 文章不是非常好 1,三个概念 可迭代对象 iterable, 迭代器 i ...