Codeforces 1137B(kmp next数组构造)
为了物尽其用,Next求出最多有哪部分能重复使用,然后重复使用就行了……
const int maxn = 5e5 + 5;
char s[maxn], t[maxn];
int cnts0, cnts1, cntt0, cntt1;
int Next[maxn];
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> (s + 1) >> (t + 1);
int lens = strlen(s + 1), lent = strlen(t + 1);
for (int i = 1; i <= lens; i++) {
if (s[i] == '1') cnts1++;
else cnts0++;
}
Next[1] = 0;
for (int i = 2, j = 0; i <= lent; i++) {
while (j > 0 && t[i] != t[j + 1]) j = Next[j];
if (t[i] == t[j + 1]) j++;
Next[i] = j;
}
for (int i = Next[lent] + 1; i <= lent; i++) {
if (t[i] == '1') cntt1++;
else cntt0++;
}
for (int i = 1; i <= lent; i++) {
if (t[i] == '1' && cnts1) cout << 1, cnts1--;
else if (t[i] == '0' && cnts0) cout << 0, cnts0--;
}
int k = min(cntt0 ? cnts0 / cntt0 : inf, cntt1 ? cnts1 / cntt1 : inf);
while (k--) {
for (int i = Next[lent] + 1; i <= lent; i++) {
if (t[i] == '1') cout << 1, cnts1--;
else cout << 0, cnts0--;
}
}
while (cnts1--) cout << 1;
while (cnts0--) cout << 0;
return 0;
}
Codeforces 1137B(kmp next数组构造)的更多相关文章
- Many Equal Substrings CodeForces - 1029A (kmp next数组应用)
题目大意 题目看样例也能猜到就是输出最短的循环串. 吐槽 明明是div3第一题为啥子还会用到kmp的知识? 解法 这个题仔细看发现是求最长可去除的后缀,也就是说去除跟下一个相同的字符串还能连接起来.这 ...
- HDU 1358 Period(KMP next数组运用)
Period Problem Description For each prefix of a given string S with N characters (each character has ...
- Common Divisors CodeForces - 182D || kmp最小循环节
Common Divisors CodeForces - 182D 思路:用kmp求next数组的方法求出两个字符串的最小循环节长度(http://blog.csdn.net/acraz/articl ...
- HDU 3333 | Codeforces 703D 树状数组、离散化
HDU 3333:http://acm.hdu.edu.cn/showproblem.php?pid=3333 这两个题是类似的,都是离线处理查询,对每次查询的区间的右端点进行排序.这里我们需要离散化 ...
- [leetcode] 根据String数组构造TreeNode,用于LeetCode树结构相关的测试用例
LeetCode 跟树结构相关的题目的测试用例中大多是通过String数组来构造树.例如{2,#,3,#,4,#,5,#,6},可以构造出如下的树(将树结构逆时针选择90度显示): 6 ...
- POJ2406 Power Strings(KMP,后缀数组)
这题可以用后缀数组,KMP方法做 后缀数组做法开始想不出来,看的题解,方法是枚举串长len的约数k,看lcp(suffix(0), suffix(k))的长度是否为n- k ,若为真则len / k即 ...
- POJ 2406 KMP/后缀数组
题目链接:http://poj.org/problem?id=2406 题意:给定一个字符串,求由一个子串循环n次后可得到原串,输出n[即输出字符串的最大循环次数] 思路一:KMP求最小循环机,然后就 ...
- PHP利用数组构造JSON
问题起因 以往都是直接用构造数组的形式构造json 例子: $arr = array("A"=>"1","B"=>"2 ...
- kmp next数组的理解(挺好的一篇文章 ,原来kmp最初的next是这样的啊,很好理解)
KMP算法的next[]数组通俗解释 我们在一个母字符串中查找一个子字符串有很多方法.KMP是一种最常见的改进算法,它可以在匹配过程中失配的情况下,有效地多往后面跳几个字符,加快匹配速度. 当然我 ...
随机推荐
- platform_set_drvdata和platform_get_drvdata用法【转】
本文转载自:http://www.cnblogs.com/wangxianzhen/archive/2013/04/09/3009530.html 在用到Linux设备驱动的platform框架时,常 ...
- Java中锁的内存语义
我们都知道,Java中的锁可以让临界区互斥执行.锁是Java并发编程中最重要的同步机制,锁除了可以让临界区互斥执行外,还可以让释放锁的线程向获取同一个锁的线程发送消息.下面是锁的释放-获取的代码: C ...
- jsp报An error has occurred. See error log for more details. Argument not valid错误
An error has occurred. See error log for more details. Argument not valid 翻译过来是:一个错误已经发生.看到更多的细节错误日志 ...
- hdu 1280 前m大的数(排序)
题意:排序 思路:排序 #include<iostream> #include<stdio.h> #include<algorithm> using namespa ...
- easyui-tabs 在ie8下基于iframe嵌套页面加载成功后切换空白问题
这是一个很坑的问题,由于项目必须支持ie8的情况下,产生了这个问题.在我进行逐步对比的分析过后,终于发现了原因所在:
- 修改SO
1.如果在jni中定义的是int型的数据,比如: JNIEXPORT jint JNICALL Java_com_ggndktest1_JniGg_getCoin (JNIEnv * env, job ...
- Kvm学习文档记录
一.实验环境: 在vmware上打开vt支持选项: 操作系统信息: [root@node2 ~]# cat /etc/issue CentOS release 6.6 (Final) Kernel \ ...
- IntelliJ IDEA 2018 设置代码提示对大小写不敏感
setting->Editor->General->Code Completion取消勾选Match case
- Persistent connections CONN_MAX_AGE django
Persistent connections¶ Persistent connections avoid the overhead of re-establishing a connection to ...
- PHP mysql_fetch_array与mysql_fetch_row的区别
如果你的表里面有字段a,b,c那么你用mysql_fetch_row() 就返回array(1=>a的值,2=>b的值,3=>c的值)这个时候你读数组的话,只能这样写$array[1 ...