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的更多相关文章
随机推荐
- 《名侦探柯南》动画登陆bilibili
不管你看没看过.喜不喜欢,也一定听说过<名侦探柯南>这部动画,它和<火影>.<海贼王>几部动画陪伴了一代人成长的道路,而且<名侦探柯南>还是这几部动画中 ...
- Nginx SSL/HTTPS 配置
使用OpenSSL生成证书 1.生成RSA密钥的方法 openssl genrsa -des3 -out privkey.pem 2048 这个命令会生成一个2048位的密钥,同时有一个des3方法加 ...
- CodeForces - 1047CEnlarge GCD(这题很难,快来看题解,超级详细,骗浏览量)
C. Enlarge GCD time limit per test1 second memory limit per test256 megabytes inputstandard input ou ...
- P1468 派对灯 Party Lamps(BIG 模拟)
题目描述 在IOI98的节日宴会上,我们有N(10<=N<=100)盏彩色灯,他们分别从1到N被标上号码. 这些灯都连接到四个按钮: 按钮1:当按下此按钮,将改变所有的灯:本来亮着的灯就熄 ...
- 图论--LCA--树上倍增法(在线)
/* * LCA在线算法(倍增法) */ const int MAXN = 10010; const int DEG = 20; struct Edge { int to, next; } edge[ ...
- E - 梦幻岛宝珠 HYSBZ - 1190 变形01背包 难
E - 梦幻岛宝珠 HYSBZ - 1190 这个题目我觉得很难,看题解都看了很久. 首先可以得到一个大概的思路就是分组,每一个数都可以分成 a*2^b 所以把b相同的数都分成一个组. 在每一组内部 ...
- Codeforces Round #563 (Div. 2) A-D
A. Ehab Fails to Be Thanos 这个A题很简单,就是排个序,然后看前面n个数和后面的n个数是不是相同,相同就输出-1 #include <cstdio> #inclu ...
- 向Redis里存入数据
实现思路:1. 从Redis缓存获取URL统计网址清单2. 逐条拼凑SQL统计语句,暂时不能支持批量计算,因为按单个网址统计.3. 发送到HIVE JDBC执行SQL并等待返回结果4 ...
- .Net Core WPF之XAML概述
原文链接,机器翻译,有误处参看原文. XAML overview in WPF 2019/08/08 What is XAML XAML syntax in brief Case and white ...
- 【Elasticsearch学习】文档搜索全过程
在ES执行分布式搜索时,分布式搜索操作需要分散到所有相关分片,若一个索引有3个主分片,每个主分片有一个副本分片,那么搜索请求会在这6个分片中随机选择3个分片,这3个分片有可能是主分片也可能是副本分片, ...