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
思路如下
- 题意:两行长度均为n的字符串分别代表是“左靴子”“右靴子”,同种颜色的左、右靴子各一只能够组成一对,每个靴子只能使用一次,问最多可以形成几对靴子,并输出所配对靴子出现在原始字符串中的位置。在字符串中 每个小写字母表示是一种颜色,’ ? ’ 字符可以当成任意颜色来使用。
- 思路:这一题我们可以使用桶排的思想来解决这一题,我们可以用先定义 vector vec[] ,用vec[] 数组来统字符串 l 中某种字符出现的位置,那么同一种字符 出现在多少个位置,那么这个字符就出现几次。在处理第二个字符串 r 时我们应用贪心的思想,因为 ’ ? ’ 字符可以充当任意字符来使用,那么我们最后在考虑 ‘ ?’字符的配对问题,所以我们先考虑 小写字母的配对问题,对于在字符串 r 中小写字母,我们肯定是现让它与在l中相同的字母先进行匹配,如果在l字符串中没有相同的 该字母,那么我们就使用在 l字符串中的万能 ’ ? ’ 来与它进行匹配一单匹配上我们就存起来,等到最后一起输出 ; 最后考 在r字符串中的 ’ ? ’ 字符就容易多了,只要在l 中用剩下的字符,都可以与之匹配。
⚠️ 在题解中我傻屌般的多用了一个 barrel[] 数组来统计 在l 字符串中 每中 字符出现的次数,其实这 与 vec[] 数组的作用 有些重复了
题解如下
#include<iostream>
#include<vector>
using namespace std;
int barrel[125];
char ar[150005];
struct Pir
{
int a,b;
}pir[150005],pirr[150005];
int main()
{
//freopen("test.txt","r",stdin);
int n;
scanf("%d ", &n);
char ch;
vector<int> vec[125];
for(int i = 1; i <= n; i ++)
{
scanf("%c",&ar[i]);
barrel[ar[i]] ++;
vec[ar[i]].push_back(i);
}
int pos = 0;
getchar();
int posi = 0;
for(int i = 1; i <= n; i ++)
{
scanf("%c",&ch);
//cin>>ch;
if(ch == '?')
{
pirr[posi ++].b = i;
}
else
{
if(barrel[ch] > 0)
{
barrel[ch] --;
pir[pos].a = vec[ch].back();
vec[ch].pop_back();
pir[pos ++].b = i;
}
else if(barrel['?'] > 0)
{
barrel['?'] --;
pir[pos].a = vec['?'].back();
vec['?'].pop_back();
pir[pos ++].b = i;
}
}
}
printf("%d\n",pos + posi);
for(int i = 0; i < pos; i ++)
{
printf("%d %d\n",pir[i].a,pir[i].b);
}
posi --;
for(int i = 1; i <= n && posi >= 0; i ++)
{
if(barrel[ar[i]] > 0)
{
for( ; barrel[ar[i]] > 0; )
{
printf("%d %d\n",vec[ar[i]].back(),pirr[posi --].b);
vec[ar[i]].pop_back();
if(posi < 0)
break;
barrel[ar[i]] --;
}
}
}
return 0;
}
来源:CSDN
作者:做一只大熊猫
链接:https://blog.csdn.net/qq_34261446/article/details/104101730