Description
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
10 codeforces dodivthree
output
5
7 8
4 9
2 2
9 10
3 1
input
7 abaca?b zabbbcc
output
5 6 5 2 3 4 6 7 4 1 2
input
9 bambarbia hellocode
output
0
input
10 code?????? ??????test
output
10 6 2 1 6 7 3 3 5 4 8 9 7 5 1 2 4 10 9 8 10
题意:
给两个字符串a和b,每个字符表示一个靴子的颜色,两个相同的字母被认为是一对合法的靴子,如[f,f],[a,a],问号字符表示未知颜色的靴子,问号可以和任何颜色的靴子合法配对,问号也能和问号配对如,【a,?】【?,b】【?,?】,问两个字符串最大能匹配多少对靴子,输出每对靴子的下标。
思路:自己写的是双重循环,很复杂也特别长,看了别人的代码发现用stl的话就会很简单,自己对stl还是很不熟悉,很多时候想不到这一点
1.用26个容器存储所有靴子的下标,然后直接遍历a和b串中是否存在某个字母,存在就塞入ans容器中。注意最后对比一下问号容器中是否存在能和不成对颜色组成一对的情况。
代码:
#include<bits/stdc++.h>
using namespace std;
const int N=150005;
char s1[N],s2[N];
int a[27][N],v[27],p[N];
struct node
{
int l,r;
}s[N];
int main()
{
int n;
scanf("%d",&n);
scanf("%s%s",s1,s2);
for(int i=0;i<n;i++)
{
if(s1[i]=='?')
{
a[0][v[0]++]=i;
}
else
{
a[s1[i]-'a'+1][v[s1[i]-'a'+1]++]=i;
}
}int k=0;int u=0;
for(int i=0;i<n;i++)
{
if(s2[i]=='?')
{
p[k++]=i;
}
else
{
if(v[s2[i]-'a'+1]>0)
{
s[u].l=a[s2[i]-'a'+1][--v[s2[i]-'a'+1]]+1;
s[u++].r=i+1;
}
else if(v[0]>0)
{
s[u].l=a[0][--v[0]]+1;
s[u++].r=i+1;
}
}
}
for(int i=0;i<=26;i++)
{
for(int j=0;j<v[i];j++)
{
if(k>0)
{
s[u].l=a[i][j]+1;
s[u++].r=p[--k]+1;
}
}
}
printf("%d\n",u);
for(int i=0;i<u;i++)
{
printf("%d %d\n",s[i].l,s[i].r);
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
char text[150005];
queue<int>l[27],r[27];
vector<pair<int,int> >v;
int main()
{
int n;
cin>>n;
getchar();
gets(text);
for(int i=0;i<n;i++)
{
if(text[i]=='?')
{
l[26].push(i);
}
else
{
l[text[i]-'a'].push(i);
}
}
gets(text);
for(int i=0;i<n;i++)
{
if(text[i]=='?')
{
r[26].push(i);
}
else
{
r[text[i]-'a'].push(i);
}
}
for(int i=0;i<=26;i++)
{
while(r[i].size()&&l[i].size())
{
v.push_back({l[i].front(),r[i].front()});
l[i].pop();
r[i].pop();
}
while(r[26].size()&&l[i].size())
{
v.push_back({l[i].front(),r[26].front()});
l[i].pop();
r[26].pop();
}
while(r[i].size()&&l[26].size())
{
v.push_back({l[26].front(),r[i].front()});
l[26].pop();
r[i].pop();
}
}
cout<<v.size()<<endl;
for(auto it:v)
{
cout<<it.first+1<<" "<<it.second+1<<endl;
}
}
#include<bits/stdc++.h>
#define LL long long
#define pb(x) push_back(x)
using namespace std;
const int maxn=150007;
int n;
char a[maxn],b[maxn];
stack<int>a1[30],b1[30];
vector<pair<int,int> >ans;
int main()
{
scanf("%d",&n);
scanf("%s",a);
scanf("%s",b);
for(int i=0;i<n;i++)
{
if(a[i]=='?')a1[27].push(i+1);
if(b[i]=='?')b1[27].push(i+1);
if(a[i]>='a'&&a[i]<='z')a1[a[i]-'a'].push(i+1);
if(b[i]>='a'&&b[i]<='z')b1[b[i]-'a'].push(i+1);
}
for(int i=0;i<26;i++)
{
while(!a1[i].empty()&&!b1[i].empty())
{
ans.pb(make_pair(a1[i].top(),b1[i].top()));
a1[i].pop();
b1[i].pop();
}
while(!a1[i].empty()&&!b1[27].empty())
{
ans.pb(make_pair(a1[i].top(),b1[27].top()));
a1[i].pop();
b1[27].pop();
}
while(!b1[i].empty()&&!a1[27].empty())
{
ans.pb(make_pair(a1[27].top(),b1[i].top()));
a1[27].pop();
b1[i].pop();
}
}
while(!a1[27].empty()&&!b1[27].empty())
{
ans.pb(make_pair(a1[27].top(),b1[27].top()));
a1[27].pop();
b1[27].pop();
}
printf("%d\n",ans.size());
while(ans.size())printf("%d %d\n",ans.back().first,ans.back().second),ans.pop_back();
}