# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
568036 | dantoh000 | Copy and Paste 3 (JOI22_copypaste3) | C++14 | 879 ms | 127916 KiB |
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 int long long
using namespace std;
typedef pair<int,int> ii;
const int INF = 1000000000000000000;
int n;
string s;
int a,b,c;
int dp[4505][4505];
int hsh[4505][4505];
int hsh2[4505][4505];
int nxt[4505][4505];
int mod = 1000000007, mod2 = 1000000009;
int H = 29, H2 = 31;
map<ii, vector<int> > groups;
main(){
scanf("%lld",&n);
cin >> s;
scanf("%lld%lld%lld",&a,&b,&c);
for (int i = 0; i < n; i++){
hsh[i][i] = hsh2[i][i] = 0;
for (int j = i+1; j <= n; j++){
hsh[i][j] = (hsh[i][j-1]*H + (s[j-1]-'a'+1))%mod;
hsh2[i][j] = (hsh2[i][j-1]*H2 + (s[j-1]-'a'+1))%mod2;
}
}
for (int i = 1; i <= n/2; i++){
groups.clear();
for (int j = 0; j+i <= n; j++){
groups[{hsh[j][j+i], hsh2[j][j+1]}].push_back(j);
}
for (auto it : groups){
auto V = it.second;
int cur = 0;
for (int k = 0; k < V.size(); k++){
int j = V[k];
while (cur < V.size() && V[cur] < j+i) cur++;
///printf("%d, length %d --> %d\n",j,i,cur);
if (cur == V.size()) nxt[i][j] = -1;
else nxt[i][j] = V[cur];
}
}
}
for (int i = 0; i <= n; i++){
dp[i][i] = 0;
for (int j = i+1; j <= n; j++){
dp[i][j] = INF;
}
}
for (int i = 1; i <= n; i++){
for (int j = 0; j+i <= n; j++){
dp[j][j+i] = min(dp[j][j+i], min(dp[j+1][j+i], dp[j][j+i-1]) + a);
}
if (i <= n/2){
for (int j = 0; j+i <= n; j++){
int cur = j;
int idx = 0;
int ct = 0;
int dpValue = dp[j][j+i];
while (true){
ct++;
int L = j;
int R = cur + i;
dp[L][R] = min(dp[L][R], dpValue + b + ct*c + (R-L - i*ct)*a);
cur = nxt[i][cur];
if (cur == -1) break;
}
}
}
}
printf("%lld\n",dp[0][n]);
}
Compilation message (stderr)
# | 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... |