Submission #945391

#TimeUsernameProblemLanguageResultExecution timeMemory
945391vjudge1Copy and Paste 3 (JOI22_copypaste3)C++17
100 / 100
1270 ms85332 KiB
#include <iostream> #include <complex> #include <vector> #include <string> #include <algorithm> #include <cstdio> #include <numeric> #include <cstring> #include <ctime> #include <cstdlib> #include <set> #include <map> #include <unordered_map> #include <unordered_set> #include <list> #include <cmath> #include <bitset> #include <cassert> #include <queue> #include <stack> #include <deque> #include <random> using namespace std; template<typename T1, typename T2> inline void chkmin(T1 &a, T2 b) {if (a > b) a = b;} template<typename T1, typename T2> inline void chkmax(T1 &a, T2 b) {if (a < b) a = b;} #define files(FILENAME) read(FILENAME); write(FILENAME) #define read(FILENAME) freopen((FILENAME + ".in").c_str(), "r", stdin) #define write(FILENAME) freopen((FILENAME + ".out").c_str(), "w", stdout) #define all(c) (c).begin(), (c).end() #define sz(c) (int)(c).size() #define left left228 #define right right228 #define y1 y1228 #define mp make_pair #define pb push_back #define y2 y2228 #define rank rank228 using ll = long long; using ld = long double; const string FILENAME = "input"; const int MAXN = 2500 + 7; const ll inf = 1e18; const ll Mod = (1LL << 61) - 1; ll n, a, b, c; string s; ll hsh[MAXN][MAXN]; ll dp[MAXN][MAXN]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); //read(FILENAME); cin >> n >> s >> a >> b >> c; s = "#" + s; for (int i = 1; i <= n; i++) { for (int j = i; j <= n; j++) { hsh[i][j] = (hsh[i][j - 1] * 26 + s[j] - 'a') % Mod; dp[i][j] = inf; } dp[i][i] = a; } for (int h = 1; h <= n; h++) { vector<pair<ll, int>> cand; for (int i = 1; i + h - 1 <= n; i++) { int j = i + h - 1; chkmin(dp[i][j], min(dp[i + 1][j], dp[i][j - 1]) + a); cand.pb(mp(hsh[i][j], i)); } sort(all(cand)); for (auto it: cand) { int x = 1; int i = it.second; while (true) { int pos = lower_bound(all(cand), mp(it.first, i + h)) - cand.begin(); if (pos < sz(cand) && cand[pos].first == it.first) { i = cand[pos].second; x++; chkmin(dp[it.second][i + h - 1], dp[it.second][it.second + h - 1] + b + c * x + a * (i + h - it.second - x * h)); } else { break; } } } } cout << dp[1][n] << '\n'; 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...