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;
const int inf = 1e9 + 1;
const ll linf = 1e18 + 1;
ll mod[] ={1000000007, 1000000009, 998244353};
ll pr[] = {29, 31, 37};
ll po[NMAX][3];
ll h[NMAX][2];
void hashing(string s){
h[0][0] = 0;
h[0][1] = 0;
h[0][2] = 0;
int n = s.size();
for(int i = 1; i <= n; i++){
ll x = s[i - 1] - 'a' + 1;
for(int j = 0; j < 2; j++){
h[i][j] = h[i - 1][j] * pr[j] + x;
h[i][j] %= mod[j];
}
}
}
int get(int l, int r, int ind){
ll x = h[r][ind] - h[l - 1][ind] * po[r - l + 1][ind];
x %= mod[ind];
x += mod[ind];
x %= mod[ind];
return x;
}
ll dp[NMAX][NMAX];
short adj[NMAX][NMAX];
map<pii, int> mp;
vector <pii> vec[NMAX];
vector <int> v[NMAX];
int main(){
po[0][0] = po[0][1] = po[0][2] = 1;
ios_base::sync_with_stdio(false); cin.tie(0);
int n; cin >> n;
for(int k = 0; k < 2; k++){
for(int i = 1; i <=n; i++){
po[i][k] = po[i - 1][k] * pr[k];
po[i][k] %= mod[k];
}
}
for(int i = 0; i <= n; i++){
for(int j = 0; j <= n; j++){
dp[i][j] = linf;
}
}
string s; cin >> s;
ll a, b, c;
cin >> a >> b >> c;
hashing(s);
for(int i = 1; i <= n; i++){
for(pii to : vec[i - 1]) mp[to] = i - 1;
for(int x,x1,v,j = i; j <= n; j++){
x = get(i, j, 0);
x1 = get(i, j, 1);
v = mp[{x, x1}];
if(v != 0)
adj[i][j - i + 1] = v - (j - i);
vec[j].pb({x, x1});
}
}
/*for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
cout << adj[i][j] << ' ';
}
cout << "\n";
}*/
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 cur,i = 1; i <= r; i++){
cur = r - i + 1;
while(cur){
v[cur].pb(i);
cur = adj[cur][i];
}
}
ll mn = 0;
dp[r][r] = a;
for(int l = r; l >= 1; l--){
for(int to, ind = 0; ind < v[l].size(); ind++){
to = v[l][ind];
cnt[to]++;
ll val = dp[r - to + 1][r] + b;
val += (c - (ll)to * a) * cnt[to];
mn = min(mn, val);
}
dp[l][r] = mn + (ll)(r - l + 1) * a;
dp[l][r] = min(dp[l][r], dp[l][r - 1] + a);
}
}
cout << dp[1][n];
return 0;
}
Compilation message (stderr)
copypaste3.cpp: In function 'int main()':
copypaste3.cpp:100:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
100 | for(int to, ind = 0; ind < v[l].size(); ind++){
| ~~~~^~~~~~~~~~~~~
copypaste3.cpp: In function 'void hashing(std::string)':
copypaste3.cpp:24:11: warning: array subscript 2 is above array bounds of 'long long int [2]' [-Warray-bounds]
24 | h[0][2] = 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... |