codeforces825 D. Suitable Replacement 二分答案
2017-08-09 17:11
253 查看
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【二分】
- [ZOJ 2112] Dynamic Rankings [块状链表+二分答案+二分查找]
- HDOJ 3715 Go Deeper (2Sat: 二分答案判定)
- HDU 6231 K-th Number CCPC2017 Harbin(二分答案)
- UVa - 815 Flooded! 洪水 二分猜答案+排序 重庆一中高2018级竞赛班第三次测试 2016.7.24 Problem 1
- 夕拾算法进阶篇:37)二分答案
- 洛谷OJ - P2759 - 奇怪的函数(二分答案)
- BZOJ 4443 [Scoi2015]小凸玩矩阵(二分答案+二分图匹配)
- CodeForces 785C Anton and Fairy Tale【二分答案+思维】
- BZOJ 1044 HAOI2008 木棍切割 二分答案+动态规划
- 【二分答案】【最大流】[HNOI2007]紧急疏散EVACUATE
- BZOJ 1717: [Usaco2006 Dec]Milk Patterns 产奶的模式( 二分答案 + 后缀数组 )
- 【BZOJ3993】星际战争(SDOI2015)-二分答案+最大流
- UVa 714 Copying Books - 二分答案
- hdu 3622 Bomb Game(2-sat 二分答案)
- LeetCode hard 668. Kth Smallest Number in Multiplication Table(二分答案,一次过了,好开心,哈哈哈哈)
- NOIP 2010 关押罪犯 二分答案+二分图染色判定 COGS 520
- 【BZOJ3993】星际战争(网络流,二分答案)
- CodeVS1696 奇怪的函数 【二分答案】