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.

Input

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.

Output

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.

Examples
input
10
codeforces
dodivthree
output
5
7 8
4 9
2 2
9 10
3 1
input
7
abaca?b
zabbbcc
output
5
6 5
2 3
4 6
7 4
1 2
input
9
bambarbia
hellocode
output
0
input
10
code??????
??????test
output
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的更多相关文章

  1. 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 ...

  2. Colored Boots题解

    题目来自Codeforce 1141Dhttp://codeforces.com/problemset/problem/1141/D 因为是全英文题面,就先简单的阐述一下题面. 首先输入一个数n,然后 ...

  3. Codeforces Round #547 (Div. 3) D. Colored Boots

    链接:https://codeforces.com/contest/1141/problem/D 题意: 给连个n长度的字符串. 求两个字符串相同字符对应位置的对数,并挨个打印. 字符:?可以代替任何 ...

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

    题意:有两个字符串,两个字符串中的相同字符可以相互匹配,\(?\)可以和任意字符匹配,输出最大匹配的字符数量和它们分别两个字符串中的位置. 题解:很容易贪心,我们先遍历第一个字符串,然后在第二个字符串 ...

  5. Codeforces Round #547 (Div. 3) 题解

    Codeforces Round #547 (Div. 3) 题目链接:https://codeforces.com/contest/1141 A,B咕咕了... C. Polycarp Restor ...

  6. Codeforces554 C Kyoya and Colored Balls

    C. Kyoya and Colored Balls Time Limit: 2000ms Memory Limit: 262144KB 64-bit integer IO format: %I64d ...

  7. poj 2513 Colored Sticks trie树+欧拉图+并查集

    点击打开链接 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27955   Accepted ...

  8. POJ 2513 Colored Sticks

    Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 28036   Accepted: 7428 ...

  9. 周赛-Colored Sticks 分类: 比赛 2015-08-02 09:33 7人阅读 评论(0) 收藏

    Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 32423 Accepted: 8556 Desc ...

随机推荐

  1. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 按钮:默认/标准按钮

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  2. rally问题合集

    rally 执行过程中涉及到keystone的用例,需要调用adminurl,在-/rally/lib/python2.7/site-packages/rally/osclients.py(主机文件的 ...

  3. Linux centosVMware zip压缩工具、tar打包、打包并压缩

    一. zip压缩工具 可以用来压缩文件和目录,压缩目录是需要指定目录下的文件. [root@davery tmp]# cp 1.txt davery/[root@davery tmp]# du -sh ...

  4. java学习-初级入门-面向对象③-类与对象-类与对象的定义和使用1

    今天学习类与对象.先大致解释一下类与对象的含义. 对象:object 有物体这一概念,一切皆对象(物体),对象由静态的属性和动态的行为组成. 比如说水杯: 水杯的静态属性:材质,容量,质量,颜色 动态 ...

  5. coding 321

    三大原理(计算机原理.操作系统原理.编译原理)两个协议(TCP与HTTP协议)一种结构(数据结构)

  6. vue - 封装input

    input子组件 <el-input :value="value" placeholder="请输入内容" size="small" ...

  7. Day2-M-Prime Ring Problem-HDU1016

    A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle ...

  8. SpringBoot 解决“不支持发行版本xx”的问题

    原因:很多地方都要配置jdk版本,某些地方配置的jdk版本不同.比如你要使用jdk8,某些地方配成了jdk7. 最常见配置错的地方:Ctrl+Shift+Alt+S 如果没问题,查看Project中的 ...

  9. C++代写,代写C++,C++程序代写,C++ assignment代写

    C++代写,代写C++,C++程序代写 关于C++代写,我们的涉猎范围: C++数据结构.算法题目 C++操作系统os题目 C++网络编程networking题目 C++ Linux题目 C++ Wi ...

  10. 7.9 规划Varnish缓存

    ./varnishlog -i VCL_LOG