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 ;
}

Codeforces 825D Suitable Replacement - 贪心 - 二分答案的更多相关文章

  1. 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 ...

  2. Codeforces 830A. Office Keys (贪心二分 or DP)

    原题链接:http://codeforces.com/contest/830/problem/A 题意:在一条数轴上分别有n个人和k把钥匙(n<=k),以及一个目的地,每个人要各自拿到一个钥匙后 ...

  3. 洛谷P5021 赛道修建 NOIp2018 贪心+二分答案

    正解:贪心+LCA+二分答案 解题报告: 想先港下部分分qwq因为我部分分只拿到了10ptsQAQ(时间不够不是理由,其实还是太弱,所以要想很久,所以才时间不够QAQ m=1 找直径长度,完 一条链 ...

  4. Codeforces 551C GukiZ hates Boxes 二分答案

    题目链接 题意:  一共同拥有n个空地(是一个数轴,从x=1 到 x=n),每一个空地上有a[i]块石头  有m个学生  目标是删除全部石头  一開始全部学生都站在 x=0的地方  每秒钟每一个学生都 ...

  5. Educational Codeforces Round 25 D - Suitable Replacement(贪心)

    题目大意:给你字符串s,和t,字符串s中的'?'可以用字符串t中的字符代替,要求使得最后得到的字符串s(可以将s中的字符位置两两交换,任意位置任意次数)中含有的子串t最多. 解题思路: 因为知道s中的 ...

  6. CodeForces 779D. String Game(二分答案)

    题目链接:http://codeforces.com/problemset/problem/779/D 题意:有两个字符串一个初始串一个目标串,有t次机会删除初始串的字符问最多操作几次后刚好凑不成目标 ...

  7. Codeforces 553D Nudist Beach(二分答案 + BFS)

    题目链接 Nudist Beach 来源  Codeforces Round #309 (Div. 1) Problem D 题目大意: 给定一篇森林(共$n$个点),你可以在$n$个点中选择若干个构 ...

  8. Codeforces 1077D Cutting Out(二分答案)

    题目链接:Cutting Out 题意:给定一个n长度的数字序列s,要求得到一个k长度的数字序列t,每次从s序列中删掉完整的序列t,求出能删次数最多的那个数字序列t. 题解:数字序列s先转换成不重复的 ...

  9. 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/ ...

随机推荐

  1. 从零开始一起学习SLAM | 神奇的单应矩阵

    小白最近在看文献时总是碰到一个奇怪的词叫“homography matrix”,查看了翻译,一般都称作“单应矩阵”,更迷糊了.正所谓:“每个字都认识,连在一块却不认识”就是小白的内心独白.查了一下书上 ...

  2. StackExchange.Redis在net中使用

    redis 官网https://redis.io redis 下载  进入下载页面  https://redis.io/download https://github.com/MicrosoftArc ...

  3. springmvc注解式开发

    搭建环境 后端控制器无需实现接口,添加相应的注解 springmvc配置文件中无需注册controller springmvc配置文件中添加组件扫描器.注解驱动 涉及常用的注解 @controller ...

  4. Discuz! 安装模板、插件提示“对不起,您安装的不是正版应用...

    iscuz 社区在更新到2.0以上后,增加了对插件的版本检测,在安装时,可能会出现:“对不起,您安装的不是正版应用,安装程序无法继续执行”的提示,要解决这个其实挺容易的,找到以下文件: /source ...

  5. Maven的特点、优点-功能摘要

    Maven功能摘要 以下是Maven的主要特点: 遵循最佳实践的简单项目设置 - 在几秒钟内启动新项目或模块 所有项目的一致使用 - 意味着新开发人员进入项目的时间不会增加 卓越的依赖管理,包括自动更 ...

  6. 原型设计模式 Prototype

    参考1 http://www.cnblogs.com/libingql/p/3633377.html http://www.cnblogs.com/promise-7/archive/2012/06/ ...

  7. 2018年Android面试题含答案--适合中高级

    1.java中==和equals和hashCode的区别  基本数据类型的==比较的值相等. 类的==比较的内存的地址,即是否是同一个对象,在不覆盖equals的情况下,同比较内存地址,原实现也为 = ...

  8. tomcat1章1

    package ex01.pyrmont; import java.net.Socket; import java.net.ServerSocket; import java.net.InetAddr ...

  9. 安装ES6及HEAD插件

    1.下载相应npm包 es6地址:https://www.elastic.co/downloads/elasticsearch head插件地址:https://github.com/mobz/ela ...

  10. jdbc连接 orale 和 mysql 所需要的jar包

    oracle: ojdbc6-12.1.0.2.jar mysql: mysql-connector-java-5.1.47.jar