codeforces 拼手速题1

 

 

题目链接:https://codeforces.com/problemset/problem/1141/D

 

 

D. Colored Boots
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 not compatible: ('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.

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

 

题意:输入一个整数n,然后再分别输入长度为n的两个字符串, 其中字符串均是由小写字母和  ‘?’组成。

  输出最大匹配数,再输出每个匹配数对于的地址。

   对于匹配数:  在两个字符串中分别取一个字符,同字符可以进行匹配,但是一个字符只能被匹配一次、例如: 字符串1:aaaa  字符串2:aaa3       最后的匹配数为3

          特例元素 ‘?’      ‘?’可以和任一元素进行匹配。            例如:abz?? 与   acc??       匹配数是5

 

 

 

思路:暴力水题,拼编码能力+手速         虽然说是一遍就过,但是编码编了很久。。。。。

 

 

#include<iostream>
#include<cstdio>
#include<ctime>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<stack>
#include<map> 
#include<algorithm>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define Mem0(x) memset(x,0,sizeof(x))
#define Mem1(x) memset(x,-1,sizeof(x))
#define MemX(x) memset(x,0x3f,sizeof(x))
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f;
const double pi=acos(-1.0);

const int MAX=150000+10;
char l,r;

queue<int> q1[30],q2[30];//1~26  存a~z     0存? 
void init()
{
    for (int i=0;i<30;i++){
        while (!q1[i].empty())
            q1[i].pop();
    }
    for (int i=0;i<30;i++){
        while (!q2[i].empty())
            q2[i].pop();
    }
    return ;
}

struct s{
    int l,r;
}ans1[MAX];

int main()
{
    int n,len1,len2;
    scanf("%d",&n);
    len1=len2=0;
    init();
    getchar();
    for (int i=1;i<=n;i++){
        scanf("%c",&l);
        if (l=='?'){
            q1[0].push(i);
        }
        else{
            q1[l-'a'+1].push(i);
            len1++;     
        } 
            
    }
    getchar();
    ll ans=0;
    for (int i=1;i<=n;i++){
        scanf("%c",&r);
        if (r=='?'){
            q2[0].push(i);
        }
        else{
            if (!q1[r-'a'+1].empty()){
                ans++;
                ans1[ans].l=q1[r-'a'+1].front();
                ans1[ans].r=i;
                q1[r-'a'+1].pop();
                len1--;
            }
            else{
                len2++;
                q2[1].push(i);//存不同字符 
            }
                
        }
    }
    while (!q1[0].empty()){
        if (len2>0){     //l中的?  和r的字母匹配 
            len2--;
            ans++;
            ans1[ans].l=q1[0].front();
            ans1[ans].r=q2[1].front();
            q2[1].pop(); 
        }
        else{   //两个?匹配 
            ans++;
            ans1[ans].l=q1[0].front();
            ans1[ans].r=q2[0].front();
            q2[0].pop();
        }
        q1[0].pop();
    }
    
    while (!q2[0].empty()){
        if (len1>0){
            len1--;
            ans++;
            for (int i=1;i<=26;i++){
                if (!q1[i].empty()){
                    ans1[ans].l=q1[i].front();
                    ans1[ans].r=q2[0].front();
                    q1[i].pop();
                    break;
                }
            }
        }
        q2[0].pop();
    }
    cout<<ans<<endl;
    for (int i=1;i<=ans;i++){
        cout<<ans1[i].l<<" "<<ans1[i].r<<endl;
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/q1204675546/p/10951614.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【优质项目推荐】 1、项目代码均经过严格本地测试,运行OK,确保功能稳定后才上传平台。可放心下载并立即投入使用,若遇到任何使用问题,随时欢迎私信反馈与沟通,博主会第一时间回复。 2、项目适用于计算机相关专业(如计科、信息安全、数据科学、人工智能、通信、物联网、自动化、电子信息等)的在校学生、专业教师,或企业员工,小白入门等都适用。 3、该项目不仅具有很高的学习借鉴价值,对于初学者来说,也是入门进阶的绝佳选择;当然也可以直接用于 毕设、课设、期末大作业或项目初期立项演示等。 3、开放创新:如果您有一定基础,且热爱探索钻研,可以在此代码基础上二次开发,进行修改、扩展,创造出属于自己的独特应用。 欢迎下载使用优质资源!欢迎借鉴使用,并欢迎学习交流,共同探索编程的无穷魅力! 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值