有毒的粽子

CF1141D Colored Boots

There are n left boots and n 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 l and r, both of length n. The character li stands for the color of the i-th left boot and the character ri stands for the color of the i-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 n (1n150000), 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 l of length n. It contains only lowercase Latin letters or question marks. The i-th character stands for the color of the i-th left boot.

The third line contains the string r of length n. It contains only lowercase Latin letters or question marks. The i-th character stands for the color of the i-th right boot.

Output

Print k — 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 k lines should contain pairs aj,bj (1aj,bjn). The j-th of these lines should contain the index aj of the left boot in the j-th pair and index bj of the right boot in the j-th pair. All the numbers aj should be distinct (unique), all the numbers bj 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
题意解释:输入2个长度为n的字符串a,b,输出2个字符串共有多少个可能相同的字母和相同的字母的位置,每个字母仅能用一次,'?'可以看成是任何字母即可以和任何字母进行匹配。
解题思路:先将所有字母匹配掉,然后匹配a的'?'和b的字母,再匹配b的'?'和a的字母,再匹配a的'?'和b的'?'如此可以确保是最多的匹配。
数据存储方面利用了vector,写起来比较方便
#include <bits/stdc++.h>
using namespace std;
vector<int>vt1[30];
vector<int>vt2[30];
int main()
{
    int n;
    string a,b;
    cin>>n>>a>>b;
    for(int i=0;i<n;++i)
    {
        if(a[i]=='?')
        {
            vt1[0].push_back(i+1);
        }
        else
        {
            vt1[(a[i]-'a'+1)].push_back(i+1);
        }
        if(b[i]=='?')
        {
            vt2[0].push_back(i+1);
        }
        else
        {
            vt2[(b[i]-'a'+1)].push_back(i+1);
        }
    }
    int ans=0;
    for(int i=1;i<30;++i)
    {
        for(int j=vt1[i].size()-1,k=vt2[i].size()-1;min(j,k)>=0;--j,--k)
        {
            if(vt1[i][j]>0&&vt2[i][k]>0)
            {
                ans++;
                vt1[i].pop_back();
                vt2[i].pop_back();
            }
        }
    }
    for(int i=1;i<30;++i)
    {
        for(int j=vt1[0].size()-1,k=vt2[i].size()-1;min(j,k)>=0;--j,--k)
        {
            if(vt1[0][j]>0&&vt2[i][k]>0)
            {
                ans++;
                vt1[0].pop_back();
                vt2[i].pop_back();
            }
        }
    }
    for(int i=1;i<30;++i)
    {
        for(int j=vt1[i].size()-1,k=vt2[0].size()-1;min(j,k)>=0;--j,--k)
        {
            if(vt1[i][j]>0&&vt2[0][k]>0)
            {
                ans++;
                vt1[i].pop_back();
                vt2[0].pop_back();
            }
        }
    }
    for(int i=0;i<1;++i)
    {
        for(int j=vt1[0].size()-1,k=vt2[0].size()-1;min(j,k)>=0;--j,--k)
        {
            if(vt1[0][j]>0&&vt2[0][k]>0)
            {
                ans++;
                vt1[0].pop_back();
                vt2[0].pop_back();
            }
        }
    }
    //
    cout<<ans<<endl;
    for(int i=0;i<30;++i)
    {
        vt1[i].clear();
        vt2[i].clear();
    }
    for(int i=0;i<n;++i)
    {
        if(a[i]=='?')
        {
            vt1[0].push_back(i+1);
        }
        else
        {
            vt1[(a[i]-'a'+1)].push_back(i+1);
        }
        if(b[i]=='?')
        {
            vt2[0].push_back(i+1);
        }
        else
        {
            vt2[(b[i]-'a'+1)].push_back(i+1);
        }
    }
    for(int i=1;i<30;++i)
    {
        for(int j=vt1[i].size()-1,k=vt2[i].size()-1;min(j,k)>=0;--j,--k)
        {
            if(vt1[i][j]>0&&vt2[i][k]>0)
            {
                cout<<vt1[i][j]<<" "<<vt2[i][k]<<endl;
                vt1[i].pop_back();
                vt2[i].pop_back();
            }
        }
    }
    for(int i=1;i<30;++i)
    {
        for(int j=vt1[0].size()-1,k=vt2[i].size()-1;min(j,k)>=0;--j,--k)
        {
            if(vt1[0][j]>0&&vt2[i][k]>0)
            {
                cout<<vt1[0][j]<<" "<<vt2[i][k]<<endl;
                vt1[0].pop_back();
                vt2[i].pop_back();
            }
        }
    }
    for(int i=1;i<30;++i)
    {
        for(int j=vt1[i].size()-1,k=vt2[0].size()-1;min(j,k)>=0;--j,--k)
        {
            if(vt1[i][j]>0&&vt2[0][k]>0)
            {
                cout<<vt1[i][j]<<" "<<vt2[0][k]<<endl;
                vt1[i].pop_back();
                vt2[0].pop_back();
            }
        }
    }
    for(int i=0;i<1;++i)
    {
        for(int j=vt1[0].size()-1,k=vt2[0].size()-1;min(j,k)>=0;--j,--k)
        {
            if(vt1[0][j]>0&&vt2[0][k]>0)
            {
                cout<<vt1[0][j]<<" "<<vt2[0][k]<<endl;
                vt1[0].pop_back();
                vt2[0].pop_back();
            }
        }
    }
}
View Code

 

posted on 2019-09-15 22:01  有毒的粽子  阅读(166)  评论(0编辑  收藏  举报