CodeForces - 825D

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lpeaceminusone/article/details/83110459

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. Suitabilityis 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串。

#include<set>
#include<map>
#include<stack>
#include<queue>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define ll long long
#define inf 0x3f3f3f
using namespace std;
string s1,s2,s3,s4;
const int maxn=2005;
int vis1[maxn];
int vis2[maxn];
int a;
bool judge(int x){
    int b=0;
    for(int i='a';i<='z';i++){
       if(vis2!=0){
        b+=max(0,x*vis2[i]-vis1[i]);
       }
    }
    if(b<=a)return true;
    else
        return false;
}
void judge1(int x){
    for(int i='a';i<='z';i++){
       if(vis2!=0){
        vis2[i]=max(0,x*vis2[i]-vis1[i]);
       }
       for(int j=0;j<vis2[i];j++)
        s3+=i;
    }
}
int main(){
    cin>>s1>>s2;
    int len1=s1.size();
    int len2=s2.size();
    for(int i=0;i<len1;i++){
        if(s1[i]!='?')vis1[s1[i]]++;
        else a++;
    }
    int x=len1/len2;
    if(x==0){
        for(int i=0;i<s1.size();i++)
            if(s1[i]=='?')s1[i]=s2[0];
        cout<<s1<<endl;
        return 0;
    }
    for(int i=0;i<s2.size();i++){
        vis2[s2[i]]++;
    }
    int l=0,r=x,m;
    while(l<=r){
        int mid=(l+r)/2;
        if(judge(mid)){
            l=mid+1;m=mid;}
        else
        r=mid-1;
    }
    judge1(m);
    int l1=0;
    for(int i=0;i<len1;i++){
        if(s1[i]=='?'&&l1<s3.size()){
            s1[i]=s3[l1++];
        }
        else if(s1[i]=='?'&&l1>=s3.size())
            s1[i]='z';
    }
    cout<<s1<<endl;
}

猜你喜欢

转载自blog.csdn.net/lpeaceminusone/article/details/83110459