1141D - Colored Boots

博主在代码操作中,对于匹配顺序理解有误,原本应先同色匹配,而博主理解为先匹配其他色再同色匹配。同时,对queue.empty()函数理解出错,忘记空时返回1。反映出长时间未编程易犯低级错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先同色匹配,再?匹配剩下的,再?匹配?
我的错误
1.我的理解是先?匹配其他色,再?匹配?,再同色匹配同色。。。。不知道哪里出错
2.对queue.empty()理解出错。。。。当空返回1。好久 没有打代码了,犯下低级错误。

#include<bits/stdc++.h>
using namespace std;
queue<int> leftshoe[27];
queue<int> rightshoe[27];
vector<int> zuo;
vector<int> you;
int leftsize[27]={0};
int rightsize[27]={0};
int par[150000+5][2]={0};
char lss[150000+5];
char rss[150000+5];
int main(void){
    int n;
    cin>>n;
    scanf("%s",lss);
    scanf("%s",rss);

    for(int i=0;i<n;i++){
        char a;
        a=lss[i];
        if(a=='?'){
            leftshoe[0].push(i+1);//1号2号等
            leftsize[0]++;
            continue;
        }
        leftshoe[a-'a'+1].push(i+1);
        leftsize[a-'a'+1]++;
    }
    for(int i=0;i<n;i++){
        char a;
        a=rss[i];
        if(a=='?'){
            rightshoe[0].push(i+1);
            rightsize[0]++;
            continue;
        }
        rightshoe[a-'a'+1].push(i+1);
        rightsize[a-'a'+1]++;
    }
    int zuoduo=0,youduo=0;



    for(int i=1;i<27;i++){
        if(leftsize[i]>rightsize[i]){
//            zuoduo+=leftsize[i]-rightsize[i];
            zuo.push_back(i);//左边多的鞋子编号a,b,c等
        }
        if(leftsize[i]<rightsize[i]){
//            youduo+=rightsize[i]-leftsize[i];
            you.push_back(i);//右边多的鞋子编号
        }
    }

    int ct=0;
//    for(int i=0;i<27;i++){
//        while(!leftshoe[i].empty()){
//           int a=leftshoe[i].front() ;
//           leftshoe[i].pop();
//           printf("lefti=%d   bianhao=%d\n",i,a);
//        }
//        while(!rightshoe[i].empty()){
//           int a=rightshoe[i].front() ;
//           rightshoe[i].pop();
//           printf("righti=%d   bianhao=%d\n",i,a);
//        }
//
//    }

    for(int i=1;i<27;i++){
//        printf("%d",i);
        while((!leftshoe[i].empty())&&(!rightshoe[i].empty())){
            int aj=leftshoe[i].front();
            leftshoe[i].pop();
            int bj=rightshoe[i].front();
            rightshoe[i].pop();
            par[ct][0]=aj;
            par[ct][1]=bj;
            ct++;
//            printf("cnm\n");
        }
    }
    //?与右边多鞋子匹配


    for(int i=0;i<you.size();i++){
//        printf("%d",i);
        if(leftshoe[0].empty()){
                break;
        }
        while(!rightshoe[you[i]].empty()){
            int bj=rightshoe[you[i]].front();
            rightshoe[you[i]].pop();
            int aj=leftshoe[0].front();
            leftshoe[0].pop();
            par[ct][0]=aj;
            par[ct][1]=bj;
            ct++;
            rightsize[you[i]]--;
            if(leftshoe[0].empty()){
                break;
            }
        }
        if(leftshoe[0].empty()){
                break;
            }
    }

    //左边鞋子与?匹配
    for(int i=0;i<zuo.size();i++){
//        printf("%d",i);
        if(rightshoe[0].empty()){
                break;
            }
        while(!leftshoe[zuo[i]].empty()){
            int aj=leftshoe[zuo[i]].front();
            leftshoe[zuo[i]].pop();
            int bj=rightshoe[0].front();
            rightshoe[0].pop();
            par[ct][0]=aj;
            par[ct][1]=bj;
            ct++;
            leftsize[zuo[i]]--;
            if(rightshoe[0].empty()){
                break;
            }
        }
        if(rightshoe[0].empty()){
                break;
            }
    }
//    printf("left0size=%d\n",leftshoe[0].size());
//    while(!leftshoe[0].empty()){
//            printf("%d\n",leftshoe[0].front());
//            leftshoe[0].pop();
//        }
    //剩下鞋子匹配
    for(int i=0;i<1;i++){
//        printf("%d",i);
        while((!leftshoe[i].empty())&&(!rightshoe[i].empty())){
            int aj=leftshoe[i].front();
            leftshoe[i].pop();
            int bj=rightshoe[i].front();
            rightshoe[i].pop();
            par[ct][0]=aj;
            par[ct][1]=bj;
            ct++;
//            printf("cnm\n");
        }
    }

    printf("%d\n",ct);
    for(int i=0;i<ct;i++){
        printf(i==0?"%d %d":"\n%d %d",par[i][0],par[i][1]);
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值