前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Codeforces Round #547 (Div. 3)D. Colored Boots

Codeforces Round #547 (Div. 3)D. Colored Boots

作者头像
glm233
发布2020-09-28 10:34:56
3710
发布2020-09-28 10:34:56
举报
文章被收录于专栏:glm的全栈学习之路

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存出现字符的位置,首先是普通字符(都不含'?')能匹配就匹配,之后是第一个字符串中'?'与剩下第二个字符串中还剩下的遍历到就匹配,因为?字符和任何都行,之后第二个字符串中'?'与剩下第一个字符串中还剩下的遍历到就匹配

代码语言:javascript
复制
// 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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/08/09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档