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]是 ...
随机推荐
- 【SqlServer】SqlServer的游标使用
什么是游标 结果集,结果集就是select查询之后返回的所有行数据的集合. 游标则是处理结果集的一种机制吧,它可以定位到结果集中的某一行,多数据进行读写,也可以移动游标定位到你所需要的行中进行操作数据 ...
- Effective Java 第三版——58. for-each循环优于传统for循环
Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...
- JS中的PadLeft、PadRight,位数不足,自动补位,String扩展方法
类似C#中的 PadLeft.PadRight方法 //方法一 function FillZero(p) { return new Array(3 - (p + '').length + 1).joi ...
- sublime text 3 安装卸载插件和取消启动检查更新
1.Perferences->Package Contro,输入install 调出 Install Package 选项并回车 2.安装相应的插件 2.点击首选项–设置(用户) 3.卸载插件 ...
- linux每日命令(29):chown命令
chown将指定文件的拥有者改为指定的用户或组,用户可以是用户名或者用户ID:组可以是组名或者组ID:文件是以空格分开的要改变权限的文件列表,支持通配符.系统管理员经常使用chown命令,在将文件拷贝 ...
- egret3.x升级5.2
第一步 先用新建项目向导新建一个5.2的项目 第二步 把3.x的代码和资源文件复制过来替换掉 修改资源加载代码 在3.x里 egret采用的是事件机制来加载资源,在5.2里则采用了await/asyn ...
- Centos 编译安装nodejs&express框架
一. 下载nodejs 版本 wget http://nodejs.org/dist/v0.10.28/node-v0.10.28.tar.gz 二. 编译安装 cp node-v0.10.28.ta ...
- hdoj:2027
#include <iostream> #include <string> #include <vector> using namespace std; int m ...
- 分布式系统CAP理论与CA选择
总结: CAP指的是数据一致性.服务可用性.分区容错性:(这里的一致性指的是强一致性,又叫原子性或线性一致性:可用性指的是所有读写操作都要能终止,没有时延上的要求) 分布式系统中P是必选项:在P必选的 ...
- ORA-00257:archiver error问题处理方法
原文链接:http://www.7747.net/database/201109/104615.html ORA-00257: archiver error. Connect internal onl ...