Submission #761057

#TimeUsernameProblemLanguageResultExecution timeMemory
761057ymmCopy and Paste 3 (JOI22_copypaste3)C++17
57 / 100
3109 ms491060 KiB
#include <bits/stdc++.h> #define Loop(x,l,r) for (ll x = (l); x < (ll)(r); ++x) #define LoopR(x,l,r) for (ll x = (r)-1; x >= (ll)(l); --x) typedef long long ll; typedef std::pair<int, int> pii; typedef std::pair<ll , ll > pll; using namespace std; const int N = 2550; int pre[N][N]; vector<pii> wright[N][N]; ll dp[N][N]; ll A, B, C; char s[N]; int n; // TODO: use kmp, this is probably O(n^3) (but with glibc's blazingly fast mem* ans str*) void make_pre() { memset(pre, -1, sizeof(pre)); Loop (r2,1,n+1) { int len = 1; LoopR (r1,len,r2) { if (r2-r1 < len) continue; if (memcmp(&s[r2-len], &s[r1-len], len) == 0) { pre[r2-len][r2] = r1-len; ++len; ++r1; } } } } void make_wright() { Loop (l,0,n) Loop (r,l+1,n+1) { int k = 1; int p = l; do { wright[p][r].push_back({r-l, k}); p = pre[p][p+r-l]; ++k; } while (p != -1); } } void calc_dp() { Loop (r,1,n+1) LoopR (l,0,r) { dp[l][r] = min(dp[l][r-1], dp[l+1][r]) + A; for (auto [k, cnt] : wright[l][r]) dp[l][r] = min(dp[l][r], dp[l][l+k] + B + C * cnt + A * (r-l - cnt*k)); } } int main() { cin.tie(0) -> sync_with_stdio(false); cin >> n >> s >> A >> B >> C; make_pre(); make_wright(); calc_dp(); cout << dp[0][n] << '\n'; }
#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...