1. 程式人生 > >Codeforces Round #490 (Div. 3)-C. Alphabetic Removals(思維)

Codeforces Round #490 (Div. 3)-C. Alphabetic Removals(思維)

題目連結:http://codeforces.com/contest/999/problem/C

題意:給了n和k,然後輸入一個長度為n的字串,然後有k次操作,從字串中依次按abcd...的順序刪除字元,最後輸出k次操作後的字串。

思路:sort排序後的前k個字元就是我們要刪除的字元,用map標記要刪除的字元,直接輸出就好了。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define read(x) scanf("%d",&x)
const int maxn = 1e6+7;
map<char,int>mp;
int main()
{
    int n, k; read(n); read(k);
    string s, t; cin >> s; t = s;
    sort(t.begin(),t.end());
    for(int i = 0; i < k; i++) mp[t[i]]++;
    for(int i = 0; i < n; i++)
    {
        if(!mp[s[i]]) cout << s[i];
        else mp[s[i]]--;
    }
}