이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*
#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 = 1e9 + 9;
const int base = 29;
const ll oo = 1e18;
string s;
int n, a, b, c, dp[2505][2505];
int hsh[2505], power[2505];
void gen_hash() {
power[0] = 1;
for(int i = 1; i <= n; ++i) {
hsh[i] = (hsh[i - 1] * base + s[i] - 'a') % mod;
power[i] = power[i - 1] * base % mod;
}
}
int get_hash(int l, int r) {
int res = hsh[r];
res -= hsh[l - 1] * power[r - l + 1] % mod;
return res < 0 ? res + mod : 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<int, 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... |