Submission #1085094

#TimeUsernameProblemLanguageResultExecution timeMemory
1085094hahahahaCopy and Paste 3 (JOI22_copypaste3)C++17
100 / 100
255 ms67668 KiB
#include <bits/stdc++.h> using namespace std; #define int long long #define isz(a) (int)(a).size() const int NM = 2500, inf = 1e18; int N, A, B, C; string S; int z[NM+5], nxt[NM+5][NM+5], dp[NM+5][NM+5]; void build_z(){ z[0] = -1; int l = 0, r = -1; for (int i = 1; i < isz(S); i++){ if (i <= r) z[i] = min(r-i, z[i-l]); else z[i] = -1; while (i+z[i]+1 < isz(S) && S[i+z[i]+1] == S[z[i]+1]) z[i]++; if (i+z[i] > r){ l = i; r = i+z[i]; } } } void solve(){ for (int i = 0; i < N; i++) for (int j = 1; j <= N-i; j++) nxt[i][j] = +inf; for (int i = 0; i < N; i++){ build_z(); for (int j = 1; j < N-i; j++) if (z[j] >= 0){ nxt[i][min(j, z[j]+1)] = min(nxt[i][min(j, z[j]+1)], i+j); } for (int j = N-i-1; j >= 1; j--) nxt[i][j] = min(nxt[i][j], nxt[i][j+1]); S.erase(0, 1); } for (int i = 0; i < N; i++) for (int j = i; j < N; j++) dp[i][j] = +inf; for (int k = 1; k <= N; k++) for (int i = 0; i+k <= N; i++){ int j = i+k-1; if (k == 1) dp[i][j] = A; else{ dp[i][j] = min(dp[i][j], dp[i+1][j]+A); dp[i][j] = min(dp[i][j], dp[i][j-1]+A); } int t = nxt[i][k], cnt = 2; while (t < N){ dp[i][t+k-1] = min(dp[i][t+k-1], dp[i][j]+A*(t+k-i-cnt*k)+B+C*cnt); t = nxt[t][k]; cnt++; } } cout << dp[0][N-1]; } signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> N >> S >> A >> B >> C; 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...