CF1138D.Camp Schedule
传送门
虽然是D,但是还是Sb题,把模式串跑一遍KMP,然后把按顺序放,每次放完之后跳到对应的前缀,继续放。
如果最后1的数量还有剩,再将最后的位数全部放1
代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
void read(int &x) {
char ch; bool ok;
for(ok=0,ch=getchar(); !isdigit(ch); ch=getchar()) if(ch=='-') ok=1;
for(x=0; isdigit(ch); x=x*10+ch-'0',ch=getchar()); if(ok) x=-x;
}
#define rg register
const int maxn=5e5+10;
char p[maxn],s[maxn];
int num,num1,len,len1,nxt[maxn],ans;
int main()
{
scanf("%s",p+1),scanf("%s",s+1);
len=strlen(p+1),len1=strlen(s+1);
for(rg int i=1;i<=len;i++)if(p[i]=='1')num++;
for(rg int i=1;i<=len1;i++)if(s[i]=='1')num1++;
if(num<num1||len<len1)
{
for(rg int i=1;i<=len;i++)printf("%c",p[i]);
printf("\n");return 0;
}
for(rg int i=2,j=0;i<=len1;i++)
{
while(j&&s[j+1]!=s[i])j=nxt[j];
if(s[j+1]==s[i])j++;
nxt[i]=j;
}
ans=len+1;
for(rg int i=1,j=0;i<=len;i++)
{
if(num&&s[j+1]=='1')num--,p[i]='1',j++;
else if(s[j+1]=='0')j++,p[i]='0';
else {ans=i;break;}
if(j==len1)j=nxt[j];
}
for(rg int i=ans;i<=len;i++)p[i]='0';
while(num)
{
ans--;
if(p[ans]=='1')num++;
num--,p[ans]='1';
}
for(rg int i=1;i<=len;i++)printf("%c",p[i]);
}
CF1138D.Camp Schedule的更多相关文章
- Codeforces Round #545 (Div. 2)-Camp Schedule
题目要求,给定一个s序列,一个p序列,问能不能对s做相应的调整,使得s序列中,有尽可能多的p子串(可以重复) 最开始我拿到这个题目,也是一点头绪都没有,如何做调整呢? 首先考虑如何会有尽可能多的子串, ...
- Codeforces Round #545 (Div. 2)(D. Camp Schedule)
题目链接:http://codeforces.com/contest/1138/problem/D 题目大意:给你两个字符串s1和s2(只包含0和1),对于s1中,你可以调换任意两个字符的位置.问你最 ...
- 【Codeforces 1137B】Camp Schedule
Codeforces 1137 B 题意:给两个串\(S\).\(T\),问将\(S\)中字符任意调换后出现\(T\)次数最多的方案. 思路:我们首先考虑怎么样放\(T\)才是最优的.我们直观上考虑前 ...
- CodeForces Round #545 Div.2
A. Sushi for Two 代码: #include <bits/stdc++.h> using namespace std; ; ; int a[maxn], vis[maxn]; ...
- Codeforces Round #545 (Div. 1) Solution
人生第一场Div. 1 结果因为想D想太久不晓得Floyd判环法.C不会拆点.E想了个奇奇怪怪的set+堆+一堆乱七八糟的标记的贼难写的做法滚粗了qwq靠手速上分qwqqq A. Skyscraper ...
- Codeforces Round #545 Div1 题解
Codeforces Round #545 Div1 题解 来写题解啦QwQ 本来想上红的,结果没做出D.... A. Skyscrapers CF1137A 题意 给定一个\(n*m\)的网格,每个 ...
- Codeforces Round #545 (Div. 1)
本来开头两道题写得挺快的,然后第三题想了一会儿胡出一个scc计算gcd的做法,写了一发凭借信仰交了上去结果一发pp了?然后第四题沙雕了想了很久才会,于是罚时瞬间变多.结果后面两题都没时间看了,赛后感觉 ...
- CodeForces Contest #1137: Round #545 (Div. 1)
比赛传送门:CF #1137. 比赛记录:点我. 每次都自闭的 div1 啊,什么时候才能上 IM 呢. [A]Skyscrapers 题意简述: 有一个 \(n\times m\) 的矩阵 \(a_ ...
- Codeforces Round #545 (Div. 2) 题解
题目链接 A. Sushi for Two 题意 在一个 01 序列中找出长为偶数的连续的一段使得它前一半和后一半内部分别相同,而前一半和后一半不同. \(2\le n\le 100\ 000\) 题 ...
随机推荐
- define tensorflow and run it
import tensorflow as tf a, b, c, d, e = tf.constant(5, name='input_a'), tf.constant(6, name='input_b ...
- Javascript的参数详解
函数可以有参数也可以没有参数,如果定义了参数,在调用函数的时候没有传值,默认设置为undefined 在调用函数时如果传递参数超过了定义时参数,jS会忽略掉多余参数 jS中不能直接写默认值,可以通过a ...
- 20170316 ABAP注意点
1.debug 时在MODIFY db from table 后数据便提交了: 一般情况下,更新数据库需要commit,但debug会自动commit,程序结束也会自动commit. 2.使用at n ...
- 使用libcurl进行HTTP GET操作
Working example how to do a GET request with libcurl and save it to a string variable for future use ...
- 重装系统后texstudio拼写检查不工作
重装texstudio还是不行. 后来发现是重装系统后用户名和以前的系统用户明不一样,导致系统盘里的用户文件夹路径不一样.而texstudio的字典存放在用户路径文件夹下 C:\Users\xxx\A ...
- Understand JavaScript’s “this” With Clarity, and Master It
The this keyword in JavaScript confuses new and seasoned JavaScript developers alike. This article a ...
- springmvc配置一:ajax请求防止返回中文乱码配置说明
Spring3.0 MVC @ResponseBody 的作用是把返回值直接写到HTTP response body里. Spring使用AnnotationMethodHandlerAdapter的 ...
- 洛谷 P4660 & bzoj 1168 [ Baltic OI 2008 ] 手套 —— 分析+单调栈
题目:https://www.luogu.org/record/show?rid=12702916 https://www.lydsy.com/JudgeOnline/problem.php?id=1 ...
- Ubuntu18.04 安装 JDK7
直接下载jdk压缩包方式安装 1.官网下载JDK 地址: http://www.oracle.com/technetwork/articles/javase/index-jsp-138363 ...
- 【旧文章搬运】KeUserModeCallback用法详解
原文发表于百度空间及看雪论坛,2010-01-10 看雪论坛地址:https://bbs.pediy.com/thread-104918.htm 代码及附件可到这里下载=============== ...