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;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
const int maxn = 500 + 10;
int nex[maxn][12];
int dp[maxn][maxn];
int main(){
ios_base::sync_with_stdio(false);
int n;
string s;
cin >> n >> s;
for (int i = 0; i < 10; i++){
int last = -1;
for (int j = n-1; j >= 0; j--){
if (last == -1)
nex[j][i] = j;
else
nex[j][i] = last;
if ((int)(s[j] - 'a') == i and s[j] != 'e')
last = j;
}
}
vector<int> e;
for (int i = 0; i < n; i++)
if (s[i] == 'e')
e.push_back(i);
if (e.empty())
return cout << 0 << endl, 0;
int laste = e.back();
int m = e.size();
memset(dp, 63, sizeof dp);
dp[0][0] = 0;
for (int i = 0; i < m; i++){
for (int j = 0; j <= i; j++){
int idx = 0;
if (j > 0){
idx = e[j-1];
while (s[idx] == 'e')
idx ++;
}
int cnt = 0;
while (idx < laste){
int mxm = idx;
cnt += 2;
for (int c = 0; c < 10; c++){
if (nex[idx][c] == idx)
continue;
mxm = max(mxm, nex[idx][c]);
int it = lower_bound(e.begin(), e.end(), nex[idx][c]) - e.begin();
if (it == i)
continue;
dp[it][i+1] = min(dp[it][i+1], dp[i][j] + cnt + (nex[idx][c]-e[i]) + (it-i));
}
idx = mxm;
}
}
}
int answer = 4*n;
for (int i = 1; i <= m; i++)
answer = min(answer, dp[m][i]);
cout << answer << 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... |