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

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 riristands 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').

扫描二维码关注公众号,回复: 6527780 查看本文章

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

就是找两个字符串中最多能找到相同字母的对数,还有一个条件是问号能表示任意一个字母每个字符串中的每个字母

#include<bits/stdc++.h>

using namespace std;

const int N = 2e5;
char a[N],b[N];
struct node
{
	int l,r;
}p;
stack<int> a1[N],b1[N];
queue<node> que;

int main()
{
	int n;
	scanf("%d %s %s",&n,a,b);
	for (int i=0;i<n;i++)
	{
		if ('a'<=a[i] && a[i]<='z')
		{
			a1[a[i]-'a'].push(i+1);
		}
		if ('a'<=b[i] && b[i]<='z')
		{
			b1[b[i]-'a'].push(i+1);
		}
		if (a[i]=='?')
		{
			a1[27].push(i+1);
		}
		if (b[i]=='?')
		{
			b1[27].push(i+1);
		}
	}
	for (int i=0;i<=25;i++)
	{
		while (!a1[i].empty() && !b1[i].empty())
		{
			p.l = a1[i].top();
			p.r = b1[i].top();
			que.push(p);
			a1[i].pop();
			b1[i].pop();
		}
		while (!a1[i].empty() && !b1[27].empty())
		{
			p.l = a1[i].top();
			p.r = b1[27].top();
			que.push(p);
			a1[i].pop();
			b1[27].pop();
		}
		while (!a1[27].empty() && !b1[i].empty())
		{
			p.l = a1[27].top();
			p.r = b1[i].top();
			que.push(p);
			a1[27].pop();
			b1[i].pop();
		}
	}
	while (!a1[27].empty() && !b1[27].empty())
	{
		p.l = a1[27].top();
		p.r = b1[27].top();
		que.push(p);
		a1[27].pop();
		b1[27].pop();
	}
	printf("%d\n",que.size())
;	while (!que.empty())
	{
		node t = que.front();
		que.pop();
		printf("%d %d\n",t.l,t.r);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40912854/article/details/89366742