• CodeForces


    C - Alphabetic Removals

     

    You are given a string 

    s consisting of n lowercase Latin letters. Polycarp wants to remove exactly k characters (kn≤n) from the string s. Polycarp uses the following algorithm k times:
    • if there is at least one letter 'a', remove the leftmost occurrence and stop the algorithm, otherwise go to next item;
    • if there is at least one letter 'b', remove the leftmost occurrence and stop the algorithm, otherwise go to next item;
    • ...
    • remove the leftmost occurrence of the letter 'z' and stop the algorithm.

    This algorithm removes a single letter from the string. Polycarp performs this algorithm exactly k times, thus removing exactly k characters.

    Help Polycarp find the resulting string.

    Input

    The first line of input contains two integers n and k (1kn4105) — the length of the string and the number of letters Polycarp will remove.

    The second line contains the string s consisting of n lowercase Latin letters.

    Output

    Print the string that will be obtained from s after Polycarp removes exactly kkletters using the above algorithm k times.

    If the resulting string is empty, print nothing. It is allowed to print nothing or an empty line (line break).

    Examples
    Input
    15 3
    cccaabababaccbc
    
    Output
    cccbbabaccbc
    
    Input
    15 9
    cccaabababaccbc
    
    Output
    cccccc
    
    Input
    1 1
    u
    
    Output

            这个题就是给你一个已知长度的字符串,给你一个数k,要求要从这里面删去k个字符,删去的这k个字符要求是从a开始删除,如果a没有删完,那么就不能删除b,依次进行,直到删完k个字符就可以了。

        

    #include <bits/stdc++.h>
    
    using namespace std;
    
    char s[400005];
    int a[1000];  // a用来标记每个字母出现的次数
    int main()
    {
        int n, k;
        while(~scanf("%d %d",&n, &k))
        {
            getchar();
            memset(a, 0,sizeof(a));
            for(int i = 0; i < n; i ++)
            {
                scanf("%c",&s[i]);
                a[s[i]] ++;
            }
            if(k >= n) printf("
    ");  //如果需要删除的k大于n,就全部删除没了
            else
            {
                int num = 0;
                for(int i = 97; ; i ++) // 从a开始删除,如果满足k>=a[i],把所有的字符都删去即可
                {
                    if(k >= a[i])
                    {
                        num ++;
                        k = k - a[i];
                    }   
                    else break;
                }
                for(int i = 0; i < n; i ++)
                {
    
                    if(s[i] < 97 + num);  //删除掉的不再输出
                    else
                    {
                        if(s[i] == 97 + num)  // 没有全部删除完成,继续删除这个字符
                        {
                            if(k > 0)k--;
                            else printf("%c",s[i]);
                        }
                        else printf("%c",s[i]); // 输出不需要删除的
                    }
    
                }
                printf("
    ");
            }
        }
        return 0;
    }
    发现可以省去好多代码:
    #include <bits/stdc++.h>
    using namespace std;
    int n,m;
    char str[400005];
    
    int main()
    {
      cin>>n>>m;
      cin>>str;
      for(int i=0;i<=25;i++){
        for(int j=0;j<n&&m;j++){
          if(str[j] == 'a' + i){
            str[j] = '0';
            m--;
          }
        }
      }
      for(int i=0;i<n;i++){
        if(str[i] != '0')cout<<str[i];
      }
      return 0;
    }
    


  • 相关阅读:
    Android——问题解决之adb not responding;adb不是内部或外部命令;path变量的默认值为多少
    PHP——小尾巴之权限管理
    Android——Android studio项目中如何查看R.java文件(转)
    Genymotion常见问题整合与解决方案(转)
    Android Studio简单设置(转)
    Android——配置环境变量
    Android——寄存器和存储器的区别
    Android——手机尺寸相关的概念 +尺寸单位+关于颜色
    Android——区别DVM与JVM (2)
    Psql 安装问题
  • 原文地址:https://www.cnblogs.com/lcchy/p/10139645.html
Copyright © 2020-2023  润新知