CF 999 C.Alphabetic Removals
找出减到第几个字母,和这个字母要减的个数
1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 using namespace std; 5 6 char s[400005]; 7 char ans[4000005]; 8 int f[26]; 9 int main() 10 { 11 memset(f,0,sizeof(f)); 12 int x = 0, sum = 0, n, k, cnt = -1, jian; 13 cin >> n >> k; 14 for(int i = 0; i < n; i++){ 15 cin >> s[i]; 16 f[s[i]-'a']++; 17 } 18 for(int i = 0; i < 26; i++){ 19 sum += f[i]; 20 //cout << " sum : " << sum << endl; 21 if(sum == k) 22 cnt = i; 23 else if(sum > k){ 24 cnt = i; 25 jian = f[i] - sum + k; 26 break; 27 } 28 } 29 //cout << " cnt :" << cnt << " jian : " << jian << endl; 30 for(int i = 0; i < n; i++){ 31 if((s[i]-'a') < cnt) continue; 32 else if((s[i]-'a') == cnt){ 33 if(jian > 0) jian--; 34 else ans[x++] = s[i]; 35 }else 36 ans[x++] = s[i]; 37 } 38 if(x == 0) return 0; 39 else 40 for(int i = 0; i < x; i++) 41 cout << ans[i]; 42 return 0; 43 }
果然也只有晚上能写几个水题,最近这几天真的是 完全不想学习。qwq