zoukankan      html  css  js  c++  java
  • C

    题目链接:

    You are given a string ss consisting of nn lowercase Latin letters. Polycarp wants to remove exactly kk characters (k≤nk≤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 kkcharacters.

    Help Polycarp find the resulting string.

    Input

    The first line of input contains two integers nn and kk (1≤k≤n≤4⋅1051≤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

    15 3
    cccaabababaccbc
    

    Output

    cccbbabaccbc
    

    Input

    15 9
    cccaabababaccbc
    

    Output

    cccccc
    

    Input

    1 1
    u
    

    Output

    题目大意:就是给你一个字符串,要求你去掉k个字符,去掉的规则就是从a开始,如果有a,从a开始去,如果a没有了就从b开始,一次次的递推。

    思路:一开始,我是打算一个一个的去,然后一直循环到0结束。然后忘记了看时间复杂度,如果按照我那个计算的话,时间复杂度是8*10 的十次方,这玩意。。。。肯定超时啊,我还搁那傻了吧唧的一个劲的优化,然后就是4发tle。

    比完赛后,问别人有咩有比较好的算法,可以这样来做。直接按26个英文按时逐个递减,剪完a再减b,依次往下进行,这样的话时间复杂度就大大的降低了。

    代码如下:

    #include<iostream>#include<string>#include<cstring>#include<map>#include<cmath>using namespace std;map<char,int >wakaka;bool Exit[400005];int main(){ int n,t; cin>>n>>t; string s; cin>>s; memset(Exit,false,sizeof(Exit)); for(int i=0; i<n; i++) { wakaka[s[i]]++; } int temp=-1; while(t) { temp++; char str=char(temp+97); if(wakaka[str]==0)continue; for(int i=0; i<s.length(); i++) { if(t==0)break; if(s[i]==str&&Exit[i]==false) { Exit[i]=true; t--; } } } for(int i=0; i<s.length(); i++) { if(Exit[i]==false)cout<<s[i]; } cout<<endl; return 0;}

  • 相关阅读:
    【EF学习笔记11】----------查询中常用的扩展方法
    .NET 扩展方法
    【EF学习笔记10】----------主从表级联操作
    【EF学习笔记09】----------使用 EntityState 枚举标记实体状态,实现增删改查
    【EF学习笔记08】----------加载关联表的数据 显式加载
    【EF学习笔记07】----------加载关联表的数据 贪婪加载
    【EF学习笔记06】----------加载关联表的数据 延迟加载
    Linux内核ROP姿势详解(二)
    见微知著(三):解析ctf中的pwn--Fastbin和bins的溢出
    见微知著(二):解析ctf中的pwn--怎么利用double free
  • 原文地址:https://www.cnblogs.com/letlifestop/p/10263002.html
Copyright © 2011-2022 走看看