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.

Input

The first line contains string s (1 ≤ |s| ≤ 106).

The second line contains string t (1 ≤ |t| ≤ 106).

Output

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.

Examples
Input
?aa?
ab
Output
baab
Input
??b?
za
Output
azbz
Input
abcd
abacaba
Output
abcd
Note

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 ;
}
上一篇:Android -- 程序判断手机ROOT状态,获取ROOT权限


下一篇:史上最简单的SpringCloud教程 | 第二篇: 服务消费者(rest+ribbon)