codeforces825 D. Suitable Replacement 二分答案
2017-08-09 17:11
197 查看
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
Output
Input
Output
Input
Output
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;
}
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;
}
相关文章推荐
- codeforces825 D. Suitable Replacement 二分答案
- Educational Codeforces Round 25 D. Suitable Replacement【二分】
- POJ 3122(二分答案)
- [BZOJ]2095 二分答案 + 混合图欧拉回路判定
- 【二分答案+spfa】架设电话线
- CODE[VS] 2744 养鱼喂妹纸 (二分答案验证 + 贪心)
- BZOJ 1863 ZJOI 2006 皇帝的烦恼 二分答案
- 【BZOJ4552】【HEOI2016】排序 [二分答案][线段树]
- 最佳序列 二分答案
- SPOJ 287 Smart Network Administrator 网络流、二分答案
- [BZOJ2196]Brownie Slicing 二分答案
- 【算法学习笔记】45. 二分答案 BFS路径 SJTU OJ 1031 二哥在黄山
- cf 377 D. Exams (安排考试||二分答案)
- 【BZOJ 4326】运输计划【树链剖分+差分+二分答案】
- poj 1743 Musical Theme(后缀数组+二分答案+height数组分组)
- NOIP模拟赛 军训(二分答案+单调队列优化DP)
- bzoj1189 [HNOI2007]紧急疏散evacuate(二分答案+bfs+最大流判是否满流)
- 洛谷P1991无线通讯网[kruskal | 二分答案 并查集]
- ZSOI2012 选数排序 二分答案
- 【网络流24题】魔术球问题 二分答案+最小路径覆盖