Submission #922104

#TimeUsernameProblemLanguageResultExecution timeMemory
922104vjudge1Copy and Paste 3 (JOI22_copypaste3)C++17
62 / 100
3049 ms174936 KiB
// I stand with PALESTINE //#pragma GCC optimize("Ofast,O3") //#pragma GCC target("avx,avx2") #include "bits/stdc++.h" using namespace std; using ll = long long; const ll inf = 1e18; const int mod1 = 1007173513, coeff1 = 995269721; const int mod2 = 1000458911, coeff2 = 997000859; void solve() { int n; ll A, B, C; string s; cin >> n >> s >> A >> B >> C; vector dp(n, vector(n, inf)); vector hash(n, vector(n, 0ll)); map<ll, vector<int>> pos; for (int i = 0; i < n; i++) { ll val1 = 0, pw1 = 1, val2 = 0, pw2 = 1; for (int j = i; j < n; j++) { val1 = (val1 + (s[j] - 'a' + 1) * pw1) % mod1; val2 = (val2 + (s[j] - 'a' + 1) * pw2) % mod2; pw1 = pw1 * coeff1 % mod1; pw2 = pw2 * coeff2 % mod2; hash[i][j] = val1 + val2 * mod1; pos[hash[i][j]].emplace_back(i); } } // set<ll> st; // for (int ln = 1; ln <= n; ln++) { // for (int i = 0, j = ln - 1; j < n; i++, j++) assert(!st.count(hash[i][j])); // for (int i = 0, j = ln - 1; j < n; i++, j++) st.insert(hash[i][j]); // } for (int i = n - 1; i >= 0; i--) { for (int j = i; j < n; j++) { dp[i][j] = min(dp[i][j], (j - i + 1) * A); if (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); } auto &it = pos[hash[i][j]]; int h = upper_bound(it.begin(), it.end(), j) - it.begin(), cnt = 1; int ln = j - i + 1; // cout << i << ' ' << j << ' ' << dp[i][j] << endl; while (h != it.size()) { h = it[h] + ln - 1; cnt++; dp[i][h] = min(dp[i][h], dp[i][j] + B + cnt * C + (h - i + 1 - ln * cnt) * A); // cout << '\t' << i << ' ' << h << ' ' << dp[i][h] << endl; h = upper_bound(it.begin(), it.end(), h) - it.begin(); } } } cout << dp[0][n - 1]; } int main() { cin.tie(0)->sync_with_stdio(false); #ifdef sunnatov freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int q = 1; // cin >> q; while (q--) { solve(); cout << '\n'; } }

Compilation message (stderr)

copypaste3.cpp: In function 'void solve()':
copypaste3.cpp:51:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |             while (h != it.size()) {
      |                    ~~^~~~~~~~~~~~
#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...