D. Colored Boots(STL)

博客围绕左右靴子颜色匹配问题展开,给定左右靴子颜色字符串,其中问号代表不定颜色,特定颜色相同才兼容,不定颜色与任何颜色兼容。需计算最大兼容靴子对数并输出具体配对,题解用vector记录相关信息,定义查找函数实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值