zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    Android面试题
    java面试题大全
    关于索引的sql语句优化之降龙十八掌
    java动态代理的实现
    java动态代理
    进程与线程
    SqlServer聚合函数
    2015年创业中遇到的技术问题:21-30
    hadoop集群ambari搭建(2)之制作hadoop本地源
    Android录屏命令、Android录Gif、Android录视频
  • 原文地址:https://www.cnblogs.com/q1204675546/p/10951614.html
Copyright © 2011-2022 走看看