经观察发现只要有AC和AK,将这两个字母交换就会减少一个数量。利用前缀和求原序列数量即可。
#include <bits/stdc++.h>
using namespace std;
#define int long long
string str;
int m, sum[200005], ans;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> str >> m;
for (int i = 0; i < str.size(); i++){
sum[i + 1] = sum[i] + (str[i] == 'A');
}
for (int i = 0; i < str.size(); i++){
if (str[i] == 'C' || str[i] == 'K') ans += sum[i];
} cout<<max(ans-m,0ll);
}