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();
}
}
}
}
最新文章
- ExtJS 4.2 Grid组件的单元格合并
- Css - 渲染按钮
- Hibernate常用配置文件详解
- hdu 3579 Hello Kiki
- knowledge about apache
- 解决 Zabbix agent on [HOSTNAME] is unreachable for 5 minutes
- 在mysql中创建存储过程出现1307错误,解决方法
- btsync 分享资源
- AJAX,PHP,前端简单交互制作输入框效果
- Python的*args与**kwargs
- 【jframe】Java架构师之路 - 第01篇:Get Started
- VMware虚拟机里Centos7的IP地址查看方法
- js递归遍历多维数组并在修改数组的key后返回新的多维数组
- Linux —— 命令
- 3.键盘输入10个数,放到数组中,(1)去除该数组中大于10的数 	(2)将该数组中的数字写入到本地文件number.txt中
- React Native 异步存储
- jmeter中测试接口
- 01: 企业微信API开发前准备
- js面向对象编程-高级内容
- Zabbix监控TCP status