CF1056E Check Transcription 字符串哈希
暴力枚举\(0\)的长度,如果对应的\(1\)的长度也是一个整数就去check是否合法。check使用字符串哈希。
复杂度看起来是\(O(st)\)的,但是因为\(01\)两个数中数量较多的至少有\(\frac{|s|}{2}\)个,那么最多有\(\frac{2|t|}{|s|}\)个可能的答案,而每一次check是\(O(|s|)\)的,所以总复杂度是\(O(|t|)\)的
#include<bits/stdc++.h>
#define ll long long
#define PLL pair < ll , ll >
//This code is written by Itst
using namespace std;
inline int read(){
int a = 0;
char c = getchar();
bool f = 0;
while(!isdigit(c) && c != EOF){
if(c == '-')
f = 1;
c = getchar();
}
if(c == EOF)
exit(0);
while(isdigit(c)){
a = (a << 3) + (a << 1) + (c ^ '0');
c = getchar();
}
return f ? -a : a;
}
const int seed = 13331 , MOD1 = 1e9 + 7 , MOD2 = 1e9 + 9;
char s1[100010] , s2[1000010];
int L1 , L2 , cnt0 , cnt1 , cnt;
ll Hash[1000010][2] , poww[1000010][2];
void input(){
scanf("%s %s" , s1 + 1 , s2 + 1);
L1 = strlen(s1 + 1);
L2 = strlen(s2 + 1);
}
void init(){
for(int i = 1 ; i <= L1 ; ++i)
s1[i] == '0' ? ++cnt0 : ++cnt1;
poww[0][0] = poww[0][1] = 1;
for(int i = 1 ; i <= L2 ; ++i){
Hash[i][0] = (Hash[i - 1][0] * seed + s2[i]) % MOD1;
Hash[i][1] = (Hash[i - 1][1] * seed + s2[i]) % MOD2;
poww[i][0] = poww[i - 1][0] * seed % MOD1;
poww[i][1] = poww[i - 1][1] * seed % MOD2;
}
}
inline PLL calcHash(int i , int j){
return PLL((Hash[i + j - 1][0] - Hash[i - 1][0] * poww[j][0] % MOD1 + MOD1) % MOD1 , (Hash[i + j - 1][1] - Hash[i - 1][1] * poww[j][1] % MOD2 + MOD2) % MOD2);
}
void work(){
for(int i = 1 ; i * cnt0 < L2 ; ++i)
if(!((L2 - i * cnt0) % cnt1)){
int p = 1 , j = (L2 - i * cnt0) / cnt1;
bool f = 1;
PLL Hash0 = PLL(-1,0) , Hash1 = PLL(-1,0);
for(int k = 1 ; f && k <= L1 ; ++k)
if(s1[k] == '0'){
if(Hash0.first == -1)
Hash0 = calcHash(p , i);
else
if(Hash0 != calcHash(p , i))
f = 0;
p += i;
}
else{
if(Hash1.first == -1)
Hash1 = calcHash(p , j);
else
if(Hash1 != calcHash(p , j))
f = 0;
p += j;
}
cnt += f && (Hash0 != Hash1);
}
}
void output(){
cout << cnt;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("in" , "r" , stdin);
//freopen("out" , "w" , stdout);
#endif
input();
init();
work();
output();
return 0;
}
CF1056E Check Transcription 字符串哈希的更多相关文章
- [CF1056E]Check Transcription
题目:Check Transcription 传送门:http://codeforces.com/contest/1056/problem/E 分析: 1)显然有个$O( \frac{t}{max(c ...
- CodeForces 1056E - Check Transcription - [字符串hash]
题目链接:https://codeforces.com/problemset/problem/1056/E One of Arkady's friends works at a huge radio ...
- 从Hash Killer I、II、III论字符串哈希
首先,Hash Killer I.II.III是BZOJ上面三道很经典的字符串哈希破解题.当时关于II,本人还琢磨了好久,但一直不明白为啥别人AC的代码都才0.3kb左右,直到CYG神犇说可以直接随机 ...
- cf_514C(字符串哈希)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=121897#problem/G Watto and Mechanism Time ...
- Codeforces1056E.Check Transcription(枚举+Hash)
题目链接:传送门 题目: E. Check Transcription time limit per test seconds memory limit per test megabytes inpu ...
- 【CodeForces】961 F. k-substrings 字符串哈希+二分
[题目]F. k-substrings [题意]给定长度为n的串S,对于S的每个k-子串$s_ks_{k+1}...s_{n-k+1},k\in[1,\left \lceil \frac{n}{2} ...
- HDU6599 (字符串哈希+回文自动机)
题意: 求有多少个回文串的前⌈len/2⌉个字符也是回文串.(两组解可重复)将这些回文串按长度分类,分别输出长度为1,2,...,n的合法串的数量. 题解:https://www.cnblogs.co ...
- 字符串哈希——1056E
典型的字符串哈希题 字符串hash[i]:子串s[1-i]代表的值 要截取串s[l-r] 求hash[r]-hash[l-1]*F[len]即可 #include<bits/stdc++.h& ...
- poj 2774 字符串哈希求最长公共子串
Long Long Message #include <iostream> #include <algorithm> #include <cstdio> #incl ...
随机推荐
- link标签链接CSS和@import加载的区别
link:基本语法 <link rel="stylesheet" href="路径"> @import 基本语法 <style> @im ...
- python之网络通信协议
TCP/IP五层协议和OSI的七层协议: TCP和UDP的区别: Tcp协议:面向连接,数据可靠,传输效率低,面向字节流 Udp协议:面向无连接,数据不可靠,传输效率高,面向报文
- Ansible--常用模块使用(2)
Ansible常用模块 cron 模块 用途:cron模块⽤于设置定时任务,也⽤于管理定时任务中的环境变量使用方法: [root@ansible ~]# ansible-doc -s cron - n ...
- loadrunner 运行脚本-Run-time Settings-ContentCheck简单设置
运行脚本-Run-time Settings-ContentCheck简单设置 by:授客 QQ:1033553122 ContentCheck的设置可用来让VuGen检测存在错误的站点页面.如果被测 ...
- Flutter 布局(八)- Stack、IndexedStack、GridView详解
本文主要介绍Flutter布局中的Stack.IndexedStack.GridView控件,详细介绍了其布局行为以及使用场景,并对源码进行了分析. 1. Stack A widget that po ...
- Handle的原理(Looper、Handler、Message三者关系)
转载请注明出处:http://blog.csdn.net/lowprofile_coding/article/details/72580044 介绍 前面的内容对Handler做了介绍,也讲解了如何使 ...
- OneAPM大讲堂 | Java 异常日志记录最佳实践
[编者按]本文作者是 Casey Dunham.Casey 是一位具有 10 多年经验的专业软件开发人员,以其独特的方式应对应用安全问题而闻名.本文系国内 ITOM 管理平台 OneAPM 工程师编译 ...
- 洗礼灵魂,修炼python(37)--面向对象编程(7)—了解魔法方法
也许上一篇博文提到的property内置函数的用法你还觉得记忆犹新,那么有没有更多的类似的方法呢?有的,就是本篇文章要说的魔法方法 魔法方法 1.什么是魔法方法 (其实前面已经提到多次,每次我都是说暂 ...
- CSS| 學習心得
resize :both , 只有overflow設置為auto時, 才能起作用???
- SEVERE: An incompatible version 1.1.27 of the APR based Apache Tomcat Native library is installed, while Tomcat requires version 1.1.32
问题: SEVERE: An incompatible version 1.1.27 of the APR based Apache Tomcat Native library is installe ...