답안 #936850

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
936850 2024-03-02T20:46:10 Z fyan Copy and Paste 3 (JOI22_copypaste3) C++14
30 / 100
1407 ms 98588 KB
//source: https://oj.uz/problem/view/JOI22_copypaste3
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
using namespace std;
#define int long long
#define P pair
#define pi P<int,int>
#define f first
#define s second
#define mp make_pair
#define all(x) begin(x), end(x)
#define sz(x) (int) (x).size()
#define V vector
#define vi V<int>
#define v2i V<vi>
#define v3i V<v2i>
#define vpi V<pi>
#define vb V<bool>
#define v2b V<vb>
#define pb push_back
#define S set
#define si S<int>
#define ins insert
#define era erase
#define M map
#define mii M<int,int>
#define Q queue
#define PQ priority_queue
const int MOD=1e9+7;
const int INF=1e18;
int smax(int& a, int b) {return a = max(a,b);}
int smin(int& a, int b) {return a = min(a,b);}

struct stringmatch {
	int n;
	int p = 29;
	int MOD = 1420696969;
	vector<int> pref, ppow;
	stringmatch() {}
	stringmatch(vector<int> arr) {
		n = arr.size();
		ppow.push_back(1);
		for (int i = 0; i < n+5; i++) {
			ppow.push_back((p * ppow[i])%MOD);
		}
		pref.push_back(0);
		for (int i = 0; i < n; i++) {
			pref.push_back((pref[i] + (arr[i]+1)*ppow[i])%MOD);
		}
	}
	bool equiv(int l1, int r1, int l2, int r2) {
		if (r1 - l1 != r2 - l2) return 0;
		if (l1 > l2) {
			swap(l1, l2);
			swap(r1, r2);
		}
		int sub1 = (pref[r1+1] - pref[l1] + MOD)%MOD;
		int sub2 = (pref[r2+1] - pref[l2] + MOD)%MOD;
		sub1 *= ppow[l2-l1]; sub1 %= MOD;
		return (sub1 == sub2);
	}
};

int n;
vi s;
int a, b, c;
v2i dp, nxt;
stringmatch sm;

signed main() {
	ios::sync_with_stdio(false); cin.tie(nullptr);
	cin >> n;
	for (int i = 0; i < n; i++) {
		char c; cin >> c; s.pb(c - 'a');
	}
	cin >> a >> b >> c;
	sm = stringmatch(s);
	nxt = v2i(n, vi(n, -1));
	for (int l = 0; l < n; l++) {
		for (int r = l; r < n; r++) {
			for (int sf = r-l+1; r + sf < n; sf++) {
				if (sm.equiv(l, r, l+sf, r+sf)) {
					nxt[l][r] = l + sf;
					break;
				}
			}
		}
	}
	dp = v2i(n, vi(n, INF));
	for (int i = 0; i < n; i++) {
		dp[i][i] = a;
	}
	for (int l = n-1; l >= 0; l--) {
		for (int r = l; r < n; r++) {
			//add letters
			if (r+1 < n) {
				smin(dp[l][r+1], dp[l][r]+a);
			}
			if (l > 0) {
				smin(dp[l-1][r], dp[l][r]+a);
			}
			//copy + paste
			int cost = dp[l][r] + b + c;
			int cl = l; int cr = r;
			while (nxt[cl][cr] != -1) {
				int nl = nxt[cl][cr];
				int nr = nl + r - l;
				cost += a * (nl - cr - 1);
				cost += c;
				smin(dp[l][nr], cost);
				cl = nl; cr = nr;
			}
		}
	}
	cout << dp[0][n-1];
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 460 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 174 ms 52820 KB Output is correct
4 Correct 241 ms 61780 KB Output is correct
5 Correct 282 ms 72020 KB Output is correct
6 Correct 362 ms 84404 KB Output is correct
7 Correct 425 ms 98588 KB Output is correct
8 Correct 424 ms 98532 KB Output is correct
9 Correct 420 ms 98588 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 460 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 600 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 600 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 600 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 452 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 460 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 600 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 600 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 600 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 452 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 1 ms 448 KB Output is correct
41 Correct 1 ms 860 KB Output is correct
42 Correct 11 ms 1044 KB Output is correct
43 Correct 11 ms 860 KB Output is correct
44 Correct 12 ms 856 KB Output is correct
45 Correct 12 ms 860 KB Output is correct
46 Correct 12 ms 1040 KB Output is correct
47 Correct 3 ms 1112 KB Output is correct
48 Correct 7 ms 1112 KB Output is correct
49 Correct 7 ms 860 KB Output is correct
50 Correct 6 ms 1040 KB Output is correct
51 Correct 3 ms 860 KB Output is correct
52 Correct 3 ms 860 KB Output is correct
53 Correct 12 ms 1040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 460 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 1 ms 600 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 600 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 600 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 0 ms 348 KB Output is correct
28 Correct 0 ms 452 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 1 ms 448 KB Output is correct
41 Correct 1 ms 860 KB Output is correct
42 Correct 11 ms 1044 KB Output is correct
43 Correct 11 ms 860 KB Output is correct
44 Correct 12 ms 856 KB Output is correct
45 Correct 12 ms 860 KB Output is correct
46 Correct 12 ms 1040 KB Output is correct
47 Correct 3 ms 1112 KB Output is correct
48 Correct 7 ms 1112 KB Output is correct
49 Correct 7 ms 860 KB Output is correct
50 Correct 6 ms 1040 KB Output is correct
51 Correct 3 ms 860 KB Output is correct
52 Correct 3 ms 860 KB Output is correct
53 Correct 12 ms 1040 KB Output is correct
54 Correct 118 ms 3584 KB Output is correct
55 Correct 36 ms 16220 KB Output is correct
56 Correct 1362 ms 16024 KB Output is correct
57 Correct 1385 ms 16020 KB Output is correct
58 Correct 1400 ms 16024 KB Output is correct
59 Incorrect 1407 ms 16020 KB Output isn't correct
60 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 460 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 174 ms 52820 KB Output is correct
14 Correct 241 ms 61780 KB Output is correct
15 Correct 282 ms 72020 KB Output is correct
16 Correct 362 ms 84404 KB Output is correct
17 Correct 425 ms 98588 KB Output is correct
18 Correct 424 ms 98532 KB Output is correct
19 Correct 420 ms 98588 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 0 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 0 ms 344 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 1 ms 600 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 1 ms 600 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 0 ms 600 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 0 ms 348 KB Output is correct
45 Correct 0 ms 452 KB Output is correct
46 Correct 0 ms 348 KB Output is correct
47 Correct 0 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 1 ms 348 KB Output is correct
50 Correct 1 ms 348 KB Output is correct
51 Correct 0 ms 348 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 0 ms 348 KB Output is correct
54 Correct 1 ms 348 KB Output is correct
55 Correct 0 ms 348 KB Output is correct
56 Correct 0 ms 348 KB Output is correct
57 Correct 1 ms 448 KB Output is correct
58 Correct 1 ms 860 KB Output is correct
59 Correct 11 ms 1044 KB Output is correct
60 Correct 11 ms 860 KB Output is correct
61 Correct 12 ms 856 KB Output is correct
62 Correct 12 ms 860 KB Output is correct
63 Correct 12 ms 1040 KB Output is correct
64 Correct 3 ms 1112 KB Output is correct
65 Correct 7 ms 1112 KB Output is correct
66 Correct 7 ms 860 KB Output is correct
67 Correct 6 ms 1040 KB Output is correct
68 Correct 3 ms 860 KB Output is correct
69 Correct 3 ms 860 KB Output is correct
70 Correct 12 ms 1040 KB Output is correct
71 Correct 118 ms 3584 KB Output is correct
72 Correct 36 ms 16220 KB Output is correct
73 Correct 1362 ms 16024 KB Output is correct
74 Correct 1385 ms 16020 KB Output is correct
75 Correct 1400 ms 16024 KB Output is correct
76 Incorrect 1407 ms 16020 KB Output isn't correct
77 Halted 0 ms 0 KB -