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 ll long long
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define all(x) x.begin(),x.end()
#define vf first
#define vs second
const int mod = 998244353;
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
const int P = 71;
int convert(char c){
return (c-'a') + 1;
}
void solve(){
int n;
cin >> n;
string s;
cin >> s;
ll a , b , c;
cin >> a >> b >> c;
ll dp[n][n];
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
dp[i][j] = 1e18;
}
}
map<pair<int,int> , vector<int>> m;
ll hash_val[n][n];
for(int i = 0; i < n; i++){
ll pow = 1;
ll hash = 0;
for(int j = i; j < n; j++){
hash += convert(s[j]) * pow;
pow *= P;
hash %= mod;
pow %= mod;
hash_val[i][j] = hash;
m[mp(hash,(j-i+1))].pb(i);
}
}
int next[n][n];
memset(next,-1,sizeof next);
for(int i = 0; i < n; i++){
for(int j = i; j < n; j++){
int pos = upper_bound(all(m[mp(hash_val[i][j],(j-i+1))]),j) - m[mp(hash_val[i][j],(j-i+1))].begin();
if(pos < m[mp(hash_val[i][j],(j-i+1))].size()){
next[i][j] = m[mp(hash_val[i][j],(j-i+1))][pos];
}
}
}
for(int len = 0; len < n; len++){
for(int i = 0; i + len < n; i++){
int j = i + len;
dp[i][j] = min(dp[i][j],(len+1LL)*a);
if(len > 0){
dp[i][j] = min(dp[i][j] , min(dp[i+1][j] , dp[i][j-1]) + a);
}
ll val = dp[i][j] + b;
val += c;
int x = i , y = j;
while(next[x][y] != -1){
int pos = next[x][y];
val += c;
val += (pos - (y + 1LL))*a;
dp[i][pos + len] = min(dp[i][pos+len],val);
x = pos;
y = pos + len;
}
}
}
cout << dp[0][n-1] << '\n';
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//int t;cin>>t;
//while(t--)
solve();
return 0;
}
Compilation message (stderr)
copypaste3.cpp: In function 'void solve()':
copypaste3.cpp:56:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | if(pos < m[mp(hash_val[i][j],(j-i+1))].size()){
| ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |