CF551B
题目链接:http://codeforces.com/contest/551/problem/B
题目大意:给出字符串a, b, c。试图合理的安排a的字母顺序,使其中有尽可能多的与 c 或 b 相同的不重叠的子串.。
解题思路:You just need some power! 记录 a, b, c 中各个字母的个数,然后枚举 b 能出现的所有次数,再算出 c 相应的能出现的次数,两者相加即为一个ans,最终答案即为ans最大的时候对应的情况。最坏情况下的时间复杂度:O(|a| * 26).
AC代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<functional>
#include<list>
#include<map>
#include<istream>
#include<ostream>
#include<queue>
#include<set>
#include<sstream>
#include<string>
#include<stack>
#include<string>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int maxn=+,inf=0x7ffffff;
int ca[],cb[],cc[];
int main(){
string a,b,c;
cin>>a;
cin>>b;
cin>>c;
for(int i=;i<a.size();i++) ca[a.at(i)-'a']++;
for(int i=;i<b.size();i++) cb[b.at(i)-'a']++;
for(int i=;i<c.size();i++) cc[c.at(i)-'a']++;
bool ending=false;
int ans=;
P as;
for(int i=;;i++){
int ta=;
int tempa[];
for(int j=;j<;j++)
tempa[j]=ca[j];
for(int j=;j<;j++){
if(tempa[j]<cb[j]*i){
ending=true;
break;
}
tempa[j]-=(cb[j]*i);
}
if(ending) break;
ta+=i;
int ti=inf;
for(int j=;j<;j++){
int ttt;
if(cc[j]>){
int ttt=tempa[j]/cc[j];
ti=min(ti,ttt);
}
}
if(ti!=inf){
ta+=ti;
}
if(ta>ans){
ans=ta;
if(ti!=inf)
as=make_pair(i,ti);
else
as=make_pair(i,);
}
}
int num_b=as.first,num_c=as.second;
for(int i=;i<;i++){
ca[i]-=(cb[i]*num_b);
ca[i]-=(cc[i]*num_c);
}
string ret;
while(num_b>){
ret+=b;
num_b--;
}
while(num_c>){
ret+=c;
num_c--;
}
for(int i=;i<;i++){
while(ca[i]>){
ret+=(i+'a');
ca[i]--;
}
}
cout<<ret<<endl;
return ;
}
CF551B的更多相关文章
随机推荐
- 7.JUC线程高级-生产消费问题&虚假唤醒
描述 生产消费问题在java多线程的学习中是经常遇到的问题 ,多个线程共享通一个资源的时候会出现各种多线程中经常出现的各种问题. 实例说明 三个类:售货员Clerk,工厂Factory,消费者Cons ...
- Vs Code在Vue项目中v-for指令提示错误的解决办法
最近在做一个Vue项目,在其中用到v-for指令时,发现Vs Code报错,如下图(代码是没有任何问题的),在网上找了一下解决办法,希望能帮助到更多人. 解决方法: 打开 文件-首选项-设置 将 ...
- 题解 CF160B 【Unlucky Ticket】
本文为UserUnknown原创 思路 这道题应该怎么做? 可以把输入的数字逐位拆分后存入数组,就像这样存进去: int a[N],b[N] tmp=n; k=1; while(--tmp){ a[k ...
- Top 命令数据分析
一.top 命令详解 当前时间 20:27:12 当前系统运行时间 3:18秒 1个用户 系统负载平均长度为 0.00,0.00,0.00(分别为1分钟.5分钟.15分钟前到现在的平均值) 第二行为进 ...
- SSM框架完整开发流程
----------------第一阶段-------------- 1.数据库建模 2.生成sql语句 3.在mysq客户端使用命令方式执行sql脚本,生成数据库 4.允许远程访问mysql GRA ...
- 《Docker从入门到跑路》之存储卷介绍
默认情况下,容器会随着用户删除而消失,包括容器里面的数据.如果我们要对容器里面的数据进行长久保存,就不得不引用存储卷的概念. 在容器中管理数据持久化主要有两种方式:1.数据卷(data volumes ...
- 如何使用Golang实现一个API网关
你是否也存在过这样的需求,想要公开一个接口到网络上.但是还得加点权限,否则被人乱调用就不好了.这个权限验证的过程,最好越简单越好,可能只是对比两个字符串相等就够了.一般情况下我们遇到这种需要,就是在函 ...
- struts2入门教学
我的博客地址:https://blog.csdn.net/qq_41907991 首先介绍一下struts2使用的基本步骤: 1.导入相关的 jar 文件 2.需要在 web.xml 文件中配置一个 ...
- python执行脚本报错: Non-ASCII character '\xe4' in file yang.py on line 1, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
这是因为我们在写python程序的时候,无论在注释里面出现中文,还是在代码里面出现中文,也就是说只要这个python程序里面有中文,我们就不能直接用python2来进行执行,如果安装是python3可 ...
- 微信小程序-swiper(轮播图)抖动问题
ps:问题 组件swiper(轮播图)真机上不自动滚动 一直卡在那里抖动 以前遇到这个问题,官方一直没有正面回复.就搁置了,不过有大半年没写小程序了也没去关注,今天就去看了下官方文档,发觉更新了点好东 ...