codeforces825 D. Suitable Replacement 二分答案

本文介绍了一个字符串处理问题,目标是在给定的字符串S中通过替换问号字符来最大化包含目标子串T的数量。通过二分查找确定最优的替换方案,并详细解析了实现思路与代码实现细节。
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 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相同长度的串,并将S中的?改为小写字母,满足输出串中T串数量最多

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

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


#include <bits/stdc++.h>
#define LL long long
using 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;
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值