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;
const ll INF=1e17;
ll baza=29;
const ll mod=1e9+7;
ll h[2505],A,B,C;
ll dp[2505][2505];
ll v[2505];
map<ll,ll> nrm;
vector<int> who[2505];
ll n;
string s;
ll nxt[2505];
ll pw[2505];
ll gethash(ll l,ll r)
{
ll rez=(h[r]-(h[l-1]*pw[r-l+1])%mod+mod)%mod;
return rez;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
pw[0]=1;
for(int i=1;i<=2500;i++)
pw[i]=(pw[i-1]*baza)%mod;
cin>>n>>s>>A>>B>>C;
s=" "+s;
for(int i=1;i<=n;i++)
h[i]=(h[i-1]*baza+s[i]-'a'+1)%mod;
for(int i=1;i<=n;i++)
for(int j=i;j<=n;j++)
dp[i][j]=INF;
for(int i=1;i<=n;i++)
dp[i][i]=A;
for(int lg=1;lg<n;lg++)
{
nrm.clear();
vector<ll> vals;
for(ll i=1;i+lg-1<=n;i++)
{
v[i]=gethash(i,i+lg-1);
vals.push_back(v[i]);
}
sort(vals.begin(),vals.end());
vals.erase(unique(vals.begin(),vals.end()),vals.end());
for(int i=0;i<vals.size();i++)
{
nrm[vals[i]]=i+1;
who[i+1].clear();
}
for(int i=1;i+lg-1<=n;i++)
{
v[i]=nrm[v[i]];
who[v[i]].push_back(i);
}
for(int i=1;i+lg-1<=n;i++)
{
nxt[i]=n+1;
ll st=0;
ll dr=who[v[i]].size();
dr--;
while(st<=dr)
{
int mij=(st+dr)/2;
if(who[v[i]][mij]>i+lg-1)
{
nxt[i]=who[v[i]][mij];
dr=mij-1;
}
else
st=mij+1;
}
}
for(int i=1;i+lg-1<=n;i++)
{
ll l=i;
ll r=i+lg-1;
if(l>1)
dp[l-1][r]=min(dp[l-1][r],dp[l][r]+A);
if(r<n)
dp[l][r+1]=min(dp[l][r+1],dp[l][r]+A);
ll val=dp[l][r];
ll j=i;
ll cnt=1;
while(nxt[j]<=n)
{
cnt++;
j=nxt[j];
r=j+lg-1;
ll cost=val+B+C*cnt+A*(r-l+1-cnt*lg);
dp[l][r]=min(dp[l][r],cost);
}
}
}
cout<<dp[1][n];
return 0;
}
Compilation message (stderr)
copypaste3.cpp: In function 'int main()':
copypaste3.cpp:49:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | for(int i=0;i<vals.size();i++)
| ~^~~~~~~~~~~~
# | 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... |