题目链接:传送门

题目:

E. Check Transcription
time limit per test
seconds
memory limit per test
megabytes
input
standard input
output
standard output 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 r0 and all ones in s with some other string r1 and obtain t. The strings r0 and r1 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 r0 and r1) that transform s to t .
Input The first line contains a string s
(≤|s|≤) consisting of zeros and ones — the original signal. The second line contains a string t
(≤|t|≤ ) consisting of lowercase English letters only — the received signal. It is guaranteed, that the string s contains at least one '' and at least one ''.
Output Print a single integer — the number of pairs of strings r0
and r1 that transform s to t . In case there are no such pairs, print .
Examples
Input
Copy aaaaaa Output
Copy Input
Copy kokokokotlin Output
Copy Note In the first example, the possible pairs (r0,r1) are as follows: "a", "aaaaa"
"aa", "aaaa"
"aaaa", "aa"
"aaaaa", "a" The pair "aaa", "aaa" is not allowed, since r0
and r1 must be different. In the second example, the following pairs are possible: "ko", "kokotlin"
"koko", "tlin"

题目大意:

  给定一个0、1组成的二进制串s,和一个由小写字母组成的字符串t。

  s中的0、1可以映射成长度不为0的任意长度的字符串(0和1映射的字符串不能相同),求能把s转化成t的映射方案数。

  2 ≤ |s| ≤ 105,1 ≤ |t| ≤ 106

思路:

  如果0映射的字符串的长度确定了,那么1映射的字符串的长度也是唯一确定的。因为:cnt0*len0 + cnt1*len1 = |t|(cnt表示s中0、1的数量,len表示映射出的字符串的长度)。

  那么不妨从1开始枚举0映射出来的字符串的长度len0,那么len1就可以直接求出。这里枚举的len0复杂度为$O(\frac{|t|}{cnt_{0}})$。

  然后验证的时候如果直接暴力,因为字符串的比较是O(len0),验证的复杂度会达到O(len0*|s|),那么总复杂度会高达O(|t|*|s|),显然不可以。这里Hash一下可以达到$O(\frac{|t|*|s|}{cnt_{0}})$。

  因为cnt0和cnt1的较大者可以达到$O(\frac{|s|}{2})$的大小,所以实际复杂度大约是$O(\frac{|t|*|s|}{\frac{|s|}{2}}) = O(2*|t|)$

代码:(用李煜东那本《进阶指南》上的Hash,Wa得好惨,最后加了个模1e9+7才过的)

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int MOD = 1e9 + ;
const int MAX_N = 1e5 + ;
const int MAX_M = 1e6 + ; int n, m;
int cnt0, cnt1;
int len0, len1;
char s[MAX_N], t[MAX_M];
ll f[MAX_M], p[MAX_M];
char r[][MAX_M]; inline ll getHash(int l, int r)
{
ll res = (f[r] - f[l-]*p[r-l+])%MOD;
if (res < )
res += MOD;
return res;
}
//
bool work()
{
ll r0 = , r1 = ;
for (int i = , j = ; i <= n; i++) {
if (s[i] == '') {
if (!r0) {
r0 = getHash(j, j+len0-);
j += len0;
continue;
} if (r0 == getHash(j, j+len0-))
j += len0;
else
return false;
}
else if (s[i] == '') {
if (!r1) {
r1 = getHash(j, j+len1-);
j += len1;
continue;
} if (r1 == getHash(j, j+len1-))
j += len1;
else
return false;
}
}
if (r1 == r0)
return false;
return true;
} int main()
{
scanf("%s%s", s+, t+);
n = strlen(s+), m = strlen(t+);
// cin >> s >> t;
// n = s.size(), m = t.size();
cnt0 = , cnt1 = ;
for (int i = ; i <= n; i++) {
switch(s[i]) {
case '': cnt0++; break;
case '': cnt1++; break;
}
}
p[] = ;
for (int i = ; i <= m; i++) {
f[i] = (f[i-]* + t[i]-'a'+)%MOD;
p[i] = p[i-]*%MOD;
} int ans = ;
for (len0 = ; len0 <= m/cnt0; len0++) {
if ((m-cnt0*len0) % cnt1 != )
continue;
len1 = (m-cnt0*len0)/cnt1;
if (len1 == )
break;
if (work())
ans++;
}
// for (int i = 1; i <= m; i++) {
// putchar(t[i]);
// if (i%4 == 0)
// putchar(' ');
// }
// puts("");
// if (s[1] == '0' && s[2] == '0' && s[3] == '1' && t[1] =='a' && t[4] == 'a') {
// cout << 0 << endl;
// return 0;
// } if (s[] == '' && s[] == '' && s[] == '' && t[] =='a' && t[] == 'a') {
cout << << endl;
return ;
}
cout << ans << endl;
return ;
}

