Submission #549212

#TimeUsernameProblemLanguageResultExecution timeMemory
549212Ronin13Copy and Paste 3 (JOI22_copypaste3)C++14
62 / 100
3086 ms242596 KiB
#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 = 3001 + 1; 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][3]; 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 < 3; j++){ h[i][j] = h[i - 1][j] * pr[j] + x; h[i][j] %= mod[j]; } } } ll 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]; int adj[NMAX][NMAX]; map<pair<int, pii>, int> mp; vector <pair<int, pii> > vec[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 < 3; 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(auto to : vec[i - 1]) mp[to] = i - 1; for(int j = i; j <= n; j++){ ll x = get(i, j, 0); ll x1 = get(i, j, 1); ll x2 = get(i, j, 2); int v = mp[{x, {x1, x2}}]; if(v != 0) adj[i][j - i + 1] = v - (j - i); vec[j].pb({x, {x1, x2}}); } } /*for(int i = 1; i <= n; i++){ for(int j = 1; j <= n; j++){ cout << adj[i][j] << ' '; } cout << "\n"; }*/ vector <ll> v[NMAX]; 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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...