[CodeForces-1141D] Colored Boots

题意:从两串长度为n的字符串中找到相同的字符,问号可以和任意字符匹配,然后输出最多可以匹配多少个字符(每个字符最多匹配一次),输出个数x并在接下来x行中输出x对相匹配的字符分别在两串字符中的位置。

题解:stl的运用,开始就用了个vector,没写出来,然后参考了 https://www.cnblogs.com/YDDDD/p/10570981.html

这里的做法。用map<char,queue<int> >来储存第一个串中各个字母的所有位置,然后对第二个串进行循环处理,先处理不是‘?’的字符,如果字符在第一个串中存在或者第一个串中还有‘?’剩余,那就把这个字符的位置和map[' ']的队列中第一个元素(即第一个串中的位置)存入pair<int,int>数组中,同时pop出队列中的第一个元素。接着再处理‘?’,最后把pair数组中的元素依次输出。

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    int n;
    int x[30],y[30];
    memset(x,0,sizeof(x));memset(y,0,sizeof(y));
    map<char,queue<int> > imap;
    pair<int,int> ans[150010];
    string a,b;
    cin>>n;
    cin>>a;
    for(int i=0;i<n;i++)
        imap[a[i]].push(i+1);
    cin>>b;
    int cnt=0;
    for(int i=0;i<n;i++)
    {
        if(b[i]!='?')
        {
            if(imap[b[i]].size()>0)
            {
                ans[cnt].second=i+1;
                ans[cnt++].first=imap[b[i]].front();

                imap[b[i]].pop();
            }
            else
            {
                if(imap['?'].size()>0)
                {
                    ans[cnt].second=i+1;
                    ans[cnt++].first=imap['?'].front();
                    imap['?'].pop();
                }
            }
        }
    }
    for(int i=0;i<n;i++)
    {
        bool flag=false;
        if(b[i]=='?')
        {
            for(int j=0;j<26;j++)
            {
                if(imap[char('a'+j)].size()>0)
                {
                    flag=true;
                    ans[cnt].second=i+1;
                    ans[cnt++].first=imap[char('a'+j)].front();
                    imap[char('a'+j)].pop();
                    break;
                }
            }
            if(flag==false)
            {
                if(imap['?'].size()>0)
                {
                    ans[cnt].second=i+1;
                    ans[cnt++].first=imap['?'].front();
                    imap['?'].pop();
                }
            }
        }
    }
    cout<<cnt<<endl;
    for(int i=0;i<cnt;i++)
        cout<<ans[i].first<<' '<<ans[i].second<<endl;
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值