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 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.
题意:两个字符串,两两字符匹配条件是在不同字符串中且相等,'?'可以与任何字符匹配,求最大匹配并输出两个匹配字符位置
思路:贪心,用vector存出现字符的位置,首先是普通字符(都不含'?')能匹配就匹配,之后是第一个字符串中'?'与剩下第二个字符串中还剩下的遍历到就匹配,因为?字符和任何都行,之后第二个字符串中'?'与剩下第一个字符串中还剩下的遍历到就匹配
// luogu-judger-enable-o2
#include<bits/stdc++.h>
#include<unordered_set>
#define rg register ll
#define inf 2147483647
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
#define ll long long
#define maxn 200005
const double eps = 1e-8;
using namespace std;
inline ll read()
{
char ch = getchar(); ll s = 0, w = 1;
while (ch < 48 || ch>57) { if (ch == '-')w = -1; ch = getchar(); }
while (ch >= 48 && ch <= 57) { s = (s << 1) + (s << 3) + (ch ^ 48); ch = getchar(); }
return s * w;
}
inline void write(ll x)
{
if (x < 0)putchar('-'), x = -x;
if (x > 9)write(x / 10);
putchar(x % 10 + 48);
}
vector<ll>a[30],b[30];
vector<pair<ll,ll>>p;
int main()
{
ll n=read();
for(rg i=1;i<=n;i++)
{
char s;
cin>>s;
//cout<<s<<endl;
if(s=='?')a[26].push_back(i);
else a[s-'a'].push_back(i);
}
//getchar();
for(rg i=1;i<=n;i++)
{
char s;
cin>>s;
//cout<<s<<endl;
if(s=='?')b[26].push_back(i);
else b[s-'a'].push_back(i);
}
ll ans=0;
for(rg i=0;i<26;i++)
{
//cout<<a[i].size()<<" "<<b[i].size()<<endl;
if(a[i].size()&&b[i].size())
{
ll k=min(a[i].size(),b[i].size());
ans+=k;
for(rg j=0;j<k;j++)
{
p.push_back(make_pair(a[i][j],b[i][j]));
}
a[i].erase(a[i].begin(),a[i].begin()+k);
b[i].erase(b[i].begin(),b[i].begin()+k);
}
}
//cout<<ans<<"a"<<endl;
//cout<<1<<endl;
for(rg i=0;i<26;i++)
{
if(b[i].size())
{
ll k=min(a[26].size(),b[i].size());
ans+=k;
for(rg j=0;j<k;j++)
{
p.push_back(make_pair(a[26][j],b[i][j]));
}
a[26].erase(a[26].begin(),a[26].begin()+k);
b[i].erase(b[i].begin(),b[i].begin()+k);
}
}
// cout<<1<<endl;
for(rg i=0;i<26;i++)
{
if(a[i].size())
{
ll k=min(b[26].size(),a[i].size());
ans+=k;
for(rg j=0;j<k;j++)
{
p.push_back(make_pair(a[i][j],b[26][j]));
}
a[i].erase(a[i].begin(),a[i].begin()+k);
b[26].erase(b[26].begin(),b[26].begin()+k);
}
}
if(a[26].size()&&b[26].size())
{
ll k=min(b[26].size(),a[26].size());
ans+=k;
for(rg j=0;j<k;j++)
{
p.push_back(make_pair(a[26][j],b[26][j]));
}
a[26].erase(a[26].begin(),a[26].begin()+k);
b[26].erase(b[26].begin(),b[26].begin()+k);
}
cout<<ans<<endl;
for(auto it:p)
{
cout<<it.first<<" "<<it.second<<endl;
}
return 0;
}