Educational Codeforces Round 25 D. Suitable Replacement【二分】

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 strings s, you choose the one with the largest number of non-intersecting occurrences of string tSuitability 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.


题意:输入a,b字符串,问在a中出现b字符串最多次情况下(补齐?后,a中字母可以随意排列),输出a;

思路:就是求字母出现次数的最大倍数,记录两个字符串字母出现次数,只需要二分出这个最大倍数,更新e数组补齐?部分就可以了;

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define max_n 1000010
using namespace std;
typedef long long LL;
char a[max_n],b[max_n];
LL c[310],d[310],e[310];
LL ans;

bool judge(LL x) //二分判断 
{
	LL res=ans;
	for(int i='a';i<='z';i++)
	{
		e[i]=x*d[i]-c[i]; //该字母多,有可能不需要用?补字母 
		if(e[i]>0)
			res=res-e[i];
		else e[i]=0;
	}
	return res>=0;
}

int main()
{
	memset(c,0,sizeof(c));
	memset(d,0,sizeof(d));
	memset(e,0,sizeof(e));
	scanf("%s",a);
	scanf("%s",b);
	int len1=strlen(a);
	int len2=strlen(b);
	for(int i=0;i<len1;i++)
		c[a[i]]++;
	ans=c['?']; //统计 ?的个数 
	for(int i=0;i<len2;i++)
		d[b[i]]++;
	LL l=-1,r=max_n,mid;
	while(r-l>1)
	{
		mid=(l+r)/2;
		if(judge(mid)) l=mid;
		else r=mid;
	}
	for(int i='a';i<='z';i++)  //这个地方需要重新更新 e 数组,不然会 wa 
	{
		if(l*d[i]>c[i])
			e[i]=l*d[i]-c[i];
		else e[i]=0;
	}
//	printf("%d\n",l);
//	for(int i='a';i<='z';i++)
//	{
//		printf("%d\n",e[i]); 
//	}
	int j='a';
	e['z']=max_n;  //剩余的?用 z 字符全部补上 
	for(int i=0;i<len1;i++)
	{
		if(a[i]!='?') printf("%c",a[i]);
		else 
		{
			if(e[j])
			{
				printf("%c",j);
				e[j]--;
			}
			else
			{
				j++;
				i--;
			}
		}
	}
	printf("\n");
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值