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 pb push_back
#define int long long
#define vi vector<int>
#define vvi vector<vector<int>>
#define pii pair<int, int>
#define vpii vector<pair<int, int>>
#define vc vector<char>
#define vb vector<bool>
#define mii map<int,int>
#define f0r(i,n) for(int i=0;i<n;i++)
#define FOR(i,k,n) for(int i=k;i<n;i++)
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define in(a) int a; cin>>a
#define in2(a,b) int a,b; cin>>a>>b
#define in3(a,b,c) int a,b,c; cin>>a>>b>>c
#define in4(a,b,c,d) int a,b,c,d; cin>>a>>b>>c>>d
#define vin(v,n); vi v(n); f0r(i,n){cin>>v[i];}
#define out(a) cout<<a<<'\n'
#define out2(a,b) cout<<a<<' '<<b<<'\n'
#define out3(a,b,c) cout<<a<<' '<<b<<' '<<c<<'\n'
#define out4(a,b,c,d) cout<<a<<' '<<b<<' '<<c<<' '<<d<<'\n'
#define vout(v) cout<<#v<<' '; for(auto u : v){cout<<u<<' ';} cout<<'\n'
#define dout(a) cout<<a<<' '<<#a<<'\n'
#define dout2(a,b) cout<<a<<' '<<#a<<' '<<b<<' '<<#b<<'\n'
#define yn(x); if(x){cout<<"YES"<<'\n';}else{cout<<"NO"<<'\n';}
const int leg = 1e9 + 7;
const int mod = 1e15 + 1911;
using namespace std;
const int A = 711;
int minu(int a, int b){
int ret = (a - b) % mod;
if(ret < 0)ret += mod;
return ret;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
//ifstream cin(".in");
//ofstream cout(".out");
in(n);
string s;
cin>>s;
vi h(n);
h[0] = s[0];
FOR(i, 1, n)h[i] = (h[i-1] * A + s[i]) % mod;
vi p(n);
p[0] = 1;
FOR(i, 1, n)p[i] = (p[i-1] * A) % mod;
//vout(h);
//vout(p);
in3(a,b,c);
vvi dp(n, vi(n));
f0r(i,n){
FOR(j, i, n){
dp[i][j] = (j - i + 1) * a;
}
}
vvi hash(n, vi(n));
f0r(i,n){
hash[i][i] = s[i];
FOR(j, i+1, n){
hash[i][j] = (hash[i][j-1] * A + s[j]) % mod;
}
}
f0r(i,n){
//vout(hash[i]);
}
vvi nxt(n+1, vi(n, -1));
FOR(i, 1, n+1){
mii prev;
f0r(j,n-i+1){
if(prev.count(hash[j][j + i - 1]) == 0){
prev[hash[j][j + i - 1]] = j;
}
else{
int p = prev[hash[j][j + i - 1]];
if(p + i - 1 < j){
nxt[i][p] = j;
prev[hash[j][j + i - 1]] = j;
}
}
}
}
FOR(i, 1, n+1){
//vout(nxt[i]);
}
vector<vector<vpii>>t(n, vector<vpii>(n));
FOR(sz, 1, n+1){
f0r(i, n - sz + 1){
if(sz != 1)dp[i][i + sz - 1] = min(dp[i][i+sz-1], min(dp[i][i+sz-2], dp[i+1][i+sz-1]) + a);
//else dp[i][i] = a;
int cur = i;
int count = 1;
while(cur != -1){
cur = nxt[sz][cur];
if(cur != -1){
//out2(i, cur + sz - 1);
//out4(i, i+sz-1, count, (count + 1) * c + (cur + sz - 1 - i + 1 - (count + 1) * sz) * a + b + dp[i][i + sz - 1]);
dp[i][cur + sz - 1] = min(dp[i][cur + sz - 1], (count + 1) * c + (cur + sz - 1 - i + 1 - (count + 1) * sz) * a + b + dp[i][i + sz - 1]);
}
count++;
}
}
}
f0r(i,n){
//vout(dp[i]);
}
out(dp[0][n-1]);
}
# | 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... |