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
#define all(x) x.begin(),x.end()
#define ff first
#define ss second
#define pb push_back
using namespace std;
const int N=2505,mod=1e9+11;
int h[N][N],dp[N][N];
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int n,a,b,c;
cin>>n;
string s;
cin>>s;
cin>>a>>b>>c;
for(int i=0;i<n;i++){
h[i][i]=s[i]-'a';
//cout<<h[i][i]<<" ";
for(int j=i+1;j<n;j++){
h[i][j]=h[i][j-1]*26+(s[j]-'a');
h[i][j]%=mod;
dp[i][j]=1e18;
//cout<<h[i][j]<<" ";
}
dp[i][i]=a;
//cout<<"\n";
}
for(int sz=1;sz<=n;sz++){
vector <pair <int,int> > v;
for(int l=0;l<=n-sz;l++){
int r=l+sz-1;
dp[l][r]=min(dp[l][r],min(dp[l+1][r],dp[l][r-1])+a);
v.pb({h[l][r],l});
}
sort(all(v));
for(auto x : v){
int pos=x.ss;
int hash=x.ff;
int cnt=1;
while(true){
pair <int,int> p={hash,pos+sz};
auto it=lower_bound(all(v),p);
if(it!=v.end() && it->ff==hash){
pos=it->ss;
cnt++;
dp[x.ss][pos+sz-1]=min(dp[x.ss][pos+sz-1],dp[x.ss][x.ss+sz-1]+b+c*cnt+a*(pos+sz-x.ss-sz*cnt));
}
else break;
}
}
}
cout<<dp[0][n-1]<<"\n";
}
/*
2 5 4 9 15 11
6 7 6 8 12 14
1 0 0 0
1 0 0 0
0 0 0 0
0 1 0 0
0 1 1 0
0 0 0 0
0 1 0 0
0 1 1 0
0 1 0 0
0 0 1 0
0 0 1 1
0 0 0 1
*/
# | 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... |