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>
#define int long long
using namespace std;
const int INF = 1LL << 60;
int dp[2501][2501];
int chmin(int& x, int y){
return x = min(x, y);
}
const int a1 = 32913, m1 = 1e9 + 7, a2 = 5289, m2 = 998244353;
int pw1[2501], h1[2501], pw2[2501], h2[2501];
int ha(int l, int len){
return ((((h1[l + len] - h1[l] * pw1[len]) % m1 + m1) % m1) << 32) + (((h2[l + len] - h2[l] * pw2[len]) % m2 + m2) % m2);
}
int H[2501][2501];
int32_t main(){
int n; string s; cin >> n >> s;
int A, B, C; cin >> A >> B >> C;
pw1[0] = pw2[0] = 1;
for(int i = 0; i < n; i++)
pw1[i + 1] = pw1[i] * a1 % m1, h1[i + 1] = (h1[i] * a1 + s[i]) % m1,
pw2[i + 1] = pw2[i] * a2 % m2, h2[i + 1] = (h2[i] * a2 + s[i]) % m2;
for(int l = 0; l < n; l++){
for(int r = l; r < n; r++){
H[l][r] = ha(l, r - l + 1);
}
}
for(int sz = 2; sz <= n; sz++){
for(int l = 0; l <= n - sz; l++){
dp[l][l + sz - 1] = INF;
}
}
for(int sz = 1; sz <= n; sz++){
if(sz != 1){
for(int l = 0; l <= n - sz; l++){
int r = l + sz - 1;
dp[l][r] = min(dp[l][r], min(dp[l][r - 1], dp[l + 1][r]) + A);
}
}else{
for(int l = 0; l < n; l++) dp[l][l] = A;
}
unordered_map<int, vector<int>> M;
for(int l = 0; l <= n - sz; l++){
M[ha(l, sz)].push_back(l);
}
for(auto& [h, v] : M){
int k = v.size();
for(int l = 0; l < k; l++){
int _acc = 0, _max = -1;
int r = l;
while(r < k){
_acc++, _max = v[r] + sz - 1;
dp[v[l]][_max] = min(dp[v[l]][_max], dp[v[l]][v[l] + sz - 1] + (_max - v[l] + 1 - _acc * sz) * A + B + _acc * C);
r = lower_bound(v.begin(), v.end(), _max + 1) - v.begin();
}
}
}
}
cout << dp[0][n - 1] << endl;
}
Compilation message (stderr)
copypaste3.cpp: In function 'int32_t main()':
copypaste3.cpp:55:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
55 | for(auto& [h, v] : M){
| ^
# | 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... |