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]是 ...
随机推荐
- ios实例开发精品源码文章推荐(8.28)
iOS源码:游戏引擎-推箱子游戏 <ignore_js_op> http://www.apkbus.com/android-106392-1-11.html iOS源码:进度条-Color ...
- 编程调节Win7/Win8系统音量的一种方法
不得不说, 自Win7(好像是吧), Windows的音量调节功能比以前更人性化了.... 但编程接口却变得更加复杂了............. 还要用到IAudioEndpointVolu ...
- linux 下的emoji在MariaDB中的字符集修改
在此目录下修改这个文件: 添加一行字符集: 然后重启服务,就OK了
- sublime text 3 安装ES6插件
- 未能加载文件或程序集"CheckRegister"或它的某一个依赖项.参数错误. (异常来
报“未能加载文件或程序集“CheckRegister”或它的某一个依赖项.参数错误”的解决方法 问题如下所示: 未能加载文件或程序集“CheckRegister”或它的某一个依赖项.参数错误. (异常 ...
- requestNextAnimationFrame兼容主浏览器方法
window.requestNextAnimationFrame = (function () { var originalRequestAnimationFrame = undefined, wra ...
- Android开发(二十三)——Application
参考: [1] Android中Application类用法.http://www.cnblogs.com/renqingping/archive/2012/10/24/Application.htm ...
- 【平差软件学习---科傻】四、科傻二等水准平差(参数设置和in1文件讲解)
[平差软件学习---科傻]四.科傻二等水准平差(参数设置和in1文件讲解) 这个算是最后一集了,也可能不是如果我想到不足的地方我会在补上一集视频,或者是文章页.总感觉自己操作的很熟练,到自己真正讲的时 ...
- 【emWin】例程二十七:窗口对象——Listview
简介: LISTVIEW小工具可在具有多个列的列表中选择某个元素.由于LISTVIEW小工具包含了 一个HEADER小工具,因此可对列加以管理(排序等).所创建的LISTVIEW 既可以无环绕型框架窗 ...
- Android 测试入门之---Monkey test
这周重点学习的也是Android monkey test 的一些相关知识,也对其进行了初步的操作和试验.讲学习资料整理如下 : Monkey是一个命令行工具 ,可以运行在模拟器里或实际设备中.它向系统 ...