Codeforces 825D Suitable Replacement - 贪心 - 二分答案
You are given two strings s and t consisting of small Latin letters, string s can also contain '?' characters.
Suitability of string s is calculated by following metric:
Any two letters can be swapped positions, these operations can be performed arbitrary number of times over any pair of positions. Among all resulting strings s, you choose the one with the largest number of non-intersecting occurrences of string t. Suitability is this number of occurrences.
You should replace all '?' characters with small Latin letters in such a way that the suitability of string s is maximal.
The first line contains string s (1 ≤ |s| ≤ 106).
The second line contains string t (1 ≤ |t| ≤ 106).
Print string s with '?' replaced with small Latin letters in such a way that suitability of that string is maximal.
If there are multiple strings with maximal suitability then print any of them.
?aa?
ab
baab
??b?
za
azbz
abcd
abacaba
abcd
In the first example string "baab" can be transformed to "abab" with swaps, this one has suitability of 2. That means that string "baab" also has suitability of 2.
In the second example maximal suitability you can achieve is 1 and there are several dozens of such strings, "azbz" is just one of them.
In the third example there are no '?' characters and the suitability of the string is 0.
题目大意 给定串s和串t,s中包含小写字符和通配符'?'(可以代替一个小写字母),t中只包含小写字母,你可以多次交换s中任意两个字母,使得t在s中出现的次数尽可能多。输出一组最优解的s。
看到了多次我还以为读错题了(我高估了这道题的难度)。
因为和顺序无关,那么有关的就只有各个字母出现的数量。
所以二分t在s中不想交出现的次数(有相交不好判断),判断是显然的。
注意check的时候计数要用long long,否则会炸int。
Code
/**
* Codeforces
* Problem#825D
* Accepted
* Time: 31ms
* Memory: 4008k
*/
#include <bits/stdc++.h>
using namespace std;
typedef bool boolean; const int limit = 1e6;
char s[limit + ], t[limit + ]; inline void init() {
gets(s);
gets(t);
} int ca = ;
int cs[], ct[];
boolean check(int mid) {
long long c = ;
for(int i = ; i < && c <= ca; i++)
c += (cs[i] >= ct[i] * 1LL * mid) ? () : (ct[i] * 1LL * mid - cs[i]);
return c <= ca;
} inline void solve() {
memset(cs, , sizeof(cs));
memset(ct, , sizeof(ct));
for(int i = ; s[i]; i++)
(s[i] >= 'a' && s[i] <= 'z') ? (cs[s[i] - 'a']++) : (ca++);
if(!ca) {
puts(s);
return;
}
for(int i = ; t[i]; i++)
ct[t[i] - 'a']++;
int l = , r = 1e6;
while(l <= r) {
int mid = (l + r) >> ;
if(check(mid)) l = mid + ;
else r = mid - ;
}
int ans = l - ;
for(int i = ; i < ; i++)
ct[i] = max(ct[i] * ans - cs[i], );
int p = ;
for(int i = ; s[i]; i++) {
while(p < && ct[p] == ) p++;
if(s[i] == '?')
s[i] = p + 'a', ct[p]--;
}
puts(s);
} int main() {
init();
solve();
return ;
}
Codeforces 825D Suitable Replacement - 贪心 - 二分答案的更多相关文章
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案 - 动态规划
There are n people and k keys on a straight line. Every person wants to get to the office which is l ...
- Codeforces 830A. Office Keys (贪心二分 or DP)
原题链接:http://codeforces.com/contest/830/problem/A 题意:在一条数轴上分别有n个人和k把钥匙(n<=k),以及一个目的地,每个人要各自拿到一个钥匙后 ...
- 洛谷P5021 赛道修建 NOIp2018 贪心+二分答案
正解:贪心+LCA+二分答案 解题报告: 想先港下部分分qwq因为我部分分只拿到了10ptsQAQ(时间不够不是理由,其实还是太弱,所以要想很久,所以才时间不够QAQ m=1 找直径长度,完 一条链 ...
- Codeforces 551C GukiZ hates Boxes 二分答案
题目链接 题意: 一共同拥有n个空地(是一个数轴,从x=1 到 x=n),每一个空地上有a[i]块石头 有m个学生 目标是删除全部石头 一開始全部学生都站在 x=0的地方 每秒钟每一个学生都 ...
- Educational Codeforces Round 25 D - Suitable Replacement(贪心)
题目大意:给你字符串s,和t,字符串s中的'?'可以用字符串t中的字符代替,要求使得最后得到的字符串s(可以将s中的字符位置两两交换,任意位置任意次数)中含有的子串t最多. 解题思路: 因为知道s中的 ...
- CodeForces 779D. String Game(二分答案)
题目链接:http://codeforces.com/problemset/problem/779/D 题意:有两个字符串一个初始串一个目标串,有t次机会删除初始串的字符问最多操作几次后刚好凑不成目标 ...
- Codeforces 553D Nudist Beach(二分答案 + BFS)
题目链接 Nudist Beach 来源 Codeforces Round #309 (Div. 1) Problem D 题目大意: 给定一篇森林(共$n$个点),你可以在$n$个点中选择若干个构 ...
- Codeforces 1077D Cutting Out(二分答案)
题目链接:Cutting Out 题意:给定一个n长度的数字序列s,要求得到一个k长度的数字序列t,每次从s序列中删掉完整的序列t,求出能删次数最多的那个数字序列t. 题解:数字序列s先转换成不重复的 ...
- Codeforces Round #307 (Div. 2) C. GukiZ hates Boxes 贪心/二分
C. GukiZ hates Boxes Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/551/ ...
随机推荐
- aop编程之前置通知
aop( Aspect-Oriented Programming)前置通知原理案例讲解 编程步骤: 定义接口 编写对象(被代理的对象即目标对象) 编写通知(前置通知即目标方法调用前调用) 在beans ...
- Python记录1:基础知识常识
今日内容: 一,Python的数据类型 Python一共有以下几种常见的数据类型:int(整形) float(浮点型) str(字符串) list(列表) tuple元组 dict(字典) ...
- c#之如何正确地实现IDisposable接口
见实例: public class TestClass : IDisposable { //供程序员显式调用的Dispose方法 public void Dispose() { //调用带参数的Dis ...
- Quick-Cocos2d-x文件结构分析
在上一章我们讲过了Quick-Cocos2d-x中的环境搭建,这章我们分析下quick中的文件结构吧!打开quick的文件夹,可以看到如下的这些目录和文件: bin:存放各种与引擎相关的脚本 comp ...
- zookeeper开发
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 zookeeper-3.4.11 ZK客户端操作命令: #登 ...
- 【Hadoop学习之六】MapReduce原理
一.概念MapReduce:"相同"的key为一组,调用一次reduce方法,方法内迭代这一组数据进行计算 块.分片.map.reduce.分组.分区之间对应关系block > ...
- JS实现document.ready
通常我们想要在页面内容加载完成后运行 JS 时,都会使用 window.onload 来处理,比如: window.onload = function(){ alert('Hello World!') ...
- ubuntu重启+sublime快捷键
sudo reboot Sublime常用快捷键: 掌握基本的代码编辑器的快捷键,能让你打码更有效率,刚开始可能不大记得住,多敲几次就能熟悉并使用它 精华键 : Ctrl+Shift+P:打开命令面板 ...
- MyEclipse10.7安装Aptana后重启:An internal error has occurred. No more handles [Could not detect registered XULRunner to use]
问题描述: 当安装Aptana插件后重启MyEclipse10.7,发生错误: An internal error has occurred. No more handles [Could not d ...
- MyEclipse如何清除废弃的工作空间
1.MyEclipse如何清除废弃的工作空间Windows--->Preferences--->General--->Startup and Shutdown--->Works ...