fork download
  1. #include <iostream>
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. long int ar[26]={0}; //to store frequency of 26 alphabets
  5.  
  6.  
  7. int main(void)
  8. {
  9. long int n,k,i;
  10. scanf("%ld %ld",&n,&k);
  11. char s[n+1];
  12. scanf(" %s",s);
  13. char sorted[n+1]; // to store sorted characters in alphabetical order
  14. strcpy(sorted,s);
  15. sort(sorted,sorted+n);
  16. for( i=0;i<strlen(s);i++)
  17. {
  18. char temp=sorted[i];
  19. ar[temp-97]+=1; // increments occurence of alphabet (temp)
  20.  
  21. }
  22. long int count=0;
  23. while(count<26 && k>0)
  24. {
  25. int t=ar[count];
  26. if(t>0)
  27. {
  28. if(k>=t) // if k > total occurence of alphabet then
  29. // all occurences of this alphabet will be removed.
  30. {
  31.  
  32. k=k-t;
  33. }
  34. else
  35. {
  36. ar[count]=k; // otherwise t occurences willbe
  37. // reduced by k
  38. k=0;
  39. }
  40. }
  41. count++;
  42. }
  43. if(count<26)
  44. {
  45. for(i=count;i<26;i++) // if k becomes zero before reaching
  46. // last character all rem. characters
  47. ar[i]=0; // will remain as it is.
  48. }
  49. for(i=0;i<strlen(s);i++)
  50. {
  51.  
  52. char temp=s[i];
  53. if(ar[temp-97]>0)
  54. {
  55. // cout<<i<<" "<<ar[temp-97]<<endl;
  56. s[i]='A'; // to mark its removal later.
  57. ar[temp-97]=ar[temp-97]-1;
  58.  
  59. }
  60. }
  61.  
  62. for(i=0;i<strlen(s);i++)
  63. {
  64. if(s[i]!='A') //if 'A' not printed.
  65. {
  66. printf("%c",s[i]);
  67. }
  68. }
  69.  
  70. return 0;
  71. }
Runtime error #stdin #stdout 0s 4384KB
stdin
Standard input is empty
stdout
Standard output is empty