알파벳 개수를 센 다음 a부터 k개수 만큼 뺀다.
그 다음 남은 개수만큼 출력하면 될 꺼라 생각했음..
하지만 제출하고 생각해보니 이렇게 짜면 앞에서 남은 개수만큼 출력하게 된다.
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <functional>
#include <string>
using namespace std;
using i64 = long long;
int main() {
int n, k;
scanf("%d %d", &n, &k);
string s;
cin >> s;
vector <int> alpha(26, 0);
vector <int> origin(26, 0);
for (int i = 0; i < n; i++)
{
alpha[s[i] - 'a']++;
origin[s[i] - 'a']++;
}
int count = 0;
for (int i = 0; i < k; )
{
if (alpha[count] != 0)
{
alpha[count]--;
i++;
}
if (alpha[count] == 0)
count++;
}
int sum = n - k;
for (int i = 0; i < n; i++)
{
if (origin[s[i] - 'a'] == alpha[s[i] - 'a'] && sum >= 0)
{
sum--;
cout << s[i];
origin[s[i] - 'a']--;
alpha[s[i] - 'a']--;
}
else
{
origin[s[i] - 'a']--;
}
}
return 0;
}
음.. 내 생각과는 다르게 n개만큼 문자를 출력하게 된다.
그래서 총 출력해야 할 개수를 구하고 그 개수만큼만 출력하게 했음
#include <iostream>
#include <queue>
using namespace std;
queue<int> q[26];
bool removed[400005];
int main() {
int n, k;
string s;
cin >> n >> k >> s;
for (int i = 0; i < s.size(); i++)
q[s[i] - 'a'].push(i);
for (int i = 0; i < k ; i++)
{
for (int c = 0; c < 26; c++)
{
if (q[c].empty())
continue ;
removed[q[c].front()] = true;
q[c].pop();
break ;
}
}
for (int i = 0; i < s.size(); i++)
{
if (removed[i])
continue ;
cout << s[i];
}
return (0);
}
'코드포스' 카테고리의 다른 글
[코드포스 Practice15] C. Odd sum (0) | 2020.03.21 |
---|---|
[코드포스 Practice15] B. Chtholly's request (0) | 2020.03.21 |
[코드포스 Practice15] 후기 (0) | 2020.03.20 |
[코드포스 Practice14] E. Balanced Ternary String (0) | 2020.03.14 |
[코드포스 Practice14] D. Secret Passwords (0) | 2020.03.14 |