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]是 ...
随机推荐
- 【PHP】php生成一个不重复的数字(订单号、会员号)
1.目的:利用php的do .. while 生成一个不重复的字符串或者数组,比如(订单号.会员号) 2.不废话,代码来: $repeat_order = array(); do{ $ordersn ...
- easyui-switchbutton js判断是否启动
<input class="easyui-switchbutton" id="statusId" name="status" onTe ...
- curl 调用jenkins的api
jenkins提供了rest api,通过调用接口,可以执行一些job的操作,如构建job ,新建job,启用禁用等操作骑车 其次curl是什么? cURL是一个利用URL语法在命令行下工作的文件传输 ...
- elasticsearch中 refresh 和flush区别【转】
elasticsearch中有两个比较重要的操作:refresh 和 flush refresh操作 当我们向ES发送请求的时候,我们发现es貌似可以在我们发请求的同时进行搜索.而这个实时建索引并可以 ...
- GDB用法简要整理
[时间:2017-05] [状态:Open] [关键词:gdb,调试,debug,用户手册] 使用gdb是需要在编译是指定-g命令,在可执行文件中添加符号信息. 1. 启动和退出 可以使用gdb gd ...
- Error:java: invalid source release 无效的源发行版: 8
原因:这是由于jdk的版本与项目的要求不一致造成的,如果是maven项目,首先查看一下pom.xml,以我的项目为例: 从其中可以看出要求的编译插件为1.8版本,而我本机上安装的jdk为1.7版本,因 ...
- C#学习笔记(33)——批量修改word标题
说明(2017-12-22 11:20:44): 1. 因为数学脚本的主标题和副标题没有格式,目录导航里不显示,修改的时候不好定位,所以需要改成下图格式: 2. 问题的难点有两个,一个是word的操作 ...
- Vue中的computed属性
阅读Vue官网的过程中,对于计算属于与监听器章节的内容有点理解的不清晰:https://cn.vuejs.org/v2/guide/computed.html. 后来上网查询了资料,结合官网的说明,总 ...
- buildroot 编译问题
buildroot 编译的时候,出现 g++: internal compiler error: Killed (program cc1plus) 这个是因为虚拟机内存不足的原因, 加上一些内存即可 ...
- MyCAT简易入门 (Linux)
MyCAT是mysql中间件,前身是阿里大名鼎鼎的Cobar,Cobar在开源了一段时间后,不了了之.于是MyCAT扛起了这面大旗,在大数据时代,其重要性愈发彰显.这篇文章主要是MyCAT的入门部署. ...