Codeforces Round #547 (Div. 3) D. Colored Boots (贪心,模拟)

题意:有两个字符串,两个字符串中的相同字符可以相互匹配,\(?\)可以和任意字符匹配,输出最大匹配的字符数量和它们分别两个字符串中的位置.
题解:很容易贪心,我们先遍历第一个字符串,然后在第二个字符串中去找与当前位置相同的字符,这个过程我们可以先将每个字符的位置存下来然后再操作,遍历完后再遍历字符和问号,最后是问号和问号匹配,具体看代码吧,主要是想学习一下用队列来模拟操作(会方便很多).
代码:
int n;
string l,r;
queue<int> h1[30],h2[30];
vector<PII> ans; int main() {
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n;
cin>>l>>r; rep(i,0,n-1){
if(l[i]=='?') h1[0].push(i+1);
else h1[l[i]-96].push(i+1);
if(r[i]=='?') h2[0].push(i+1);
else h2[r[i]-96].push(i+1);
} rep(i,1,26){
int mi=min(h1[i].size(),h2[i].size());
rep(j,1,mi){
ans.pb({h1[i].front(),h2[i].front()});
h1[i].pop();
h2[i].pop();
}
} rep(i,1,26){
int mi=min(h1[0].size(),h2[i].size());
rep(j,1,mi){
ans.pb({h1[0].front(),h2[i].front()});
h1[0].pop();
h2[i].pop();
}
} rep(i,1,26){
int mi=min(h1[i].size(),h2[0].size());
rep(j,1,mi){
ans.pb({h1[i].front(),h2[0].front()});
h1[i].pop();
h2[0].pop();
}
} int mi=min(h1[0].size(),h2[0].size());
rep(i,1,mi){
ans.pb({h1[0].front(),h2[0].front()});
h1[0].pop();
h2[0].pop();
} cout<<(int)ans.size()<<'\n'; rep(i,0,(int)ans.size()-1) cout<<ans[i].fi<<' '<<ans[i].se<<'\n'; return 0;
}
Codeforces Round #547 (Div. 3) D. Colored Boots (贪心,模拟)的更多相关文章
- Codeforces Round #547 (Div. 3) D. Colored Boots
链接:https://codeforces.com/contest/1141/problem/D 题意: 给连个n长度的字符串. 求两个字符串相同字符对应位置的对数,并挨个打印. 字符:?可以代替任何 ...
- Codeforces Round #547 (Div. 3) 题解
Codeforces Round #547 (Div. 3) 题目链接:https://codeforces.com/contest/1141 A,B咕咕了... C. Polycarp Restor ...
- Codeforces Round #367 (Div. 2) B. Interesting drink (模拟)
Interesting drink 题目链接: http://codeforces.com/contest/706/problem/B Description Vasiliy likes to res ...
- E. Superhero Battle Codeforces Round #547 (Div. 3) 思维题
E. Superhero Battle time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #547 (Div. 3) G 贪心
https://codeforces.com/contest/1141/problem/G 题意 在一棵有n个点的树上给边染色,连在同一个点上的边颜色不能相同,除非舍弃掉这个点,问最少需要多少种颜色来 ...
- Codeforces Round #547 (Div. 3) F 贪心 + 离散化
https://codeforces.com/contest/1141/problem/F2 题意 一个大小为n的数组a[],问最多有多少个不相交的区间和相等 题解 离散化用值来做,贪心选择较前的区间 ...
- Codeforces Round #547 (Div. 3) D
http://codeforces.com/contest/1141/problem/D 题目大意: 鞋子匹配,用一个小写字母表示一种颜色.L[i]表示左脚的颜色,R[i]表示右脚的颜色,只有当L[i ...
- Codeforces Round #547 (Div. 3) B.Maximal Continuous Rest
链接:https://codeforces.com/contest/1141/problem/B 题意: 给n个数,0代表工作,1代表休息,求能连续最大的休息长度. 可以连接首尾. 思路: 求普通连续 ...
- Codeforces Round #547 (Div. 3) A.Game 23
链接:https://codeforces.com/contest/1141/problem/A 题意: 给n和m,有两种操作:将n×2 或 n×3,求最少的乘法次数由n得到m. 不能得到时为-1. ...
随机推荐
- requests基础封装-get/post封装
字符串转化成字典: convert_to_dict.py: import jsonstr1 = '{"grant_type":"client_credential&qu ...
- 【Java】网络编程之NIO
简单记录 慕课网-解锁网络编程之NIO的前世今生 & 一站式学习Java网络编程 全面理解BIO/NIO/AIO 内容概览 文章目录 1.[了解] NIO网络编程模型 1.1.NIO简介 1. ...
- --safe-user-create
此参数如果启用,用户将不能用grant语句创建新用户,除非用户有mysql数据库中user表的insert权限, ./mysqld_safe --safe-user-create & 用-- ...
- leetcode 470. 用 Rand7() 实现 Rand10() (数学,优化策略)
题目链接 https://leetcode-cn.com/problems/implement-rand10-using-rand7/ 题意: 给定一个rand7()的生成器,求解如何产生一个rand ...
- 配置 containerd 镜像仓库完全攻略
作者简介 王海龙,Rancher中国社区技术经理,负责Rancher中国技术社区的维护和运营.拥有6年的云计算领域经验,经历了OpenStack到Kubernetes的技术变革,无论底层操作系统Lin ...
- SpringCloud Alibaba Nacos注册中心源码浅析
一.前置了解 1.1 简介 Nacos是一款阿里巴巴推出的一款微服务发现.配置管理框架.我们本次对将对它的服务注册发现功能进行简单源码分析. 1.2 流程 Nacos的分析分为两部分,一部分是我们的客 ...
- java虚拟机入门(三)- 你了解对象吗
对象对于java程序员来说,那是想要多少就有多少,所以那些嘲笑程序员的单身狗,哼,只有无知使你们快乐,想我大java开发,何曾缺少过对象.我们不仅仅知道创建对象,还知道创建对象的过程是啥样的,不信?往 ...
- 使用Python的pandas模块、mplfinance模块、matplotlib模块绘制K线图
目录 pandas模块.mplfinance模块和matplotlib模块介绍 pandas模块 mplfinance模块和matplotlib模块 安装mplfinance模块.pandas模块和m ...
- logging philosophy 日志哲学
Go kit - Frequently asked questions https://gokit.io/faq/ Logging - Why is package log so different? ...
- c 越界 数组越界
int main(int argc, char* argv[]){ int i = 0; int arr[3] = {0}; for(; i<=3; i++){ arr[i] = 0; prin ...