This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// #pragma GCC target ("avx,avx2,fma")
// #pragma GCC optimize ("Ofast,inline") // O1 - O2 - O3 - Os - Ofast
// #pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define per(i, a, b) for (int i = (b - 1); i >= (a); --i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) x.size()
#define pb push_back
#define debug(x) cout << #x << " = " << x << endl
#define umap unordered_map
#define uset unordered_set
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
const int INF = 1'000'000'007;
int n, cnt = 0, memo[5001][5001], nxt[5001][130], prev_pos[130], prev_skip_e = 0, skip_e[5001],prefix[5001];
vi e;
string str;
int dp(int pos, int taken) {
if (taken == cnt) return 0;
int &ans = memo[pos][taken];
if (ans != -1) return memo[pos][taken];
ans = INF;
if (e[taken] <= pos) {
int nxt_taken = prefix[pos];
int nxt_pos = skip_e[e[taken]];
// cout << "pos = " << pos << " taken = " << taken << " nxt_taken = " << nxt_taken << " nxt_pos = " << nxt_pos << endl;
ans = (pos - e[taken]) + dp(nxt_pos, nxt_taken);
}
rep(i, 'a', 'k') {
if (i == 'e') continue;
int npos = nxt[pos][i];
if (npos != -1) ans = min(ans, 2 + dp(npos, taken));
}
// cout << "pos = " << pos << " taken = " << taken << " ans = " << ans << endl;
return ans;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
memset(prev_pos, 0, sizeof(prev_pos));
memset(prefix, 0, sizeof(prefix));
memset(memo, -1, sizeof(memo));
memset(nxt, -1, sizeof(nxt));
cnt = 0;
cin >> n >> str;
rep(i, 0, n) {
char let = str[i];
rep(j, prev_pos[let], i) nxt[j][let] = i;
prev_pos[let] = i;
if (let != 'e') {
rep(j,prev_skip_e,i)skip_e[j]=i;
prev_skip_e = i;
} else{
++cnt;
e.pb(i);
++prefix[i];
}
prefix[i+1]=prefix[i];
}
cout << dp(0, 0) + cnt << endl;
return 0;
}
Compilation message (stderr)
vim.cpp: In function 'int main()':
vim.cpp:75:25: warning: array subscript has type 'char' [-Wchar-subscripts]
75 | rep(j, prev_pos[let], i) nxt[j][let] = i;
| ^~~
vim.cpp:8:36: note: in definition of macro 'rep'
8 | #define rep(i, a, b) for (int i = (a); i < (b); ++i)
| ^
vim.cpp:75:41: warning: array subscript has type 'char' [-Wchar-subscripts]
75 | rep(j, prev_pos[let], i) nxt[j][let] = i;
| ^~~
vim.cpp:77:18: warning: array subscript has type 'char' [-Wchar-subscripts]
77 | prev_pos[let] = i;
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |