Submission #1317683

#TimeUsernameProblemLanguageResultExecution timeMemory
1317683gelastropodCopy and Paste 3 (JOI22_copypaste3)C++20
6 / 100
462 ms182740 KiB
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define int long long

signed main() {
	int N, A, B, C, cnt = 1; cin >> N;
	string S; cin >> S >> A >> B >> C;
	vector<int> s = { 26 };
	for (char i : S) s.push_back(i - 'a');
	vector<vector<int>> vals(1, vector<int>(27, -1)), idxs(1, vector<int>()), children(1, vector<int>()), f(N, vector<int>(N, 0)), p(N, vector<int>(N, -1)), dp(N, vector<int>(N, INT_MAX));
	vector<vector<pair<int, int>>> toupd(N, vector<pair<int, int>>());
	for (int i = 0; i < N; i++) {
		int crnt = 0;
		for (int j = i; j >= -1; j--) {
			if (vals[crnt][s[j + 1]] == -1) {
				vals[crnt][s[j + 1]] = cnt++;
				vals.push_back(vector<int>(27, -1));
				idxs.push_back(vector<int>());
				children.push_back(vector<int>());
				children[crnt].push_back(s[j + 1]);
			}
			for (int k : children[crnt])
				if (k != s[j + 1])
					for (int l : idxs[vals[crnt][k]])
						f[i][l] = f[l][i] = i - j;
			crnt = vals[crnt][s[j + 1]];
			idxs[crnt].push_back(i);
		}
	}
	for (int i = 1; i < N; i++) {
		int lb = 0;
		for (int j = i - 1; j >= 0; j--)
			while (lb < f[i][j])
				p[i][lb++] = j;
	}
	for (int i = 0; i < N; i++) {
		dp[i][i] = A;
		if (p[i][0] != -1) toupd[p[i][0]].push_back({ 0, 2 });
		for (int j = 1; j <= i; j++) toupd[i - j].push_back({ j, 1 });
		int crnt = INT_MAX;
		for (int j = i - 1; j >= 0; j--) {
			dp[i][j] = dp[i - 1][j] + A;
			for (auto k : toupd[j]) {
				crnt = min(crnt, (C - (k.first + 1) * A) * k.second + dp[i][i - k.first]);
				if (p[j][k.first] != -1) toupd[p[j][k.first] - k.first].push_back({ k.first, k.second + 1 });
			}
			toupd[j].clear();
			dp[i][j] = min(dp[i][j], (i - j + 1) * A + B + crnt);
		}
	}
	cout << dp[N - 1][0] << '\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...