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 optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,fma,bmi,bmi2,sse4.2,popcnt,lzcnt")
#include <bits/stdc++.h>
#define taskname ""
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define int long long
#define ll long long
#define ld long double
#define pb push_back
#define ff first
#define ss second
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define isz(x) (int)x.size()
using namespace std;
const int mxN = 2e5 + 5;
const int mod[] = {1000000007, 100000009};
const int base = 31;
const ll oo = 1e18;
string s;
int n, a, b, c, dp[2505][2505];
int hsh[2505][2], power[2505][2];
void gen_hash() {
power[0][0] = power[0][1] = 1;
for(int i = 1; i <= n; ++i) {
for(int j = 0; j < 2; ++j) {
hsh[i][j] = (hsh[i - 1][j] * base + s[i] - 'a') % mod[j];
power[i][j] = power[i - 1][j] * base % mod[j];
}
}
}
array<int, 2> get_hash(int l, int r) {
array<int, 2> res;
for(int i = 0; i < 2; ++i) {
res[i] = hsh[r][i];
res[i] -= hsh[l - 1][i] * power[r - l + 1][i] % mod[i];
res[i] = res[i] < 0 ? res[i] + mod[i] : res[i];
}
return res;
}
void solve() {
cin >> n >> s >> a >> b >> c;
memset(dp, 0x3f, sizeof dp);
s = " " + s;
for(int i = 1; i <= n; ++i)
dp[i][i] = a;
gen_hash();
for(int len = 1; len <= n; ++len) {
map<array<int, 2>, vi> mp;
vi nxt(n - len + 2);
for(int l = 1; l + len - 1 <= n; ++l) {
int r = l + len - 1; mp[get_hash(l, r)].emplace_back(l);
dp[l][r] = min({dp[l][r], dp[l][r - 1] + a, dp[l + 1][r] + a});
}
for(auto &[val_hash, v] : mp) {
int sz = isz(v), idx = 0;
for(int tmp : v) {
while(idx < sz && v[idx] < tmp + len) ++idx;
nxt[tmp] = (idx == sz ? 0 : v[idx]);
}
}
for(int l = 1; l + len - 1 <= n; ++l) {
int idx = nxt[l], val = dp[l][l + len - 1], cnt = 1;
while(idx) {
int idx_r = idx + len - 1; ++cnt, idx = nxt[idx];
dp[l][idx_r] = min(dp[l][idx_r], val + b + c * cnt + a * (idx_r - l + 1 - cnt * len));
}
}
}
cout << dp[1][n] << endl;
}
signed main() {
#ifndef CDuongg
if(fopen(taskname".inp", "r"))
assert(freopen(taskname".inp", "r", stdin)), assert(freopen(taskname".out", "w", stdout));
#else
freopen("bai3.inp", "r", stdin);
freopen("bai3.out", "w", stdout);
auto start = chrono::high_resolution_clock::now();
#endif
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1; //cin >> t;
while(t--) solve();
#ifdef CDuongg
auto end = chrono::high_resolution_clock::now();
cout << "\n"; for(int i = 1; i <= 100; ++i) cout << '=';
cout << "\nExecution time: " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
#endif
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |