This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// I stand with PALESTINE
// The_Samurai
//#pragma GCC optimize("Ofast,O3")
//#pragma GCC target("avx,avx2")
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
const ll inf = 1e18;
const int mod1 = 1007173513, coeff1 = 995269721;
const int mod2 = 1000458911, coeff2 = 997000859;
void solve() {
int n;
ll A, B, C;
string s;
cin >> n >> s >> A >> B >> C;
vector dp(n, vector(n, inf));
vector hash(n, vector(n, 0ll));
map<ll, vector<int>> pos;
for (int i = 0; i < n; i++) {
ll val1 = 0, pw1 = 1, val2 = 0, pw2 = 1;
for (int j = i; j < n; j++) {
val1 = (val1 + (s[j] - 'a' + 1) * pw1) % mod1;
val2 = (val2 + (s[j] - 'a' + 1) * pw2) % mod2;
pw1 = pw1 * coeff1 % mod1;
pw2 = pw2 * coeff2 % mod2;
hash[i][j] = val1 + val2 * mod1;
}
}
// set<ll> st;
// for (int ln = 1; ln <= n; ln++) {
// for (int i = 0, j = ln - 1; j < n; i++, j++) assert(!st.count(hash[i][j]));
// for (int i = 0, j = ln - 1; j < n; i++, j++) st.insert(hash[i][j]);
// }
for (int ln = 1; ln <= n; ln++) {
for (int i = 0, j = ln - 1; j < n; i++, j++) pos[hash[i][j]].emplace_back(i);
for (int i = 0, j = ln - 1; j < n; i++, j++) {
dp[i][j] = min(dp[i][j], ln * A);
if (ln > 1) {
dp[i][j] = min(dp[i][j], dp[i + 1][j] + A);
dp[i][j] = min(dp[i][j], dp[i][j - 1] + A);
}
auto &it = pos[hash[i][j]];
int h = upper_bound(it.begin(), it.end(), j) - it.begin(), cnt = 1;
while (h != it.size()) {
h = it[h] + ln - 1;
cnt++;
dp[i][h] = min(dp[i][h], dp[i][j] + B + cnt * C + (h - i + 1 - ln * cnt) * A);
h = upper_bound(it.begin(), it.end(), h) - it.begin();
}
}
pos.clear();
}
cout << dp[0][n - 1];
}
int main() {
cin.tie(0)->sync_with_stdio(false);
#ifdef sunnatov
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int q = 1;
// cin >> q;
while (q--) {
solve();
cout << '\n';
}
}
Compilation message (stderr)
copypaste3.cpp: In function 'void solve()':
copypaste3.cpp:51:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | while (h != it.size()) {
| ~~^~~~~~~~~~~~
# | 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... |