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/ ...
随机推荐
- 编写带有下列声明的例程:第一个例程是个驱动程序,它调用第二个例程并显示String str中的字符的所有排列。例如,str是"abc", 那么输出的串则是abc,acb,bac,bca,cab,cba,第二个例程使用递归。
全排列在笔试面试中很热门,因为它难度适中,既可以考察递归实现,又能进一步考察非递归的实现,便于区分出考生的水平.所以在百度和迅雷的校园招聘以及程序员和软件设计师的考试中都考到了,因此本文对全排列作下总 ...
- spring之继承配置
我们有一下两个类,并且Gradate类继承了Student类 public class Student public class Gradate extends Student 在applicatio ...
- Python记录5:函数1
函数 ''' 1. 什么是函数 在程序中具备某一功能的工具->函数 函数的使用必须遵循原则: 先定义 后调用 函数分为两大类: 1 ...
- Python全栈-网络编程基础
一.C/S架构 1.硬件C/S架构 如PC-打印机 2.软件C/S架构 如PC-网站服务器 参照: https://baike.baidu.com/item/Client%2FServer/15044 ...
- Mvcpager以下各节已定义,但尚未为布局页“~/Views/Shared/_Layout.cshtml”呈现:“Scripts”。
解决办法如下: 1.在_Layout.cshtml布局body内,添加section,Scripts.Render和RenderSection标签示例代码如下: <body class=&quo ...
- html5-盒子模型
/*div{background: green;width: 60%;padding-top: 10px;padding-right: 20px;padding-bottom: 30px;paddin ...
- poj2114 寻找树上存在长度为k点对,树上的分治
寻找树上存在长度为k点对,树上的分治 代码和 这个 差不多 ,改一下判断的就好 #include <iostream> #include <algorithm> #inc ...
- jQuery选择器--:first和:last
:first 概述 获取匹配的第一个元素 :last 概述 获取匹配的最后个元素 <!DOCTYPE html> <html> <head> <m ...
- A stock
1. 密集成交不太妙 主力抛压退为好
- 华为手机安装 charles 证书