Codeforces 825 D Suitable Replacement
题目地址:http://codeforces.com/contest/825/problem/D
题意:给你两个字符串,第一个字符串里会有‘?’,‘?’可以替换任意为字母,要求你把字符串中的‘?’全部换成字母,要能与第二个字符串有最大匹配数,然后他并不要求是匹配的是子串,只要是字母个数和第二个字符串字母个数相同就算是匹配的(这个要理解,比如第一个字符串是abba,第二个是ab,按他的规则最大匹配数是2),匹配过一次就不能再利用了。(字符串可以调换顺序,就简单很多了,一开始我还以为不能调换顺序)
思路:因为可以调换顺序所以直接记录需要多少某个字母就好了,我的代码是模拟过程,先把‘?’的个数记录下来,再把可以构成第二个字符串的个数全部在第一个字符串中去掉,然后每次判断构成第二个字符串需要多少字符,一次次的减去‘?’个数,直到所有‘?’个数全部用完。但是我觉得正确写法应该是二分枚举最后拥有的第二个字符串个数,有兴趣的可以试下看
#include <iostream>
#include <cstring>
#include <string>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <cstdio>
#include <algorithm>
#define LL long long
#define N 1010
#define M 50010
#define inf 0x3f3f3f3f
using namespace std;
const LL mod = 1e9 + 7;
const double eps = 1e-9;
map<char, LL> map1, map2, used;
int main() {cin.sync_with_stdio(false);string a, b;while (cin >> a >> b) {map1.clear();map2.clear();used.clear();LL ans = 0;LL cnt = 0;for (int i = 0; i < a.length(); i++) {if (a[i] == '?') {ans++;}else{if (map1.find(a[i]) == map1.end()) {map1[a[i]] = 0;}map1[a[i]]++;}}for (int i = 0; i < b.length(); i++) {cnt++; if (map2.find(b[i]) == map2.end()) {map2[b[i]] = 0;}map2[b[i]]++;}LL flag = inf;for (map<char, LL>::iterator it = map2.begin(); it != map2.end(); it++) {flag = min(map1[it->first] / it->second, flag);}for (map<char, LL>::iterator it = map2.begin(); it != map2.end(); it++) {map1[it->first] -= flag*it->second;}while (ans>0) {for (map<char, LL>::iterator it = map2.begin(); it != map2.end(); it++) {if (ans < 0) {break;}if (map1[it->first] >= it->second) {map1[it->first] -= it->second;}else {if (used.find(it->first) == used.end()) {used[it->first] = 0;}int kk = min(ans, it->second - map1[it->first]);used[it->first] += kk;map1[it->first] = 0;ans -= kk;}}}int i = 0;for (map<char, LL>::iterator iter = used.begin(); iter != used.end(); iter++) {for (int j = 0; j < iter->second; j++) {while (a[i] != '?') {cout << a[i];i++;if (i == a.length()) {return 0;}}cout << iter->first;i++;}}for (; i < a.length(); i++) {cout << a[i];}cout << endl;}return 0;
}
Codeforces 825 D Suitable Replacement相关推荐
- codeforces round25
1.A Binary Protocol 遍历字符串,如果是1,则累加,如果是0,输出当前的累加值 代码参考: OJ/A Binary Protocol.java at master · wuli249 ...
- Educational Codeforces Round 25
这一场是暑期的第一场,做了4个题,被HACK两个,都是很粗心的错误,手生的问题. [A]Binary Protocol 题意:给你一串字符串,只有0和1.用m个0将字符串分为m+1段,每段字符串中'1 ...
- qt 苹果应用程序_什么是苹果的电视应用程序,您应该使用它吗?
qt 苹果应用程序 Apple's TV app, which recently appeared on iOS devices and Apple TV, is meant to help user ...
- 8.6 edu25 ,577#div2 CF补题(二分 ,dp 与 贪心
两道题 1.edu 25 D. Suitable Replacement 题意:给定字符串s,t,s中'?'字符可以以任何字符替换,问如何替换 可使 替换后的s重新排序与t的匹配次数最多(len_t& ...
- joda-time 文档的部分翻译
Why Joda Time? The standard date and time classes prior to Java SE 8 are poor(标准日期和时间类在jdk1.8之前是极差的) ...
- elementary OS 5 Juno (Pantheon) 安装后配置总结(干货很多)
欢迎加入 Ubuntu 阵营!elementary OS 是 Ubuntu 阵营中兼具实用和美观的发行版,的确值得安装尝试.这是一篇长文,我精简了一些内容,但是干货越多说得越详细,篇幅就会越大.已经克 ...
- Fiat-Shamir heuristic(含实现)和Random oracle
1. Fiat-Shamir 定义 通过Fiat-Shamir转换,可将Bulletproof中Verifier多次challenge的interactive证明切换为Non-Interactive ...
- SitePoint播客#94:新年快乐!
Episode 94 of The SitePoint Podcast is now available! This week your hosts are Patrick O'Keefe (@iFr ...
- PMP 模拟200题
PMP 模拟题三(答案和解析在最下方) 1:一项目经理正在管理他第二个项目,第二个项目在第一个项目开始一个月后启动,两个项目同时进行中.尽管第一 个项目很小,但规模与 日俱增.每经历一天,项目经理就越 ...
最新文章
- pandas使用groupby函数对dataframe进行分组统计、使用as_index参数设置分组聚合的结果中分组变量不是dataframe的索引(index)
- com.sun.crypto.provider.SunJCE
- Qt计算器开发(二):信号槽实现数学表达式合法性检查
- 2011年最新使用CSS3实现各种独特悬浮效果的教程
- mysql locking_Mysql next-key locking,读锁,写锁
- Andrew Ng机器学习编程作业:K-means Clustering and Principal Component Analysis
- 完美解决header,footer等HTML5标签在IE(IE6/IE7/IE8)无效的方法
- java哪个软件编程好学吗_自学编程:Java和C语言相比哪个好?哪个更值得学习?...
- 通俗易懂!《图机器学习导论》附69页PPT
- c#halcon 联合编程经验总结
- EPIC下载商城UE4内容后
- Windows10本地数据库搭建(MySQL、PostgreSQL)
- 雪豹安装光盘 苹果mac 下载
- Respon.WriteFile 下载文件
- 把领导发展成朋友,而不是仅仅的同事关系
- NOIP5424. 【NOIP2017提高A组集训10.25】凤凰院凶真 LCIS 方案
- 分享一下 软件测试面试历程和套路,真的很实在
- 将字符串中的特殊字符进行转义
- linux waitpid 用法,linux的fork(),waitpid()及wait()的用法
- 为什么保持代码整洁如此重要
热门文章
- ViewAnimator 之(三) ViewAnimator及其子类分析
- matlab中错误使用fmincon,MATLAB中fmincon 函数问题
- 邓凡平WIFI学习笔记3: WiFi 协议安全部分
- 详谈Python爬虫代理设置
- 电梯控制算法(4)单电梯场景——限层策略
- java websocket 1.0 tomcat_Java / Tomcat:如何处理WebSocket安装完成
- 初探arm与aarch64虚拟化兼容心得
- C语言二维数组范德蒙,浅谈范德蒙德(Vandermonde)方阵的逆矩阵的求法以及快速傅里叶变换(FFT)中IDFT的原理...
- PL/SQL删除锁表的进程
- D3 vs G2 vs Echarts以及其他可视化图表