Codeforces Round #490 (Div. 3) C

传送门http://codeforces.com/contest/999/problem/C

给一个长度为n的字符串s,要删除k个字符。删除规则:从头开始,先把a删完,再把b删完......最后(如果可能)把z删完。求删后的串。

由于长度达到了4*105,我们不能使用最直接的方法,虽然我的方法也是模拟2333

先计数,每个字符出现次数,通过“前缀和”求出哪些字符要完全删完,哪个只删一部分。然后扫描原串,弄出新串,输出。

由于忘记了string的用法,我用char数组做的QAQ

 1 #include <iostream>
 2 #include <string>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <cstdio>
 6 using namespace std;
 7 const int maxn = 400004;
 8 char s[maxn], ans[maxn];
 9 int n, k, a[30], sto, res, sum, len;
10 int main()
11 {
12     scanf("%d%d%s", &n, &k, s);
13     for (int i = 0; i < n; ++i) ++a[s[i] - 'a'];
14     for (int i = 0; i < 26; ++i)
15     {
16         sum += a[i];
17         if (sum >= k)
18         {
19             sto = i;
20             res = k + a[i] - sum;
21             break;
22         }
23     }
24     //cout << sto;
25     for (int i = 0; i < n; ++i)
26     {
27         if (s[i] - 'a' > sto) ans[len++] = s[i];
28         else if (s[i] - 'a' == sto)
29         {
30             --res;
31             if (res < 0) ans[len++] = s[i];
32         }
33     }
34     cout << ans;
35     return 0;
36 }

猜你喜欢

转载自www.cnblogs.com/lightgreenlemon/p/9211787.html