codeforces825 D. Suitable Replacement 二分答案
来源:互联网 发布:三维设计软件 编辑:程序博客网 时间:2024/06/04 22:07
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 stringss, you choose the one with the largest number ofnon-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 thesuitability of string s is maximal.
The first line contains string s (1 ≤ |s| ≤ 106).
The second line contains string t (1 ≤ |t| ≤ 106).
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.
?aa?ab
baab
??b?za
azbz
abcdabacaba
abcd
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 is1 and there are several dozens of such strings,"azbz" is just one of them.
In the third example there are no '?' characters and thesuitability of the string is 0.
题意:给S ,T两个字符串,让你输出一个和S相同长度的串,并将S中的?改为小写字母,满足输出串中T串数量最多
思路:二分答案,模拟太烦人,搞得我乱死了(主要是太弱),先二分出最多有几个T串,然后输出的时候判断S串中哪个字母不够就输出,最后全够了就随便输出一个字母。
还有注意long long,我的一下午贡献给这题也是没谁了
#include <bits/stdc++.h>#define LL long longusing 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【二分】
- Codeforces 825 D Suitable Replacement
- Educational Codeforces Round 25 D. Suitable Replacement
- Educational Codeforces Round 25 D. Suitable Replacement
- Codeforces 825D Suitable Replacement【贪心】水题
- 【贪心】codeforces 825D Suitable Replacement
- Code Force 825D Suitable Replacement
- Educational Codeforces Round 25 D Suitable Replacement 贪心
- codeforces 825D Suitable Replacement(字母代替?贪心)
- codeforces825D-Suitable Replacement
- CF825D:Suitable Replacement(字符串)
- D. String Game----二分答案
- HDU-4190 Problem D【二分答案】
- codeforces D. Multiplication Table 二分答案
- CodeForces 779D String Game【二分答案】
- codeforces #8D Two Friends 二分答案+计算几何
- Codeforces 609D Gadgets for dollars and pounds 二分答案
- python3:module
- 快速打开文件命令行的三种方法
- 算法学习笔记--3.Binary Search & Algorithmic Complexity
- zlib window上编译
- idhttpserver用法
- codeforces825 D. Suitable Replacement 二分答案
- hdu 4746 Mophues (莫比乌斯反演 + 分块+线性筛)
- python读取caffemodel文件
- HDU
- HDU1015(dfs回溯)
- java.lang.IllegalArgumentException: width and height must be > 0
- 【USACO1.3.3】回文串
- STA接入AP的过程分析
- c++ const理解