CF1141D Colored Boots
There are n left boots and n right boots. Each boot has a color which is denoted as a lowercase Latin letter or a question mark ('?'). Thus, you are given two strings l and r, both of length n. The character li stands for the color of the i-th left boot and the character ri stands for the color of the i-th right boot.
A lowercase Latin letter denotes a specific color, but the question mark ('?') denotes an indefinite color. Two specific colors are compatible if they are exactly the same. An indefinite color is compatible with any (specific or indefinite) color.
For example, the following pairs of colors are compatible: ('f', 'f'), ('?', 'z'), ('a', '?') and ('?', '?'). The following pairs of colors are notcompatible: ('f', 'g') and ('a', 'z').
Compute the maximum number of pairs of boots such that there is one left and one right boot in a pair and their colors are compatible.
Print the maximum number of such pairs and the pairs themselves. A boot can be part of at most one pair.
The first line contains n (1≤n≤150000), denoting the number of boots for each leg (i.e. the number of left boots and the number of right boots).
The second line contains the string l of length n. It contains only lowercase Latin letters or question marks. The i-th character stands for the color of the i-th left boot.
The third line contains the string r of length n. It contains only lowercase Latin letters or question marks. The i-th character stands for the color of the i-th right boot.
Print k — the maximum number of compatible left-right pairs of boots, i.e. pairs consisting of one left and one right boot which have compatible colors.
The following k lines should contain pairs aj,bj (1≤aj,bj≤n). The j-th of these lines should contain the index aj of the left boot in the j-th pair and index bj of the right boot in the j-th pair. All the numbers aj should be distinct (unique), all the numbers bj should be distinct (unique).
If there are many optimal answers, print any of them.
10
codeforces
dodivthree
5
7 8
4 9
2 2
9 10
3 1
7
abaca?b
zabbbcc
5
6 5
2 3
4 6
7 4
1 2
9
bambarbia
hellocode
0
10
code??????
??????test
10
6 2
1 6
7 3
3 5
4 8
9 7
5 1
2 4
10 9
8 10
题意解释:输入2个长度为n的字符串a,b,输出2个字符串共有多少个可能相同的字母和相同的字母的位置,每个字母仅能用一次,'?'可以看成是任何字母即可以和任何字母进行匹配。
解题思路:先将所有字母匹配掉,然后匹配a的'?'和b的字母,再匹配b的'?'和a的字母,再匹配a的'?'和b的'?'如此可以确保是最多的匹配。
数据存储方面利用了vector,写起来比较方便
#include <bits/stdc++.h>
using namespace std;
vector<int>vt1[];
vector<int>vt2[];
int main()
{
int n;
string a,b;
cin>>n>>a>>b;
for(int i=;i<n;++i)
{
if(a[i]=='?')
{
vt1[].push_back(i+);
}
else
{
vt1[(a[i]-'a'+)].push_back(i+);
}
if(b[i]=='?')
{
vt2[].push_back(i+);
}
else
{
vt2[(b[i]-'a'+)].push_back(i+);
}
}
int ans=;
for(int i=;i<;++i)
{
for(int j=vt1[i].size()-,k=vt2[i].size()-;min(j,k)>=;--j,--k)
{
if(vt1[i][j]>&&vt2[i][k]>)
{
ans++;
vt1[i].pop_back();
vt2[i].pop_back();
}
}
}
for(int i=;i<;++i)
{
for(int j=vt1[].size()-,k=vt2[i].size()-;min(j,k)>=;--j,--k)
{
if(vt1[][j]>&&vt2[i][k]>)
{
ans++;
vt1[].pop_back();
vt2[i].pop_back();
}
}
}
for(int i=;i<;++i)
{
for(int j=vt1[i].size()-,k=vt2[].size()-;min(j,k)>=;--j,--k)
{
if(vt1[i][j]>&&vt2[][k]>)
{
ans++;
vt1[i].pop_back();
vt2[].pop_back();
}
}
}
for(int i=;i<;++i)
{
for(int j=vt1[].size()-,k=vt2[].size()-;min(j,k)>=;--j,--k)
{
if(vt1[][j]>&&vt2[][k]>)
{
ans++;
vt1[].pop_back();
vt2[].pop_back();
}
}
}
//
cout<<ans<<endl;
for(int i=;i<;++i)
{
vt1[i].clear();
vt2[i].clear();
}
for(int i=;i<n;++i)
{
if(a[i]=='?')
{
vt1[].push_back(i+);
}
else
{
vt1[(a[i]-'a'+)].push_back(i+);
}
if(b[i]=='?')
{
vt2[].push_back(i+);
}
else
{
vt2[(b[i]-'a'+)].push_back(i+);
}
}
for(int i=;i<;++i)
{
for(int j=vt1[i].size()-,k=vt2[i].size()-;min(j,k)>=;--j,--k)
{
if(vt1[i][j]>&&vt2[i][k]>)
{
cout<<vt1[i][j]<<" "<<vt2[i][k]<<endl;
vt1[i].pop_back();
vt2[i].pop_back();
}
}
}
for(int i=;i<;++i)
{
for(int j=vt1[].size()-,k=vt2[i].size()-;min(j,k)>=;--j,--k)
{
if(vt1[][j]>&&vt2[i][k]>)
{
cout<<vt1[][j]<<" "<<vt2[i][k]<<endl;
vt1[].pop_back();
vt2[i].pop_back();
}
}
}
for(int i=;i<;++i)
{
for(int j=vt1[i].size()-,k=vt2[].size()-;min(j,k)>=;--j,--k)
{
if(vt1[i][j]>&&vt2[][k]>)
{
cout<<vt1[i][j]<<" "<<vt2[][k]<<endl;
vt1[i].pop_back();
vt2[].pop_back();
}
}
}
for(int i=;i<;++i)
{
for(int j=vt1[].size()-,k=vt2[].size()-;min(j,k)>=;--j,--k)
{
if(vt1[][j]>&&vt2[][k]>)
{
cout<<vt1[][j]<<" "<<vt2[][k]<<endl;
vt1[].pop_back();
vt2[].pop_back();
}
}
}
}
CF1141D Colored Boots的更多相关文章
- D. Colored Boots(STL)
There are nn left boots and nn right boots. Each boot has a color which is denoted as a lowercase La ...
- Colored Boots题解
题目来自Codeforce 1141Dhttp://codeforces.com/problemset/problem/1141/D 因为是全英文题面,就先简单的阐述一下题面. 首先输入一个数n,然后 ...
- Codeforces Round #547 (Div. 3) D. Colored Boots
链接:https://codeforces.com/contest/1141/problem/D 题意: 给连个n长度的字符串. 求两个字符串相同字符对应位置的对数,并挨个打印. 字符:?可以代替任何 ...
- Codeforces Round #547 (Div. 3) D. Colored Boots (贪心,模拟)
题意:有两个字符串,两个字符串中的相同字符可以相互匹配,\(?\)可以和任意字符匹配,输出最大匹配的字符数量和它们分别两个字符串中的位置. 题解:很容易贪心,我们先遍历第一个字符串,然后在第二个字符串 ...
- Codeforces Round #547 (Div. 3) 题解
Codeforces Round #547 (Div. 3) 题目链接:https://codeforces.com/contest/1141 A,B咕咕了... C. Polycarp Restor ...
- Codeforces554 C Kyoya and Colored Balls
C. Kyoya and Colored Balls Time Limit: 2000ms Memory Limit: 262144KB 64-bit integer IO format: %I64d ...
- poj 2513 Colored Sticks trie树+欧拉图+并查集
点击打开链接 Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27955 Accepted ...
- POJ 2513 Colored Sticks
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 28036 Accepted: 7428 ...
- 周赛-Colored Sticks 分类: 比赛 2015-08-02 09:33 7人阅读 评论(0) 收藏
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 32423 Accepted: 8556 Desc ...
随机推荐
- 第1节 storm编程:1、storm第一天上次课程内容回顾
上次课程内容回顾:1.kafka的基本介绍:kafka是一个消息队列2.消息队列的作用:解耦3.kafka与传统消息队列的对比: 传统消息队列:支持事务 kafka的特点:比较快,比较快的两个原因:顺 ...
- 提高 Java 代码性能的各种技巧
Java 6,7,8 中的 String.intern – 字符串池 这篇文章将要讨论 Java 6 中是如何实现 String.intern 方法的,以及这个方法在 Java 7 以及 Java 8 ...
- GeoDa绘制疫情地图
刚学习GeoDa,菜鸟,目前还不能在地图上显示省市名称,求教. 看到丁香医生发布的疫情地图,我也尝试做一下,不过我的shp文件上只有中国大陆的31个省市. 数据来源于丁香医生,截至时间为 2020.1 ...
- 在linux环境中配置solr
第一步:安装linux.jdk.tomcat. 第二步:把solr的压缩包上传到服务器.并解压.我的solr压缩包是解压在/usr/local/solr/包下的 系统默认是没有solr包的需要自己创建 ...
- ng-校验重复并提示具体重复内容
//校验其他等级模块是否存在"职业类别"完全一致的等级模块 var moreFlag=false; for(var i=0;i<$scope.djArr.length;i++ ...
- 用绿色版TOMCAT和绿色版JDK安装一个WEB服务器
(1) 使用绿色版本JDK,解压到一个目录上D:\jdk1.6. (2) 使用绿色版本Tomcat,解压到另一个目录上D:\jdk1.6\tomcat5.5 只要在bat文件D:\tomcat5. ...
- jenkins -- 安装、任务构建
一.jenkins是什么? Jenkins是一个开源的.提供友好操作界面的持续集成(CI)工具,起源于Hudson(Hudson是商用的),主要用于持续.自动的构建/测试软件项目.监控外部任务的运行( ...
- 查看 Secret【转】
可以通过 kubectl get secret 查看存在的 secret. 显示有两个数据条目,kubectl describe secret 查看条目的 Key: 如果还想查看 Value,可以用 ...
- 站在巨人的肩膀上才能看得更加远[Amo]
本来只是路过,写详细一点. 我看楼主浮躁得不得了.现在什么都不要做了,先去看几遍<不要做浮躁的嵌入式工程师>这篇文章,想清楚了,再动手吧. 我做了个实例,不用ST的库来点LED,解答你的问 ...
- vue :src 不显示的解决方案
一定要将静态资源引入 [ require("@/assets/") ],绑定到 模型绑定的:src 数据中 动态的数据才能有效 <template> <d ...