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 int N = 2500 + 2, MOD = (int)1e9+7;
#define int ll
ll binpow(ll a,ll b) {
ll ret = 1,k = a;
while(b) {
if(b & 1) {
ret *= k;
ret %= MOD;
}
k *= k;
k%=MOD;
b /= 2;
}
return ret;
}
string s;
ll a,b,c,h[N],p = 167,inv[N];
map<int,ll> mem;
int get(int l,int r) {
ll val = h[r];
if(l) {
val -= h[l - 1];
}
if(val < 0) val += MOD;
val = val * inv[l]%MOD;
return val;
}
ll solve(int l,int r) {
if(r == l) {
return a;
}
int t = get(l,r);
// if(mem.count(t)) {
// return mem[t];
// }
vector<array<int,3>> x;
int mx = (r - l + 2) / 2;
for(int i = l;i <= r;i++) {
for(int j = i;j <= min(r,i+mx-1);j++) {
// cout << l << ' ' << r << ' ' << i << ' ' << j << '\n';
x.push_back({get(i,j),i,j});
}
}
sort(x.begin(),x.end());
ll res = (r - l + 1) * 1ll * a;
int n = (r - l + 1);
for(int i = 0;i < (int)x.size();) {
int j = i;
while(j < (int)x.size() && x[i][0] == x[j][0]) {
j++;
}
int mn = -1,col = 0;
for(int k = i;k < j;k++) {
if(x[k][1] > mn) {
mn = x[k][2];
col++;
}
}
int sz = (x[i][2] - x[i][1] + 1);
if(col == 1) {
i = j;
continue;
}
ll val = solve(x[i][1],x[i][2]) + (n - sz * 1ll * col) * 1ll * a + (col - 1) * c + b + c;
res = min(res,val);
i = j;
}
return mem[t] = res;
}
int n;
void test() {
cin >> n >> s >> a >> b >> c;
ll x = 1;
for(int i = 0;i < n;i++) {
h[i] = x * 1ll * (s[i] - 'a' + 1)%MOD;
if(i) h[i] += h[i - 1];
if(h[i] >= MOD) h[i] -= MOD;
inv[i] = binpow(x,MOD-2);
x = x * 1ll * p%MOD;
}
cout << solve(0,n-1);
}
signed main() {
ios_base::sync_with_stdio(false);cin.tie(0);
int t = 1;
// cin >> t;
while(t--) {
test();
}
}
# | 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... |