# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
634659 | alvingogo | Copy and Paste 3 (JOI22_copypaste3) | C++14 | 0 ms | 0 KiB |
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>
#pragma GCC optimize("Ofast")
#define AquA cin.tie(0);ios_base::sync_with_stdio(0);
#define fs first
#define sc second
#define p_q priority_queue
#define int long long
using namespace std;
string s;
int n,a,b,c;
const int bs=53,mod=1e9+7;
vector<int> pre,fac;
vector<vector<int> > g;
map<int,int> dp;
int inv(int x){
return x==1?1:(inv(mod%x)*(mod-mod/x)%mod);
}
void init(){
pre.resize(n+5);
fac.resize(n+5);
fac[0]=1;
pre[0]=0;
for(int i=1;i<=n;i++){
fac[i]=fac[i-1]*bs%mod;
pre[i]=(pre[i-1]+fac[i]*(s[i-1]-'a'+s[i-1]*2))%mod;
}
}
int ha(int l,int r){
l++;
r++;
return (pre[r]-pre[l-1]+mod)*inv(fac[l])%mod;
}
signed main(){
AquA;
cin >> n >> s >> a >> b >> c;
init();
g.resize(n,vector<int>(n));
vector<int> p;
for(int i=0;i<n;i++){
for(int j=i;j<n;j++){
g[i][j]=ha(i,j);
p.push_back(g[i][j]);
}
}
sort(p.begin(),p.end());
p.erase(unique(p.begin(),p.end()),p.end());
for(int i=0;i<n;i++){
for(int j=i;j<n;j++){
g[i][j]=lower_bound(p.begin(),p.end(),g[i][j]);
}
}
vector<vector<int> > zzz(n,vector<int>(n,1e18));
for(int r=0;r<n;r++){
vector<pair<int,int> > nw(1000000,{0,4274343});
for(int l=r;l>=0;l--){
if(l==r){
zzz[l][r]=a;
if(nw.find(g[l][r])==nw.end()){
nw[g[l][r]]={1,l};
}
continue;
}
zzz[l][r]=min(zzz[l][r],min(zzz[l+1][r]+a,zzz[l][r-1]+a));
for(int j=l;j<=r;j++){
if(nw.find(g[l][j])==nw.end()){
nw[g[l][j]]={0,999999};
}
if(nw[g[l][j]].sc>j){
nw[g[l][j]].sc=l;
nw[g[l][j]].fs++;
zzz[l][r]=min(zzz[l][r],a*(r-l+1-(j-l+1)*nw[g[l][j]].fs)+b+c*nw[g[l][j]].fs+zzz[l][j]);
}
}
//cout << l << " " << r << " " << zzz[l][r] << "\n";
}
}
//cout << g[5][5] << " " << g[6][6] << "\n";
cout << zzz[0][n-1] << "\n";
return 0;
}