Codeforces 825D Suitable Replacement - 贪心 - 二分答案
Posted 阿波罗2003
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
1 /** 2 * Codeforces 3 * Problem#825D 4 * Accepted 5 * Time: 31ms 6 * Memory: 4008k 7 */ 8 #include <bits/stdc++.h> 9 using namespace std; 10 typedef bool boolean; 11 12 const int limit = 1e6; 13 char s[limit + 5], t[limit + 5]; 14 15 inline void init() { 16 gets(s); 17 gets(t); 18 } 19 20 int ca = 0; 21 int cs[27], ct[27]; 22 boolean check(int mid) { 23 long long c = 0; 24 for(int i = 0; i < 26 && c <= ca; i++) 25 c += (cs[i] >= ct[i] * 1LL * mid) ? (0) : (ct[i] * 1LL * mid - cs[i]); 26 return c <= ca; 27 } 28 29 inline void solve() { 30 memset(cs, 0, sizeof(cs)); 31 memset(ct, 0, sizeof(ct)); 32 for(int i = 0; s[i]; i++) 33 (s[i] >= ‘a‘ && s[i] <= ‘z‘) ? (cs[s[i] - ‘a‘]++) : (ca++); 34 if(!ca) { 35 puts(s); 36 return; 37 } 38 for(int i = 0; t[i]; i++) 39 ct[t[i] - ‘a‘]++; 40 int l = 1, r = 1e6; 41 while(l <= r) { 42 int mid = (l + r) >> 1; 43 if(check(mid)) l = mid + 1; 44 else r = mid - 1; 45 } 46 int ans = l - 1; 47 for(int i = 0; i < 26; i++) 48 ct[i] = max(ct[i] * ans - cs[i], 0); 49 int p = 0; 50 for(int i = 0; s[i]; i++) { 51 while(p < 25 && ct[p] == 0) p++; 52 if(s[i] == ‘?‘) 53 s[i] = p + ‘a‘, ct[p]--; 54 } 55 puts(s); 56 } 57 58 int main() { 59 init(); 60 solve(); 61 return 0; 62 }
以上是关于Codeforces 825D Suitable Replacement - 贪心 - 二分答案的主要内容,如果未能解决你的问题,请参考以下文章
Educational Codeforces Round 25 D - Suitable Replacement(贪心)