codeforces825 D. Suitable Replacement 二分答案

来源:互联网 发布:三维设计软件 编辑:程序博客网 时间:2024/06/04 22:07
D. Suitable Replacement
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 stringss, you choose the one with the largest number ofnon-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 thesuitability 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
abcdabacaba
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 is1 and there are several dozens of such strings,"azbz" is just one of them.

In the third example there are no '?' characters and thesuitability of the string is 0.


题意:给S ,T两个字符串,让你输出一个和S相同长度的串,并将S中的?改为小写字母,满足输出串中T串数量最多

思路:二分答案,模拟太烦人,搞得我乱死了(主要是太弱),先二分出最多有几个T串,然后输出的时候判断S串中哪个字母不够就输出,最后全够了就随便输出一个字母。

还有注意long long,我的一下午贡献给这题也是没谁了


#include <bits/stdc++.h>#define LL long longusing namespace std;const int AX = 1e6+66;char a[AX];char b[AX];int numa[27];int numb[27];int num;int lena,lenb;bool check(int x){          //不用long long wa到死LL c=0;for(int i = 0 ; i < 26 ; i ++ )  if( numb[i] ){c += max(0LL,1LL*x*numb[i]-numa[i]);}return c <= num;}int main(){num = 0;gets(a);gets(b);lena = strlen(a);lenb = strlen(b);for( int i = 0 ; i < lena ; i++ ){if( a[i] >= 'a' && a[i] <= 'z' )numa[a[i]-'a'] ++;else num ++ ;}for( int i = 0 ; i < lenb ; i++ ){numb[b[i]-'a'] ++;}int  l = 0 , r = AX , x = 0;while( l <= r ){int mid = (l+r)>>1;if( check(mid) ) { l = mid + 1 ; x = mid ; }else r = mid - 1;}int id = 0;for( int i = 0 ; i < lena ; i++ ){         if( a[i] != '?' ) printf("%c",a[i]);else{while( id < 26 && numa[id] >= numb[id]*x ) id++;if( id < 26 ){printf("%c", id + 'a');numa[id]++;}else{putchar('a');}}}return 0;}


原创粉丝点击