CodeForces 1056E - Check Transcription - [字符串hash]
题目链接:https://codeforces.com/problemset/problem/1056/E
One of Arkady's friends works at a huge radio telescope. A few decades ago the telescope has sent a signal $s$ towards a faraway galaxy. Recently they've received a response $t$ which they believe to be a response from aliens! The scientists now want to check if the signal $t$ is similar to $s$.
The original signal $s$ was a sequence of zeros and ones (everyone knows that binary code is the universe-wide language). The returned signal $t$, however, does not look as easy as $s$, but the scientists don't give up! They represented $t$ as a sequence of English letters and say that $t$ is similar to $s$ if you can replace all zeros in $s$ with some string $r_0$ and all ones in $s$ with some other string $r_1$ and obtain $t$. The strings $r_0$ and $r_1$ must be different and non-empty.
Please help Arkady's friend and find the number of possible replacements for zeros and ones (the number of pairs of strings $r_0$ and $r_1$) that transform $s$ to $t$.
Input
The first line contains a string $s (2 \le |s| \le 10^5)$ consisting of zeros and ones — the original signal.
The second line contains a string $t (1 \le |t| \le 10^6)$ consisting of lowercase English letters only — the received signal.
It is guaranteed, that the string $s$ contains at least one '0' and at least one '1'.
Output
Print a single integer — the number of pairs of strings $r_0$ and $r_1$ that transform $s$ to $t$.
In case there are no such pairs, print 0.
Examples
Input
01
aaaaaa
Output
4
Input
001
kokokokotlin
Output
2
Note
In the first example, the possible pairs $(r_0,r_1)$ are as follows:
"a", "aaaaa"
"aa", "aaaa"
"aaaa", "aa"
"aaaaa", "a"
The pair "aaa", "aaa" is not allowed, since $r_0$ and $r_1$ must be different.
In the second example, the following pairs are possible:
"ko", "kokotlin"
"koko", "tlin"
题意:
给定一个 $0,1$ 字符串 $s$,和一个小写字母组成的字符串 $t$。
现在要找出两个互不相同且非空的字符串 $r_0, r_1$,使得将 $s$ 中的 $0$ 全部替换成 $r_0$,将 $1$ 全部替换成 $r_1$ 后,即可得到 $t$。
要求输出不同二元组 $(r_0,r_1)$ 的数量。
题解:
不妨枚举 $r_0$ 的长度,这样一来,根据 $0,1$ 的数目,可以直接算出 $r_1$ 的长度。
然后遍历 $s$,看此时假设的 $r_0$ 和 $r_1$ 是否可以成立,这个可以对 $t$ 字符串hash之后 $O(1)$ 地求出每个 $0,1$ 转换出来的 $r_0,r_1$ 符不符合要求。
关于时间复杂度,表面上看起来时间复杂度为 $O(|s||t|)$ 的,但实际上由于要满足条件 $|t| = cnt_0 \cdot |r_0| + cnt_1 \cdot |r_1|$,可供选择的 $(|r_0|,|r_1|)$ 比 |t| 少很多。
而且,对于很多的 $(|r_0|,|r_1|)$,若其对应的 $(r_0,r_1)$ 并非可行解,其实很快就能判定出来,不需要 $O(|s|)$ 次hash。
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int P=;
const int M=1e7+;
const int maxs=1e5+;
const int maxt=1e6+; int lens,lent;
char s[maxs],t[maxt];
int tot0,tot1; ll pre[maxt],Ppow[maxt];
void prework(int len)
{
pre[]=;
Ppow[]=;
for(int i=;i<=len;i++)
{
pre[i]=pre[i-]*P+(t[i]-'a'+), pre[i]%=M;
Ppow[i]=Ppow[i-]*P%M;
}
}
inline ll Hash(int l,int r) {
return (pre[r]-pre[l-]*Ppow[r-(l-)]%M+M)%M;
}
int main()
{
scanf("%s%s",s+,t+); lens=strlen(s+), lent=strlen(t+);
tot0=tot1=;
for(int i=;i<=lens;i++) tot0+=(s[i]==''), tot1+=(s[i]==''); int ans=;
int len0,len1;
prework(lent);
for(len0=;len0<lent;len0++)
{
if(tot0*len0>=lent) break;
if((lent-tot0*len0)%tot1>) continue;
len1=(lent-tot0*len0)/tot1; bool ok=;
int cnt0=, cnt1=;
ll hash_r0=, hash_r1=;
for(int i=;i<=lens;i++)
{
if(s[i]=='')
{
int l=cnt0*len0+cnt1*len1+, r=l+len0-;
ll has=Hash(l,r);
if(hash_r0==) {
hash_r0=has;
} else {
if(hash_r0!=has) {
ok=; break;
}
}
cnt0++;
}
if(s[i]=='')
{
int l=cnt0*len0+cnt1*len1+, r=l+len1-;
ll has=Hash(l,r);
if(hash_r1==) {
hash_r1=has;
} else {
if(hash_r1!=has) {
ok=; break;
}
}
cnt1++;
}
if(hash_r0!= && hash_r1!= && hash_r0==hash_r1) {
ok=;
break;
}
}
if(ok) ans++;
}
printf("%d\n",ans);
}
注意:不要使用unsigned long long类型的溢出自动取模,会被卡掉;改为对大质数取模就能通过了。
CodeForces 1056E - Check Transcription - [字符串hash]的更多相关文章
- CF1056E Check Transcription 字符串哈希
传送门 暴力枚举\(0\)的长度,如果对应的\(1\)的长度也是一个整数就去check是否合法.check使用字符串哈希. 复杂度看起来是\(O(st)\)的,但是因为\(01\)两个数中数量较多的至 ...
- CodeForces 7D Palindrome Degree 字符串hash
题目链接:点击打开链接 #include<stdio.h> #include<iostream> #include<string.h> #include<se ...
- Codeforces1056E.Check Transcription(枚举+Hash)
题目链接:传送门 题目: E. Check Transcription time limit per test seconds memory limit per test megabytes inpu ...
- CodeForces - 727E Games on a CD 字符串Hash
题意:有n个单词,每个单词长度为k,顺时针将它们写成一个圆圈串.现在知道g个长度为k的单词,是否可以从这g个单词中选择n个形成这个圆圈串?如果有多个答案,任意输出一个. 思路 可以发现,如果枚举第一个 ...
- 【题解】 Codeforces Edu44 F.Isomorphic Strings (字符串Hash)
题面戳我 Solution 我们按照每个字母出现的位置进行\(hash\),比如我们记录\(a\)的位置:我们就可以把位置表示为\(0101000111\)这种形式,然后进行字符串\(hash\) 每 ...
- codeforces gym 101164 K Cutting 字符串hash
题意:给你两个字符串a,b,不区分大小写,将b分成三段,重新拼接,问是否能得到A: 思路:暴力枚举两个断点,然后check的时候需要字符串hash,O(1)复杂度N*N: 题目链接:传送门 #prag ...
- 【codeforces 514C】Watto and Mechanism(字符串hash)
[题目链接]:http://codeforces.com/contest/514/problem/C [题意] 给你n个字符串; 然后给你m个询问;->m个字符串 对于每一个询问字符串 你需要在 ...
- Codeforces 898F - Restoring the Expression(字符串hash)
898F - Restoring the Expression 思路:字符串hash,base是10,事实证明对2e64取模会T(也许ull很费时),对1e9+7取模. 代码: #include< ...
- 线段树 + 字符串Hash - Codeforces 580E Kefa and Watch
Kefa and Watch Problem's Link Mean: 给你一个长度为n的字符串s,有两种操作: 1 L R C : 把s[l,r]全部变为c; 2 L R d : 询问s[l,r]是 ...
随机推荐
- Spring入门教程:通过MyEclipse开发第一个Spring项目
Animal.java package com.project; public class Animal { private String name; public String getName() ...
- [Aaronyang] 写给自己的WPF4.5 笔记9[复杂数据处理三步曲,数据展示ListView泪奔2/3]
我的文章一定要做到对读者负责,否则就是失败的文章 --------- www.ayjs.net aaronyang技术分享 作者留言: 小小的推荐,作者的肯定,读者的支持. ...
- ios开发版证书与企业证书相关文件申请安装及其使用方法
本文主要讲述以下内容: ios开发版证书的申请, 企业证书的申请, appid的创建, provision profile的生成, 开发设备devices的绑定, 以及每个证书文件之间的关系, 最后使 ...
- javascript promises powered by BlueBird
什么是promises? 为什么需要promises? 参见: https://promisesaplus.com/ 使用示例: 使用promises之前,代码的编写方式: 使用promises之后: ...
- PEM文件
原文链接: http://blog.sina.com.cn/s/blog_489f88710100a59w.html OpenSSL 使用 PEM 文件格式存储证书和密钥.PEM 实质上是 Base6 ...
- python列表中元素插入位置总结
要完成的操作是把一个列表里的元素通过for循环添加到另外一个列表里,但是通过insert()方法添加到另外一个列表后却发现元素的位置与原始列表的颠倒了.如以下实例: li1 = ['] li2 = [ ...
- ECSHOP后台编辑器不能上传中文名图片的解决办法
在后台上传商品图片的时候,如果你选择一个中文名称的图片,那么上传后会产生乱码,导致图片显示不出来. 下面说一种解决办法: 使用“年月日时分秒 + 6个随机字符”做为文件名,如 201010161356 ...
- 初试 Kubernetes 集群中使用 Traefik 反向代理
初试 Kubernetes 集群中使用 Traefik 反向代理 2017年11月17日 09:47:20 哎_小羊_168 阅读数:12308 版权声明:本文为博主原创文章,未经博主允许不得转 ...
- github管理开发代码流程
首先.通过github网站新建一个仓库,得到仓库地址 https://github.com/piercalex/a.git 接着回到客户端,打开git shell: //在客户端配置账户信息 git ...
- Spark学习笔记——数据读取和保存
spark所支持的文件格式 1.文本文件 在 Spark 中读写文本文件很容易. 当我们将一个文本文件读取为 RDD 时,输入的每一行 都会成为 RDD 的 一个元素. 也可以将多个完整的文本文件一次 ...