This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int inf = 1e8;
const int maxn = 5005; // 7e4 + 10;
const int alpha = 10;
string s;
int n;
int nxt[alpha][maxn];
int opt[maxn];
int mem[maxn][maxn];
int cnt[maxn];
int dp(int now, int done) {
int jump = opt[done];
if(jump == n) return 0;
int &ans = mem[now][done];
if(ans != -1) return ans;
ans = inf;
for(int i = 0; i < alpha; i++) {
if(i != 'e' - 'a' && nxt[i][now] < n) {
int idx = nxt[i][now];
if(idx < jump) {
ans = min(ans, 2 + dp(idx, max(done, idx)));
} else {
ans = min(ans, 2 + dp(jump, idx) + cnt[idx] - cnt[jump]);
}
}
}
return ans;
}
int main() {
cin >> n >> s;
vector <int> aux (alpha, n);
for(int i = n - 1; i >= 0; i--) {
for(int j = 0; j < alpha; j++) {
nxt[j][i] = aux[j];
}
aux[s[i] - 'a'] = i;
}
int var = n;
for(int i = n - 1; i >= 0; i--) {
opt[i] = var;
if(i > 0 && s[i - 1] == 'e' && s[i] != 'e') {
var = i;
}
}
int total = 0;
for(int i = 0; i < n; i++) {
total += (s[i] != 'e');
cnt[i] = total;
}
int ans = 2 * count(s.begin(), s.end(), 'e');
memset(mem, -1, sizeof mem);
cout << ans + dp(0, 0) << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |