Submission #1061827

#TimeUsernameProblemLanguageResultExecution timeMemory
1061827sleepntsheepCopy and Paste 3 (JOI22_copypaste3)C++17
100 / 100
1469 ms76484 KiB
#pragma GCC optimize("O3,unroll-loops") #include <deque> #include <string> #include <iostream> #include <map> #include <cstring> #include <vector> #include <algorithm> using namespace std; using ll = long long; template <typename T> using ve = vector<T>; template <typename T> using bs = basic_string<T>; const int N = 2505; const ll B = 137, M = 1000000007, B2 = 141, M2 = 1000000009; int n, nxt[N][N]; ll dp[N][N], a, b, c; char s[N]; ll hs[N], bp[N], ibp[N], bp2[N], ibp2[N], hs2[N], x; ll bpow(ll a, ll b, ll m) { if (!b) return 1; ll r { bpow(a, b / 2, m) }; if (b & 1) return r * r % m * a % m; return r * r % m; } void do_hash() { bp[0] = ibp[0] = bp2[0] = ibp2[0] = 1; for (int i = 1; i <= n; ++i) bp[i] = bp[i - 1] * B % M, ibp[i] = bpow(bp[i], M - 2, M); for (int i = 1; i <= n; ++i) hs[i] = bp[i] * (s[i] - 'a' + 1) % M, hs[i] = (hs[i] + hs[i - 1]) % M; for (int i = 1; i <= n; ++i) bp2[i] = bp2[i - 1] * B2 % M2, ibp2[i] = bpow(bp2[i], M2 - 2, M2); for (int i = 1; i <= n; ++i) hs2[i] = bp2[i] * (s[i] - 'a' + 1) % M2, hs2[i] = (hs2[i] + hs2[i - 1]) % M2; } inline ll get_hash(int l, int r) { return (ibp[l] * (hs[r] - hs[l - 1] + M) % M) * (ibp2[l] * (hs2[r] - hs2[l - 1] + M2) % M2); } ve<bs<int>> oo; void init() { do_hash(); memset(dp, 63, sizeof dp); for (int i = 1; i <= n; ++i) dp[i][i] = a; for (int l = 1; l <= n; ++l) { map<ll, deque<int>> w; for (int i = n - l + 1, j = n; i >= 1; --i, --j) { x = get_hash(i, j); auto it = lower_bound(w[x].begin(), w[x].end(), i + l); if (it == w[x].end()) nxt[i][j] = -1; else nxt[i][j] = *it; w[x].push_front(i); } } } int main() { scanf("%d%s%lld%lld%lld", &n, s + 1, &a, &b, &c); init(); for (int l = 1; l <= n; ++l) { for (int i = 1, j = l; j <= n; ++i, ++j) { dp[i][j] = min(dp[i][j], dp[i + 1][j] + a); dp[i][j] = min(dp[i][j], dp[i][j - 1] + a); for (int at = i, cnt = 1; ~at; ++cnt, at = nxt[at][at + l - 1]) { int jj = at + l - 1; dp[i][jj] = min(dp[i][jj], dp[i][j] + b + cnt * c + (jj - i + 1 - cnt * l) * a); } } } printf("%lld", dp[1][n]); }

Compilation message (stderr)

copypaste3.cpp: In function 'int main()':
copypaste3.cpp:69:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |     scanf("%d%s%lld%lld%lld", &n, s + 1, &a, &b, &c);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...