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
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n,a,b,c;
string s;
cin >> n >> s >> a >> b >> c;
s.insert(s.begin(),'$');
vector<vector<int>> DP(n+1,vector<int>(n+1));
for(int l=1;l<=n;l++)for(int r=l;r<=n;r++)DP[l][r]=a*(r-l+1ll);
// Do the hashing stuff here
map<pair<int,char>,int> hasher;
vector<vector<int>> occurences = {{}};
int alloc = 0;
for(int l=1;l<=n;l++){
int currhash = 0;
for(int r=l;r<=n;r++){
if(!hasher.count({currhash,s[r]})){
hasher[{currhash,s[r]}]=++alloc;
occurences.emplace_back();
}
currhash = hasher[{currhash,s[r]}];
occurences[currhash].emplace_back(r);
}
}
for(int l=n;l;l--){
int currhash = 0;
for(int r=l;r<=n;r++){
currhash = hasher[{currhash,s[r]}];
int len = r-l+1;
if(l!=r){
DP[l][r] = min(DP[l][r],DP[l+1][r]+a);
DP[l][r] = min(DP[l][r],DP[l][r-1]+a);
}
auto iter = lower_bound(occurences[currhash].begin(),occurences[currhash].end(),r+len);
int cnt = 1;
while(iter!=occurences[currhash].end()){
int x = *iter;
cnt++;
DP[l][x] = min(DP[l][x],DP[l][r]+b+a*(x-l+1ll - cnt*(len))+cnt*c);
iter = lower_bound(occurences[currhash].begin(),occurences[currhash].end(),x+len);
}
}
}
cout << DP[1][n] << '\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... |