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>
#define INF 10000000
using namespace std;
int n;
string s;
int memo[505][505];
int solve(int cur, int deleted) {
if (deleted >= n - 1) return 0;
if (memo[cur][deleted] != -1) return memo[cur][deleted];
if (cur > deleted) {
int lastE = -1;
int cntE = 0;
for (int i = cur - 1; i > deleted; --i) {
if (s[i] == 'e') {
lastE = i;
++cntE;
}
}
if (lastE == -1) return memo[cur][deleted] = solve(cur, cur);
deleted = cur;
while (deleted < n - 1 && s[deleted + 1] != 'e') ++deleted;
return memo[cur][deleted] = cur - lastE + cntE + solve(lastE, deleted);
}
int rs = INF;
for (char c = 'a'; c <= 'j'; ++c) {
if (c == 'e') continue;
int jumpTo = -1;
for (int i = cur + 1; i < n; ++i) {
if (s[i] == c) {
jumpTo = i;
break;
}
}
if (jumpTo == -1) continue;
rs = min(rs, 2 + solve(jumpTo, deleted));
}
return memo[cur][deleted] = rs;
}
int main() {
cin >> n >> s;
fill(memo[0], memo[0] + 505 * 505, -1);
cout << solve(0, 0) << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |