D. Colored Boots(STL)

There are nn left boots and nn 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 ll and rr, both of length nn. The character lili stands for the color of the ii-th left boot and the character riri stands for the color of the ii-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 nn (1≤n≤1500001≤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 ll of length nn. It contains only lowercase Latin letters or question marks. The ii-th character stands for the color of the ii-th left boot.

The third line contains the string rr of length nn. It contains only lowercase Latin letters or question marks. The ii-th character stands for the color of the ii-th right boot.

Output

Print kk — 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 kk lines should contain pairs aj,bjaj,bj (1≤aj,bj≤n1≤aj,bj≤n). The jj-th of these lines should contain the index ajaj of the left boot in the jj-th pair and index bjbj of the right boot in the jj-th pair. All the numbers ajaj should be distinct (unique), all the numbers bjbj should be distinct (unique).

If there are many optimal answers, print any of them.

Examples

input

Copy

10
codeforces
dodivthree

output

Copy

5
7 8
4 9
2 2
9 10
3 1

input

Copy

7
abaca?b
zabbbcc

output

Copy

5
6 5
2 3
4 6
7 4
1 2

input

Copy

9
bambarbia
hellocode

output

Copy

0

input

Copy

10
code??????
??????test

output

Copy

10
6 2
1 6
7 3
3 5
4 8
9 7
5 1
2 4
10 9
8 10

题解:用vector记录a数组中每个字符和其出现的位置,a数组中问号的位置。定义查找函数:bool Find(int s, int x)//在a数组里找与b数组中下标为x对应的字符s。遍历b数组,ans的pair vector记录答案,在Find函数中记录~~看代码实现:

感受:做完这道题感觉学到了不少东西呢~~

#include <iostream>
#include<vector>
using namespace std;
int n;
char a[150005];
char b[150005];
vector<int> v[666], sp;//v[i]记录字符ASCII码值为i的位置,sp记录问号的位置
vector<pair<int, int> > ans;//ans记录答案,pair分别对应a,b所对应的位置
bool Find(int s, int x)//在a数组里找与b数组中下标为x对应的字符s
{
	if (v[s].empty())//如果a数组里面没有对应的字符s了(ASCII码转换),就是没找到
		return 0;
	ans.emplace_back(v[s].back(), x);//记录字符在第一个串和第二个串的位置
	v[s].pop_back();//将记录的位置去掉
	return 1;
}
int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++)//字符串为了方便用下标来输入
	{
		cin >> a[i];
		v[a[i]].push_back(i);//将每一个a[i]字符对应的位置放入v的vector
	}
	for (int i = 1; i <= n; i++)
		cin >> b[i];
	for (int i = 1; i <= n; i++)//进行遍历
	{
		if (b[i] == '?')//记录问号的位置
			sp.push_back(i);
		else if (!Find(b[i], i))//如果不是问号,并且a中没有对应的字符(不包括问号)
			Find('?', i);//就将1个问号与之匹配
	}
	for (vector<int>::iterator it = sp.begin(); it != sp.end(); it++)//遍历问号的位置
	{
		for (int j = 0; j < 256; j++)//找到一个字符(任意)与问号对应
		{
			if (Find(j, *it))
				break;
		}
	}
	cout << ans.size() << endl;//总个数
	for(vector<pair<int,int> >::iterator it=ans.begin();it!=ans.end();it++)//遍历
		cout << (*it).first << " " <<(*it).second << endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43824158/article/details/88732834