Submission #922712

#TimeUsernameProblemLanguageResultExecution timeMemory
922712huutuanCopy and Paste 3 (JOI22_copypaste3)C++14
100 / 100
658 ms50392 KiB
#pragma GCC optimize("Ofast,unroll-loops") #pragma GCC target("avx2") #include<bits/stdc++.h> using namespace std; #define int long long #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define isz(x) ((int)x.size()) #define sumof(x) accumulate(all(x), 0ll) pair<int, int> mod={(int)1e9+7, (int)1e9+9}; const int base=311; struct Hash{ pair<int, int> value; Hash (int x=0){ value={x%mod.first, x%mod.second}; } Hash (int x, int y){ value={x, y}; } Hash operator-(){ return Hash((mod.first-value.first)%mod.first, (mod.second-value.second)%mod.second); } Hash operator+(Hash b){ return Hash((value.first+b.value.first)%mod.first, (value.second+b.value.second)%mod.second); } Hash operator*(Hash b){ return Hash(value.first*b.value.first%mod.first, value.second*b.value.second%mod.second); } bool operator<(Hash b) const { return value<b.value; } bool operator==(Hash b) const { return value==b.value; } }; const int N=2510; int f[N][N], add, cut, paste, n; Hash pf[N]; Hash pw[N]; string s; Hash get(int l, int r){ return pf[r]+(-pf[l-1])*pw[r-l+1]; } void solve(){ cin >> n >> s; s=" "+s; cin >> add >> cut >> paste; pw[0]=1; for (int i=1; i<=n; ++i){ pw[i]=pw[i-1]*base; } for (int i=1; i<=n; ++i){ pf[i]=pf[i-1]*base+s[i]; } memset(f, 0x3f, sizeof f); for (int len=1; len<=n; ++len){ map<pair<int, int>, vector<int>> mp; for (int r=len; r<=n; ++r){ int l=r-len+1; mp[get(l, r).value].push_back(l); f[l][r]=min({f[l][r], f[l][r-1]+add, f[l+1][r]+add}); if (l==r) f[l][r]=add; } vector<int> jump(n+1, -1); for (auto &tmp:mp){ vector<int> &v=tmp.second; for (int i=0, j=0; i<isz(v); ++i){ while (j<isz(v) && v[j]-v[i]<len) ++j; jump[v[i]]=j==isz(v)?-1:v[j]; } } for (int i=1; i<=n; ++i){ int j=jump[i], cnt=2; while (j!=-1){ f[i][j+len-1]=min(f[i][j+len-1], f[i][i+len-1]+cut+cnt*paste+add*(j+len-i-cnt*len)); j=jump[j]; ++cnt; } } } cout << f[1][n]; } int32_t main(){ #ifdef sus freopen("cf.inp", "r", stdin); #endif ios_base::sync_with_stdio(false); cin.tie(nullptr); int ntests=1; // cin >> ntests; for (int i=1; i<=ntests; ++i) solve(); 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...