Codeforces Round #545 (Div. 2)D(KMP,最长公共前后缀,贪心)
#include<bits/stdc++.h>
using namespace std;
const int N=1000007;
char s1[N],s2[N];
int len1,len2;
int nex[N];
int cnt1[7],cnt2[7];
int main(){
scanf("%s %s",s1+1,s2+1);
len1=strlen(s1+1);
len2=strlen(s2+1);
for(int i=1;i<=len1;i++)
cnt1[s1[i]-'0']++;
for(int i=1;i<=len2;i++)
cnt2[s2[i]-'0']++;
if(cnt1[1]>=cnt2[1]&&cnt1[0]>=cnt2[0]){//如果个数足够先输出s2
for(int i=1;i<=len2;i++)
putchar(s2[i]);
cnt1[0]-=cnt2[0];
cnt1[1]-=cnt2[1];
}
for(int i=2,j=0;i<=len2;i++){//kmp寻找最长公共前后缀
while(s2[i]!=s2[j+1]&&j)//这一位不相等就把模式串返回到标记
j=nex[j];
if(s2[i]==s2[j+1])//相等就让模式串位置前进,模式串可能为空此时不需要使模式串标记前进,因为模式串里面没有能和主串相等的字符
j++;
nex[i]=j;//记录标记,以便以后返回
}
while(cnt1[1]&&cnt1[0]){
int flag=0;
for(int i=nex[len2]+1;i<=len2;i++){//贪心地从最长公共前后缀的下一位开始输出
if(cnt1[s2[i]-'0']){
putchar(s2[i]);
cnt1[s2[i]-'0']--;
}
else{
flag=1;
break;
}
}
if(flag)
break;
}
for(int i=1;i<=cnt1[0];i++)//输出剩余的0或1
putchar('0');
for(int i=1;i<=cnt1[1];i++)//输出剩余的0或1
putchar('1');
}
Codeforces Round #545 (Div. 2)D(KMP,最长公共前后缀,贪心)的更多相关文章
- Codeforces Round #545 (Div. 1) 简要题解
这里没有翻译 Codeforces Round #545 (Div. 1) T1 对于每行每列分别离散化,求出大于这个位置的数字的个数即可. # include <bits/stdc++.h&g ...
- HDU 2594(求最长公共前后缀 kmp)
题意是在所给的两个字符串中找最长的公共前后缀,即第一个字符串前缀和第二个字符串后缀的最长相等串. 思路是将两个字符串拼接在一起,然后直接套用 kmp 算法即可. 要注意用 next 会报编译错误,改成 ...
- 【kmp+求所有公共前后缀长度】poj 2752 Seek the Name, Seek the Fame
http://poj.org/problem?id=2752 [题意] 给定一个字符串,求这个字符串的所有公共前后缀的长度,按从小到达输出 [思路] 利用kmp的next数组,最后加上这个字符串本身 ...
- Codeforces Round #545 (Div. 2) D 贪心 + kmp
https://codeforces.com/contest/1138/problem/D 题意 两个01串s和t,s中字符能相互交换,问最多能得到多少个(可交叉)的t 题解 即将s中的01塞进t中, ...
- Codeforces Round #545 (Div. 1) Solution
人生第一场Div. 1 结果因为想D想太久不晓得Floyd判环法.C不会拆点.E想了个奇奇怪怪的set+堆+一堆乱七八糟的标记的贼难写的做法滚粗了qwq靠手速上分qwqqq A. Skyscraper ...
- Codeforces Round #545 (Div. 2)-Camp Schedule
题目要求,给定一个s序列,一个p序列,问能不能对s做相应的调整,使得s序列中,有尽可能多的p子串(可以重复) 最开始我拿到这个题目,也是一点头绪都没有,如何做调整呢? 首先考虑如何会有尽可能多的子串, ...
- 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自闭记
A:求出该行该列各有多少个比其小的取max,该行该列各有多少个比其大的取max,加起来即可. #include<iostream> #include<cstdio> #incl ...
- Codeforces Round #545 (Div. 2) D
链接:http://codeforces.com/contest/1138/problem/D 啊啊啊啊啊啊,自闭啊,比赛的时候判断条件 if(s1[i-1]=='0') aa++;写成了 if(s1 ...
随机推荐
- centos 系统使用verdaccio搭建npm私库
.安装nodejs yum install -y nodejs 2.安装verdaccio npm install -g verdaccio --unsafe-perm 3.配置 a.修改配置文件 c ...
- 如何规避javascript多人开发函数重名问题
命名空间 封闭空间 js模块化mvc(数据层.表现层.控制层) seajs(如果了解的呢,可以说) 变量转换成对象的属性 对象化
- Python之购物商场
作业:购物商场 1.流程图 2.初始化用户账号存储文件 初始化存储一个空的用户账号字典,保存到文件 user.pkl.执行如下代码,即可初始化完成. #!/usr/bin/env python # - ...
- .net 连接ORACLE中文显示乱码解决方案
FYI由于历史的原因,早期的oracle没有中文字符集(如oracle6.oracle7.oracle7.1),但有的用户从那时起就使用数据库了, 并用US7ASCII字符集存储了中文,或是有的用户在 ...
- delphi 天气预报
天气预报 var astream : tmemorystream; sStream : TStringStream; jv : TJSONValue; begin astream := tmemory ...
- Physics Material
[Physics Material] 1. The Physics Material is used to adjust friction and bouncing effects of collid ...
- SQL2005 异常处理机制(Begin try Begin Catch)
将可能会出错的sql 写在begin try...end try 之间,若出错,刚程序就跳到紧接着的begin try...end try 的beign catch...end catch中,执行be ...
- 在Build Path中包含其他工程
------------siwuxie095 在 TestBuildPath 的 Build Path 中包含 SupportProje ...
- 476. Number Complement 二进制中的相反对应数
[抄题]: Given a positive integer, output its complement number. The complement strategy is to flip the ...
- opennebula kvm 创建虚拟机错误
Thu Jul :: : Error executing image transfer script: Error copying localhost.localdomain:/app/openneb ...