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;
#define int long long
#define isz(a) (int)(a).size()
const int NM = 2500, inf = 1e18;
int N, A, B, C;
string S;
int z[NM+5], nxt[NM+5][NM+5], dp[NM+5][NM+5];
void build_z(){
z[0] = -1;
int l = 0, r = -1;
for (int i = 1; i < isz(S); i++){
if (i <= r) z[i] = min(r-i, z[i-l]);
else z[i] = -1;
while (i+z[i]+1 < isz(S) && S[i+z[i]+1] == S[z[i]+1]) z[i]++;
if (i+z[i] > r){
l = i;
r = i+z[i];
}
}
}
void solve(){
for (int i = 0; i < N; i++)
for (int j = 1; j <= N-i; j++)
nxt[i][j] = +inf;
for (int i = 0; i < N; i++){
build_z();
for (int j = 1; j < N-i; j++)
if (z[j] >= 0){
nxt[i][min(j, z[j]+1)] = min(nxt[i][min(j, z[j]+1)], i+j);
}
for (int j = N-i-1; j >= 1; j--)
nxt[i][j] = min(nxt[i][j], nxt[i][j+1]);
S.erase(0, 1);
}
for (int i = 0; i < N; i++)
for (int j = i; j < N; j++)
dp[i][j] = +inf;
for (int k = 1; k <= N; k++)
for (int i = 0; i+k <= N; i++){
int j = i+k-1;
if (k == 1) dp[i][j] = A;
else{
dp[i][j] = min(dp[i][j], dp[i+1][j]+A);
dp[i][j] = min(dp[i][j], dp[i][j-1]+A);
}
int t = nxt[i][k], cnt = 2;
while (t < N){
dp[i][t+k-1] = min(dp[i][t+k-1], dp[i][j]+A*(t+k-i-cnt*k)+B+C*cnt);
t = nxt[t][k];
cnt++;
}
}
cout << dp[0][N-1];
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N >> S >> A >> B >> C;
solve();
return 0;
}
# | 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... |