You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

31 lines
851 B
C++

#define _CRT_SECURE_NO_WARNINGS
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
#include <bits/stdc++.h>
using namespace std;
const int N = 5000050;
char s[N];
deque<int> pos[26];
int main()
{
const auto sz = acos(-1);
int k;
scanf("%s%d", s, &k);
int n = strlen(s);
for (int i = 0; i < n; i++) pos[s[i] - 'a'].push_back(i);
int lmost = 0, lsec = 0;
while (pos[lmost].empty()) lmost++;
lsec = lmost + 1;
while (lsec < 26 && pos[lsec].empty()) lsec++;
for (int i = 0; i < n; i++)
{
while (pos[lmost].size() && pos[lmost].front() < i) pos[lmost].pop_front();
if (pos[lmost].empty())
{
while (pos[lmost].empty()) lmost++;
lsec = lmost + 1;
while (lsec < 26 && pos[lsec].empty()) lsec++;
}
}
return 0;
}