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 all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
#define pil pair<int,long long>
const int maxn=2509;
int n;
string s;
long long x,y,z;
long long f[maxn][maxn];
int lcp[maxn][maxn];
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("temp.INP","r",stdin);
//freopen("temp.OUT","w",stdout);
cin>>n>>s>>x>>y>>z;
s=" "+s;
for1(i,1,n){
for1(j,1,n){
lcp[i][j]=lcp[i-1][j-1]-1;
if (lcp[i-1][j-1]==0)lcp[i][j]=0;
while (lcp[i][j]<=n-max(j,i)+1&&s[i+lcp[i][j]]==s[j+lcp[i][j]]){
lcp[i][j]++;
}
}
}
for1(i,1,n){
for1(j,i,n){
f[i][j]=1ll*(j-i+1)*x;
}
}
for1(len,1,n){
for1(i,1,n-len+1){
int j=i+len-1;
if (i>1)f[i-1][j]=min(f[i-1][j],f[i][j]+x);
if (j+1<=n)f[i][j+1]=min(f[i][j+1],f[i][j]+x);
long long cost=y+z+f[i][j];
for1(p,j+1,n){
if (lcp[p][i]>=len){
cost+=z;
p+=len-1;
}
else {
cost+=x;
}
f[i][p]=min(f[i][p],cost);
}
}
}
cout<<f[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... |