大佬说博客要打完趁热写...
题目大意
给出两个字符序列,可包含小写字母与'?',相同的字母可以匹配,而可以'?'与任意字符匹配。
求最大的匹配,并将每个匹配的两个字母位置输出
思路
将字符存入对应vector中,优先匹配非'?'的字符.
Code:
#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=150010;
char buf1[maxn];
char buf2[maxn];
int n;
vector<int> match[28];
vector<pair<int,int> > ans;
int main(){
scanf("%d",&n);
getchar();
gets(buf1);
gets(buf2);
for(int i=0;i<n;++i){
if(buf1[i]=='?'){
match['z'-'a'+1].push_back(i);
}else{
match[buf1[i]-'a'].push_back(i);
}
}
for(int i=0;i<n;++i){
char ch = buf2[i];
if(ch=='?'){
continue;
}
if(match[ch-'a'].size()!=0){
ans.push_back({*(--match[ch-'a'].end())+1,i+1});
match[ch-'a'].pop_back();
}else if(match['z'-'a'+1].size()!=0){
ans.push_back({*(--match['z'-'a'+1].end())+1,i+1});
match['z'-'a'+1].pop_back();
}
}
for(int i=0;i<n;++i){
char ch = buf2[i];
if(ch=='?'){
for(int j=0;j<='z'-'a'+1;++j){
if(match[j].size()!=0){
ans.push_back({*(--match[j].end())+1,i+1});
match[j].pop_back();
break;
}
}
}
}
cout << ans.size() << endl;
for(int i=0;i<ans.size();++i){
cout << ans[i].first << ' ' << ans[i].second<<endl;
}
return 0;
}