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的更多相关文章
随机推荐
- 如何在mysql中实现自然排序
背景 熟悉mysql的同学应该清楚,mysql在对字符串做order by排序时是按照字典序进行排序的,但是如果字符串中包含数字的话(我们称这种类型的字符串为alphanumeric),仅按照字典序的 ...
- Python抓取新浪新闻数据(二)
以下是抓取的完整代码(抓取了网页的title,newssource,dt,article,editor,comments)举例: 转载于:https://blog.51cto.com/2290153/ ...
- 24-Java-Spring框架(二)
Spring框架的了解.SpringIOC的部分内容请阅读23-Java-Spring框架(二) 三.Spring Web MVC(Model View Controller) 1.SpringMVC ...
- mysql查询表内所有字段名和备注
select distinct column_name as 字段名,column_comment as 字段备注 from information_schema.columns where tabl ...
- UML笔记之类图
1.类与类之间关系在UML类图中,常见的有以下几种关系: 泛化(Generalization), 实现(Realization),关联(Association),聚合(Aggregation),组合( ...
- 3-MyBatisPlus教程(CRUD-上)
1,增加MP日志配置 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:m ...
- 谈谈Spring bean的生命周期(一)
简介 本片文章主要讲Spring IOC容器中 bean 的生命周期 Spring bean 生命周期 Spring 中bean的声明周期 可以分为如下4个阶段: 实例化阶段--Instantiati ...
- apply call bind的用法与实现
概念 apply call 和bind 允许为不同的对象分配和调用属于一个对象的函数/方法.同时它们可以改变函数内 this 的指向. 区别 apply 和 call 接收的参数形式不同 apply ...
- Excel非常规快捷键
Windows+V,调出剪贴板,是常规快捷键,鼠标右键-W-F,新建文件夹,这是非常规快捷键. 掌握Excel大半菜单和三五十快捷键,Excel也算入门了.对Excel快捷键的学习,其一是常规快捷键, ...
- 如何为Linux服务器添加磁盘
Linux服务器如果磁盘不够用了,就需要增加新的磁盘,磁盘添加到使用通常有4个步骤.其中第一个步骤虚拟机和实体服务器有差别,后面三个步骤都是相同的,这里以VMWare虚拟机来进行演示如何添加磁盘. ( ...