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;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define X first
#define Y second
#define SZ(a) ((int)a.size())
#define ALL(v) v.begin(), v.end()
#define pb push_back
const ll INF = 9e18;
const ll C = 1e9;
const int P = 29, MOD = 998244353;
const int P2 = 37, MOD2 = 998244341;
int prv[2505][2505], hsh[2505][2505], hsh2[2505][2505];
unordered_map<ll, int> mp[2505];
ll res[2505][2505], dp[2505][2505];
int cal(int l, int i, int r) {
int rt = 1, len = r - i;
while (prv[i][r] >= l)
++rt, i = prv[i][r], r = i + len;
return rt;
}
ll comb(int l, int r) {
return hsh[l][r] * C + hsh2[l][r];
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int n, a, b, c;
string s;
cin >> n >> s >> a >> b >> c;
for (int r = 0; r < n; ++r) {
for (int i = r; i >= 0; --i) {
hsh[i][r] = ((ll)hsh[i + 1][r] * P + s[i] - 'a') % MOD;
hsh2[i][r] = ((ll)hsh2[i + 1][r] * P2 + s[i] - 'a') % MOD2;
prv[i][r] = -1;
if (2 * i >= r + 1) {
mp[r - i][comb(2 * i - r - 1, i - 1)] = 2 * i - r - 1;
if (mp[r - i].find(comb(i, r)) != mp[r - i].end()) {
prv[i][r] = mp[r - i][comb(i, r)];
}
}
}
}
for (int r = 0; r < n; ++r) {
for (int l = r; l >= 0; --l)
dp[l][r] = INF;
for (int l = r; l >= 0; --l) {
dp[l][r] = min(dp[l][r], (ll)(r - l + 1) * a + b + c);
if (l < r)
dp[l][r] = min(dp[l][r], dp[l + 1][r] + a);
res[l][r] = min(res[l][r - 1] + a, dp[l][r]);
ll t = 1;
for (int x = l, y = r; prv[x][y] >= 0; ) {
++t;
x = prv[x][y], y = x + r - l;
dp[x][r] = min(dp[x][r], res[l][r] + b + t * c + (ll)(r - x + 1 - t * (r - l + 1)) * a);
}
/*cerr << "dp " << l << " " << r << " = " << dp[l][r] << "\n";
cerr << "res " << l << " " << r << " = " << res[l][r] << "\n";*/
}
}
cout << res[0][n - 1] << "\n";
}
# | 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... |