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. DTS_E_CANNOTACQUIRECONNECTIONFROMCONNECTIONMANAGER. The AcquireConnection method call to the connection manager "XXX" failed with error code 0xC0209303.

    问题: 今天写了一个新的SSIS的ETL包,运行报如下错误. DTS_E_CANNOTACQUIRECONNECTIONFROMCONNECTIONMANAGER. The AcquireConnec ...

  2. swoole之内存

    一.代码 <?php // 可以用来数据共享 // 执行完后 自动释放 // 创建内存表 $table = new swoole_table(1024); // 内存表增加一列 $table-& ...

  3. redis.rpm 安装

    yum install jemalloc wget http://www6.atomicorp.com/channels/atomic/centos/6/x86_64/RPMS/redis-3.0.7 ...

  4. 三 保存客户&分页查询&Spring解决延迟加载问题

    保存客户 Struts2通过模型驱动拿到封装对象,调用业务层将对象传递到持久层,持久层调用Hibernate模版将对象持久化到数据库. 持久层代码实现: 分页查询: 创建PageBean实体类,里面有 ...

  5. 四 String类

    1 关于String类

  6. NO22 Linux正则表达式--grep命令常用参数

    Linux正则表达式:  一.基础正则第一波字符说明: 示例: 二.基础正则第二波字符说明:  三.基础正则第二波字符说明: 示例:  grep:一般常用参数 示例:+和*  ?和.:  |:  () ...

  7. python三大神器===》迭代器

    迭代器: 1.认识迭代器 迭代器是访问集合元素的一种方式.迭代器是一个可以记住遍历的位置的对象.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退. 我们怎样才能 ...

  8. Linux:Shell-Bash基本功能

    1.历史命令 history [选项] [历史命令保存文件] 选项:-c  清空历史命令 -w 把缓存中的历史命令写入历史命令保存文件 ~/.bash_history 历史命令默认保存1000条,可以 ...

  9. day06-Python运维开发基础(字符串格式化与相关的函数、列表相关的操作)

    1. 字符串相关的操作与格式化 # ### 字符串相关操作 # (1)字符串的拼接 + var1 = "亲爱的," var2 = "男孩" res = var1 ...

  10. day04-Python运维开发基础(位运算、代码块、流程控制)

    # (7)位运算符: & | ^ << >> ~ var1 = 19 var2 = 15 # & 按位与 res = var1 & var2 " ...