https://codeforces.com/contest/1141/problem/D

题解:

记录第一个字符串每个字母的位置

历遍第二个字符串,先跳过?字符,因为?不可以与?字符配对先,造成浪费

对于找不到配对的字符与第一个字符串的?配对直到没有

然后对第二个字符串的?配对,先配对非?字符

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=150000+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k;
int ans,cnt,flag,temp,sum;
int a[N];
char str1[N],str2[N];
struct node{
    int a,b;
}x;
queue<node>q;
queue<int>st[30];
int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    //cout.tie(0);
    //scanf("%d",&t);
    //while(t--){
    scanf("%d",&n);
    scanf("%s",str1);
    scanf("%s",str2);
    for(int i=0;i<n;i++){
        if(str1[i]=='?'){
            st[0].push(i);
            continue;
        }
        st[str1[i]-'a'+1].push(i);
    }
    for(int i=0;i<n;i++){
        if(str2[i]=='?'){
            continue;
        }
        if(st[str2[i]-'a'+1].size()!=0){
            x.a=st[str2[i]-'a'+1].front();
            x.b=i;
            q.push(x);
            st[str2[i]-'a'+1].pop();
        }else{
            if(st[0].size()!=0){
                x.a=st[0].front();
                x.b=i;
                q.push(x);
                st[0].pop();
            }
        }
    }
    for(int i=0;i<n;i++){
        if(str2[i]=='?'){
            for(int j=26;j>=0;j--){
                if(st[j].size()!=0){
                    x.a=st[j].front();
                    x.b=i;
                    q.push(x);
                    st[j].pop();
                    break;
                }
            }
        }
    }
    cout<<q.size()<<endl;
    while(!q.empty()){
        cout<<q.front().a+1<<" "<<q.front().b+1<<endl;
        q.pop();
    }
    //}

#ifdef DEBUG
	printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
    //cout << "Hello world!" << endl;
    return 0;
}