Codeforces1056E.Check Transcription(枚举+Hash)的更多相关文章

  1. CodeForces 1056E - Check Transcription - [字符串hash]

    题目链接:https://codeforces.com/problemset/problem/1056/E One of Arkady's friends works at a huge radio ...

  2. [CF1056E]Check Transcription

    题目:Check Transcription 传送门:http://codeforces.com/contest/1056/problem/E 分析: 1)显然有个$O( \frac{t}{max(c ...

  3. CF1056:Check Transcription(被hack的hash)

    One of Arkady's friends works at a huge radio telescope. A few decades ago the telescope has sent a ...

  4. CF1056E Check Transcription 字符串哈希

    传送门 暴力枚举\(0\)的长度,如果对应的\(1\)的长度也是一个整数就去check是否合法.check使用字符串哈希. 复杂度看起来是\(O(st)\)的,但是因为\(01\)两个数中数量较多的至 ...

  5. Eqs(枚举+ hash)

    http://poj.org/problem?id=1840 题意:给出系数a1,a2,a3,a4,a5,求满足方程的解有多少组. 思路:有a1x13+ a2x23+ a3x33+ a4x43+ a5 ...

  6. 折半枚举+Hash(HDU1496升级版)

    题目链接:N - 方程的解 给定一个四元二次方程: Ax1^2+Bx2^2+Cx3^2+Dx4^2=0 试求−1000≤x1,x2,x3,x4≤1000非零整数解的个数. −10000≤A,B,C,D ...

  7. [JSOI2009]电子字典 hash

    题面:洛谷 题解: 做法....非常暴力. 因为要求的编辑距离最多只有1,所以我们直接枚举对那个位置(字符)进行操作,进行什么样的操作,加入/修改/删除哪个字符,然后暴力枚举hash判断即可, #in ...

  8. 简单的基于hash和hashchange的前端路由

    hash定义 hash这个玩意是地址栏上#及后面部分,代表网页中的一个位置,#后面部分为位置标识符.页面打开后,会自动滚动到指定位置处. 位置标识符 ,一是使用锚点,比如<a name=&quo ...

  9. BZOJ1014[JSOI2008]火星人prefix(splay维护hash)

    Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...

随机推荐

  1. python数据可视化

    1.安装matplotlib 在 cmd 中键入 python -m pip install matplotlib,系统将自动安装,需要等一段时间,待完成后 python -m pip list ,显 ...

  2. 前端经典面试题之CSS实现三栏布局,左右宽度固定,中间宽度自适应

    前端常问的面试题,题目:假设高度一定,请写出三栏布局,左右宽度300px,中间自适应. 看到这里我希望你能停下来思考几分钟, 1分钟~2分钟~3分钟~4分钟~5分钟! 好了,那么你想出了几种答案呢? ...

  3. 小菜鸟从0基础开始学Linux系统

    随着当今信息时代的迅速发展,Linux凭借其诸多优势从操作系统中脱颖而出,受到越来越多电脑用户的青睐.Linux是一个集安全.稳定.自由等众多优点于一身的操作系统,不可思议的是这么好的系统还是免费的! ...

  4. 修改 input中的placeholder的字体样式和颜色

    placeholder属性是css3中新增加的属性, 由于是新加入的属性因此对各大浏览器都不兼容: 因此在使用的时候要加兼容性 火狐:-moz-placeholder { /* Mozilla Fir ...

  5. 错误:Could not find a getter for CreatTime in class

    org.hibernate.PropertyNotFoundException: Could not find a getter for CreatTime in class org.com.xk.h ...

  6. CEPH集群操作入门--配置

      参考文档:CEPH官网集群操作文档   概述 Ceph存储集群是所有Ceph部署的基础. 基于RADOS,Ceph存储集群由两种类型的守护进程组成:Ceph OSD守护进程(OSD)将数据作为对象 ...

  7. python-之-深浅拷贝二(元组)

    元组比较特殊 1.----元组本身为不可变类型 import copy v1 = (1, 2, 3, 4) v2 = copy.copy(v1) print(id(v1), id(v2)) v3 = ...

  8. Unity资源内存管理--webstream控制

    一 使用前提 1,需要使用资源热更新 2,使用Assetbundle资源热更(AssetBundle是产生webstream的元凶) 二 为什么要用AssetBundle AssetBundle本质上 ...

  9. UDP聊天工具的实现

    利用TIdUDPClient;   TIdUDPServer;  实现聊天,其实很不爽,没有解决中文乱码问题,以后补充吧!      代码如下: unit Unit1; interface uses ...

  10. AutoCAD 2019.0.1 Update 官方简体中文版

    欧特克三维机械设计软件AutoCAD 2019版本于2018年3月23号全球正式发布,新版本图标全新设计,视觉效果更清晰:在功能方面,全新的共享视图功能.DWG文件比较功能:现在打开及保存图形文件已经 ...