zoukankan      html  css  js  c++  java
  • CoderForces999C-Alphabetic Removals

    C. Alphabetic Removals
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You are given a string ss consisting of nn lowercase Latin letters. Polycarp wants to remove exactly kk characters (knk≤n) from the string ss. Polycarp uses the following algorithm kk 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 kk times, thus removing exactly kk characters.

    Help Polycarp find the resulting string.

    Input

    The first line of input contains two integers nn and kk (1kn41051≤k≤n≤4⋅105) — the length of the string and the number of letters Polycarp will remove.

    The second line contains the string ss consisting of nn lowercase Latin letters.

    Output

    Print the string that will be obtained from ss after Polycarp removes exactly kk letters using the above algorithm kk times.

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

    Examples
    input
    Copy
    15 3
    cccaabababaccbc
    
    output
    Copy
    cccbbabaccbc
    
    input
    Copy
    15 9
    cccaabababaccbc
    
    output
    Copy
    cccccc
    
    input
    Copy
    1 1
    u
    
    output
    Copy

    AC代码为:

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=4e5+10;
    struct Node{
        int id,temp;
        char c;
    } node[maxn];
    bool cmp1(Node a,Node b)
    {
        return a.c==b.c? a.id<b.id : a.c-'a'<b.c-'a';
    }
    bool cmp2(Node a,Node b)
    {
        return a.id<b.id;
    }
    int n,k;
    char s1[maxn];
    int main()
    {
        scanf("%d%d",&n,&k);
        scanf("%s",s1);
        int len=strlen(s1);
        for(int i=0;i<len;i++)
        {
            node[i].c=s1[i];
            node[i].id=i;
            node[i].temp=0;
        }
        sort(node,node+len,cmp1);
        for(int i=0;i<k;i++) node[i].temp=1;
        sort(node,node+len,cmp2);
        for(int i=0;i<len;i++)
        {
            if(node[i].temp==1) continue;
            else printf("%c",node[i].c); 
        }
        printf(" ");
        
        return 0;
    }






  • 相关阅读:
    解决弹出的窗口window.open会被浏览器阻止的问题(自定义open方法)
    js call
    说说JSON和JSONP,也许你会豁然开朗
    按需加载/懒加载
    Sublime Text2 中Emmet(之前叫Zencoding)插件安装以及使用
    求数组中的最小值以及最小值的序列号
    软件工程概论作业03--将随机产生的表达式导入数据库
    软件工程作业01--四则运算1
    梦断代码阅读笔记
    学习进度条--第二周
  • 原文地址:https://www.cnblogs.com/csushl/p/9386537.html
Copyright © 2011-2022 走看看