Submission #719655

#TimeUsernameProblemLanguageResultExecution timeMemory
719655Abrar_Al_SamitCopy and Paste 3 (JOI22_copypaste3)C++17
25 / 100
3083 ms8276 KiB
#include<bits/stdc++.h> using namespace std; const int nax = 1002; const long long INF = 1e18; const int P = 31; const int M = 1e9 + 9; int n; string s; long long A, B, C; long long dp[nax][nax]; long long hv[nax]; long long pw[nax]; bool same(int l1, int r1, int l2, int r2) { long long h1 = hv[r1] - hv[l1-1]; long long h2 = hv[r2] - hv[l2-1]; if(h1<0) h1 += M; if(h2<0) h2 += M; if(r1<r2) h1 = (h1 * pw[r2-r1]) % M; else h2 = (h2 * pw[r1-r2]) % M; return h1==h2; } long long solve(int i, int j) { long long &ret = dp[i][j]; if(ret!=-1) return ret; if(i==j) return A; ret = min((j-i+1) * A, solve(i+1, j) + A); for(int k=1; k<j-i+1; ++k) { int cnt = 0; int p = i; while(p+k-1<=j) { if(same(i, i+k-1, p, p+k-1)) { ++cnt; p += k; } else ++p; } long long cur = solve(i, i+k-1) + B; cur += cnt * C; cur += ((j-i+1) - cnt * k) * A; ret = min(ret, cur); } return ret; } void PlayGround() { cin>>n>>s>>A>>B>>C; memset(dp, -1, sizeof dp); s = '@' + s; pw[0] = 1; for(int i=1; i<=n; ++i) { pw[i] = (pw[i-1] * P) % M; hv[i] = (pw[i] * (s[i]-'a'+1)) + hv[i-1]; hv[i] %= M; } cout<<solve(1, n)<<'\n'; // cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); PlayGround(); 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...