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 ...
随机推荐
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 按钮
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 表单:文本框(Textarea)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- rendering path定义了什么?有哪些?有什么作用?有什么限制?
rendering path 定义了光照在一个shader pass中处理的对象与顺序,如逐像素处理,逐顶点处理,向前处理,后向 deferred用于处理不透明物体,会先把它们根据深度检测,过滤后的内 ...
- SC.Lab3对于Factory的构建过程(from HIT)
Factory设计模式基本就是通过传入指定的参数/或者不传入参数,通过Factory的某个方法(为了避免实例化Factory对象,一般方法为静态static),来获取一个对象.这个是Factory用的 ...
- 「Luogu P2568 GCD」
看到这是一道紫题还是和gcd有关的才点进来(毕竟数论只会gcd). 前置芝士 质数**(又称素数):因数只有1和本身,但是很特殊的1不是一个质数. gcd**:欧几里得算法,又称辗转相除法,可以在约为 ...
- js获取cookie提取用户名asp.net+html
JavaScript是运行在客户端的脚本,因此一般是不能够设置Session的,因为Session是运行在服务器端的. 而cookie是运行在客户端的,所以可以用JS来设置cookie. 假设有这样一 ...
- zookeeper加Kafka集群配置
官方 https://zookeeper.apache.org/doc/r3.5.6/zookeeperStarted.html#sc_Prerequisites https://www.cnblog ...
- Jquery实现横向tab切换
//需求:鼠标放在不同的导航栏上,下面显示的内容自动切换 //代码如下 <!DOCTYPE html> <html lang="en"> <head& ...
- JuJu团队1月8号工作汇报
JuJu团队1月8号工作汇报 JuJu Scrum 团队成员 今日工作 剩余任务 困难 飞飞 实现三维Dense 将crossentrophy和softmax连接起来 无 婷婷 完善main.jl ...
- 求第K大数(分治)
题意:已知N个数,求第K大数. 分析: 1.复杂度O(n). 2.利用快排中划分的原理,每次划分以序列中第一个数x为标准,将序列划分为{比x大的数}x{比x小的数}. 3.若集合{比x大的数}中元素为 ...