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>
#define ll long long
#define ull unsigned ll
#define f first
#define s second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define epb emplace_back
using namespace std;
const int NMAX = 2501 + 1;
const int inf = 1e9 + 1;
const ll linf = 1e18 + 1;
const ll mod = 1e9 + 7;
const ll pr = 29;
ll po[NMAX];
ll h[NMAX];
void hashing(string &s){
h[0] = 0;
int n = s.size();
for(int i = 1; i <= n; i++){
ll x = s[i - 1] - 'a' + 1;
h[i] = h[i - 1] * pr + x;
h[i] %= mod;
}
}
ll get(int l, int r){
ll x = h[r] - h[l - 1] * po[r - l + 1];
x %= mod;
x += mod;
x %= mod;
return x;
}
ll dp[NMAX][NMAX];
int adj[NMAX][NMAX];
int main(){
po[0] = 1;
for(int i = 1; i < NMAX; i++){
po[i] = po[i - 1] * pr;
po[i] %= mod;
}
for(int i = 1; i < NMAX; i++){
for(int j = 1; j < NMAX; j++){
dp[i][j] = linf;
}
}
ios_base::sync_with_stdio(false); cin.tie(0);
int n; cin >> n;
string s; cin >> s;
ll a, b, c;
cin >> a >> b >> c;
hashing(s);
map <ll, int> mp;
vector < vector <ll> > vec(n + 1);
for(int i = 1; i <= n; i++){
for(ll to : vec[i - 1]) mp[to] = i - 1;
for(int j = i; j <= n; j++){
ll x = get(i, j);
int v = mp[x];
if(v != 0)
adj[i][j - i + 1] = v - (j - i);
vec[j].pb(x);
}
}
/*for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
cout << adj[i][j] << ' ';
}
cout << "\n";
}*/
vector < vector <ll> > v(n + 1);
vector < ll > cnt(n + 1);
for(ll r = 1; r <= n; r++){
for(int i = 1; i <= n; i++)v[i].clear();
for(int i = 0; i <= n; i++)cnt[i] = 0;
for(int i = 1; i <= r; i++){
int cur = r - i + 1;
while(cur){
v[cur].pb(i);
cur = adj[cur][i];
}
}
ll mn = 0;
dp[r][r] = a;
for(ll l = r; l >= 1; l--){
for(ll to : v[l]){
cnt[to]++;
ll val = dp[r - to + 1][r] + b;
val += (c - to * a) * cnt[to];
mn = min(mn, val);
}
dp[l][r] = mn + (r - l + 1) * a;
dp[l][r] = min(dp[l][r], dp[l][r - 1] + a);
}
}
/*for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
if(dp[i][j] == linf) cout << "-1 ";
else cout << dp[i][j] << " ";
}
cout << "\n";
}*/
cout << dp[1][n];
return 0;
}
# | 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... |