답안 #933761

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
933761 2024-02-26T09:34:01 Z boris_mihov Copy and Paste 3 (JOI22_copypaste3) C++17
30 / 100
2148 ms 427728 KB
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>

typedef long long llong;
const int MAXN = 2500 + 10;
const int ALPHABET = 26;
const int MOD = 998244353;
const int MOD2 = 1e9 + 7;
const int INF  = 1e9;

int n;
char s[MAXN];
int letter[MAXN][ALPHABET];
int fail[MAXN];

llong a, b, c;
llong dp[MAXN][MAXN];
llong cnt[MAXN][MAXN];
llong best[MAXN][MAXN];
std::vector <int> increase[MAXN][MAXN];
llong prefixHash[MAXN];
llong prefixHash2[MAXN];
llong base[MAXN];
llong base2[MAXN];

llong getHash(int l, int r)
{
    llong res = prefixHash[r] + MOD - ((prefixHash[l - 1] * base[r - l + 1]) % MOD);
    if (res >= MOD) res -= MOD;
    return res;
}

llong getHash2(int l, int r)
{
    llong res = prefixHash2[r] + MOD2 - ((prefixHash2[l - 1] * base2[r - l + 1]) % MOD2);
    if (res >= MOD2) res -= MOD2;
    return res;
}

void solve()
{
    base[0] = 1;
    base2[0] = 1;
    for (int i = 1 ; i <= n ; ++i)
    {
        base[i] = (base[i - 1] * 27) % MOD;
        base2[i] = (base2[i - 1] * 27) % MOD2;
    }

    for (int i = 1 ; i <= n ; ++i)
    {
        prefixHash[i] = prefixHash[i - 1] * 27;
        prefixHash[i] += s[i] - 'a' + 1;
        prefixHash[i] %= MOD;
    }

    for (int i = 1 ; i <= n ; ++i)
    {
        prefixHash2[i] = prefixHash2[i - 1] * 27;
        prefixHash2[i] += s[i] - 'a' + 1;
        prefixHash2[i] %= MOD2;
    }

    for (int l = 1 ; l <= n ; ++l)
    {
        std::vector <int> v;
        for (int r = l ; r <= n ; ++r)
        {
            if (s[l] == s[r])
            {
                // std::cout << "add: " << l << ' ' << r << '\n';
                v.push_back(r);
                increase[l][r].push_back(l);
            }
        }

        fail[1] = 0;
        for (int c = 0 ; c < ALPHABET ; ++c)
        {
            letter[1][c] = 0;
        }

        std::string curr; curr += s[l];
        for (int r = l + 1 ; r <= n ; ++r)
        {
            curr += s[r];
            fail[r - l + 1] = fail[r - l];
            while (fail[r - l + 1] > 0 && s[l + fail[r - l + 1]] != s[r])
            {
                fail[r - l + 1] = fail[fail[r - l + 1] - 1]; 
            }

            if (s[l + fail[r - l + 1] + 1] == s[r])
            {
                fail[r - l + 1]++;
            }

            for (int c = 0 ; c < ALPHABET ; ++c)
            {
                if (s[l + letter[r - l][c]] == char('a' + c))
                {
                    letter[r - l + 1][c] = letter[r - l][c] + 1;
                } else
                {
                    if (letter[r - l][c] == 0)
                    {
                        letter[r - l + 1][c] = 0;
                    } else
                    {
                        letter[r - l + 1][c] = letter[letter[r - l][c] - 1][c];
                    }
                }
            }
            
            int last = l - 1;
            std::vector <int> newV;
            for (const int &left : v)
            {
                int len = r - l;
                int idx = left + r - l;
                int newLeft = left;

                while (len > 0 && (newLeft <= last || s[l + idx - newLeft] != s[idx]))
                {
                    len = letter[len][s[idx] - 'a'];
                    newLeft = idx - len;
                }

                // std::cout << "try: " << left << ' ' << newLeft << ' ' << idx << ' ' << last << " = " << getHash(newLeft, newLeft + r - l) << ' ' << getHash(l, r) << '\n';
                if (newLeft > last && newLeft + r - l <= n && getHash(newLeft, newLeft + r - l) == getHash(l, r) && getHash2(newLeft, newLeft + r - l) == getHash2(l, r))
                {
                    newV.push_back(newLeft);
                    last = newLeft + r - l;
                }

            }

            // std::cout << "subarray: " << curr << '\n';
            // std::cout << "at\n";

            // for (const int &left : newV)
            // {
            //     std::cout << left << ' ' << left + r - l << '\n';
            // }

            v = newV;
            for (const int &left : v)
            {
                increase[l][left + r - l].push_back(r);
            }
        }
    }

    for (int len = 1 ; len <= n ; ++len)
    {
        for (int l = 1 ; l + len - 1 <= n ; ++l)
        {
            int r = l + len - 1;
            best[l][r] = best[l][r - 1];
            for (const int &idx : increase[l][r])
            {
                cnt[l][idx]++;
                if (cnt[l][idx] > 1)
                {
                    best[l][r] = std::max(best[l][r], cnt[l][idx] * (idx - l + 1) * a - b - c * cnt[l][idx] - dp[l][idx]);
                }
            }

            dp[l][r] = a * (r - l + 1) - best[l][r];
            if (len > 1)
            {
                dp[l][r] = std::min(dp[l][r], dp[l + 1][r] + a);
                dp[l][r] = std::min(dp[l][r], dp[l][r - 1] + a);
            }
        }
    }

    std::cout << dp[1][n] << '\n';
}

void input()
{
    std::cin >> n;
    std::cin >> s + 1;
    std::cin >> a >> b >> c;
}

void fastIOI()
{
    std::ios_base :: sync_with_stdio(0);
    std::cout.tie(nullptr);
    std::cin.tie(nullptr);
}

int main()
{
    fastIOI();
    input();
    solve();

    return 0;
}

Compilation message

copypaste3.cpp: In function 'void input()':
copypaste3.cpp:187:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  187 |     std::cin >> s + 1;
      |                 ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 152404 KB Output is correct
2 Correct 37 ms 152412 KB Output is correct
3 Correct 36 ms 152412 KB Output is correct
4 Correct 37 ms 152204 KB Output is correct
5 Correct 38 ms 152404 KB Output is correct
6 Correct 37 ms 152400 KB Output is correct
7 Correct 36 ms 152412 KB Output is correct
8 Correct 36 ms 152412 KB Output is correct
9 Correct 36 ms 152400 KB Output is correct
10 Correct 37 ms 152192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 152400 KB Output is correct
2 Correct 37 ms 152408 KB Output is correct
3 Correct 1062 ms 301360 KB Output is correct
4 Correct 1252 ms 324492 KB Output is correct
5 Correct 1492 ms 350416 KB Output is correct
6 Correct 1787 ms 379648 KB Output is correct
7 Correct 2148 ms 413884 KB Output is correct
8 Correct 2137 ms 414528 KB Output is correct
9 Correct 2141 ms 427728 KB Output is correct
10 Correct 37 ms 152284 KB Output is correct
11 Correct 37 ms 152316 KB Output is correct
12 Correct 38 ms 152428 KB Output is correct
13 Correct 36 ms 152412 KB Output is correct
14 Correct 37 ms 152412 KB Output is correct
15 Correct 37 ms 152408 KB Output is correct
16 Correct 37 ms 152560 KB Output is correct
17 Correct 37 ms 152408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 152404 KB Output is correct
2 Correct 37 ms 152412 KB Output is correct
3 Correct 36 ms 152412 KB Output is correct
4 Correct 37 ms 152204 KB Output is correct
5 Correct 38 ms 152404 KB Output is correct
6 Correct 37 ms 152400 KB Output is correct
7 Correct 36 ms 152412 KB Output is correct
8 Correct 36 ms 152412 KB Output is correct
9 Correct 36 ms 152400 KB Output is correct
10 Correct 37 ms 152192 KB Output is correct
11 Correct 37 ms 152288 KB Output is correct
12 Correct 37 ms 152412 KB Output is correct
13 Correct 37 ms 152404 KB Output is correct
14 Correct 37 ms 152408 KB Output is correct
15 Correct 39 ms 152320 KB Output is correct
16 Correct 36 ms 152400 KB Output is correct
17 Correct 36 ms 152436 KB Output is correct
18 Correct 36 ms 152412 KB Output is correct
19 Correct 37 ms 152468 KB Output is correct
20 Correct 38 ms 152660 KB Output is correct
21 Correct 36 ms 152404 KB Output is correct
22 Correct 37 ms 152404 KB Output is correct
23 Correct 37 ms 152504 KB Output is correct
24 Correct 38 ms 152412 KB Output is correct
25 Correct 36 ms 152404 KB Output is correct
26 Correct 36 ms 152400 KB Output is correct
27 Correct 36 ms 152456 KB Output is correct
28 Correct 37 ms 152408 KB Output is correct
29 Correct 37 ms 152352 KB Output is correct
30 Correct 36 ms 152400 KB Output is correct
31 Correct 37 ms 152400 KB Output is correct
32 Correct 37 ms 152404 KB Output is correct
33 Correct 37 ms 152356 KB Output is correct
34 Correct 37 ms 152224 KB Output is correct
35 Correct 35 ms 152400 KB Output is correct
36 Correct 36 ms 152412 KB Output is correct
37 Correct 36 ms 152408 KB Output is correct
38 Correct 36 ms 152280 KB Output is correct
39 Correct 36 ms 152408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 152404 KB Output is correct
2 Correct 37 ms 152412 KB Output is correct
3 Correct 36 ms 152412 KB Output is correct
4 Correct 37 ms 152204 KB Output is correct
5 Correct 38 ms 152404 KB Output is correct
6 Correct 37 ms 152400 KB Output is correct
7 Correct 36 ms 152412 KB Output is correct
8 Correct 36 ms 152412 KB Output is correct
9 Correct 36 ms 152400 KB Output is correct
10 Correct 37 ms 152192 KB Output is correct
11 Correct 37 ms 152288 KB Output is correct
12 Correct 37 ms 152412 KB Output is correct
13 Correct 37 ms 152404 KB Output is correct
14 Correct 37 ms 152408 KB Output is correct
15 Correct 39 ms 152320 KB Output is correct
16 Correct 36 ms 152400 KB Output is correct
17 Correct 36 ms 152436 KB Output is correct
18 Correct 36 ms 152412 KB Output is correct
19 Correct 37 ms 152468 KB Output is correct
20 Correct 38 ms 152660 KB Output is correct
21 Correct 36 ms 152404 KB Output is correct
22 Correct 37 ms 152404 KB Output is correct
23 Correct 37 ms 152504 KB Output is correct
24 Correct 38 ms 152412 KB Output is correct
25 Correct 36 ms 152404 KB Output is correct
26 Correct 36 ms 152400 KB Output is correct
27 Correct 36 ms 152456 KB Output is correct
28 Correct 37 ms 152408 KB Output is correct
29 Correct 37 ms 152352 KB Output is correct
30 Correct 36 ms 152400 KB Output is correct
31 Correct 37 ms 152400 KB Output is correct
32 Correct 37 ms 152404 KB Output is correct
33 Correct 37 ms 152356 KB Output is correct
34 Correct 37 ms 152224 KB Output is correct
35 Correct 35 ms 152400 KB Output is correct
36 Correct 36 ms 152412 KB Output is correct
37 Correct 36 ms 152408 KB Output is correct
38 Correct 36 ms 152280 KB Output is correct
39 Correct 36 ms 152408 KB Output is correct
40 Correct 39 ms 152668 KB Output is correct
41 Correct 44 ms 158552 KB Output is correct
42 Correct 42 ms 158556 KB Output is correct
43 Correct 40 ms 158548 KB Output is correct
44 Correct 46 ms 158456 KB Output is correct
45 Correct 43 ms 158800 KB Output is correct
46 Correct 40 ms 158548 KB Output is correct
47 Correct 42 ms 158800 KB Output is correct
48 Correct 43 ms 158544 KB Output is correct
49 Correct 41 ms 158556 KB Output is correct
50 Correct 41 ms 158580 KB Output is correct
51 Correct 42 ms 158552 KB Output is correct
52 Correct 42 ms 158480 KB Output is correct
53 Correct 40 ms 158544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 152404 KB Output is correct
2 Correct 37 ms 152412 KB Output is correct
3 Correct 36 ms 152412 KB Output is correct
4 Correct 37 ms 152204 KB Output is correct
5 Correct 38 ms 152404 KB Output is correct
6 Correct 37 ms 152400 KB Output is correct
7 Correct 36 ms 152412 KB Output is correct
8 Correct 36 ms 152412 KB Output is correct
9 Correct 36 ms 152400 KB Output is correct
10 Correct 37 ms 152192 KB Output is correct
11 Correct 37 ms 152288 KB Output is correct
12 Correct 37 ms 152412 KB Output is correct
13 Correct 37 ms 152404 KB Output is correct
14 Correct 37 ms 152408 KB Output is correct
15 Correct 39 ms 152320 KB Output is correct
16 Correct 36 ms 152400 KB Output is correct
17 Correct 36 ms 152436 KB Output is correct
18 Correct 36 ms 152412 KB Output is correct
19 Correct 37 ms 152468 KB Output is correct
20 Correct 38 ms 152660 KB Output is correct
21 Correct 36 ms 152404 KB Output is correct
22 Correct 37 ms 152404 KB Output is correct
23 Correct 37 ms 152504 KB Output is correct
24 Correct 38 ms 152412 KB Output is correct
25 Correct 36 ms 152404 KB Output is correct
26 Correct 36 ms 152400 KB Output is correct
27 Correct 36 ms 152456 KB Output is correct
28 Correct 37 ms 152408 KB Output is correct
29 Correct 37 ms 152352 KB Output is correct
30 Correct 36 ms 152400 KB Output is correct
31 Correct 37 ms 152400 KB Output is correct
32 Correct 37 ms 152404 KB Output is correct
33 Correct 37 ms 152356 KB Output is correct
34 Correct 37 ms 152224 KB Output is correct
35 Correct 35 ms 152400 KB Output is correct
36 Correct 36 ms 152412 KB Output is correct
37 Correct 36 ms 152408 KB Output is correct
38 Correct 36 ms 152280 KB Output is correct
39 Correct 36 ms 152408 KB Output is correct
40 Correct 39 ms 152668 KB Output is correct
41 Correct 44 ms 158552 KB Output is correct
42 Correct 42 ms 158556 KB Output is correct
43 Correct 40 ms 158548 KB Output is correct
44 Correct 46 ms 158456 KB Output is correct
45 Correct 43 ms 158800 KB Output is correct
46 Correct 40 ms 158548 KB Output is correct
47 Correct 42 ms 158800 KB Output is correct
48 Correct 43 ms 158544 KB Output is correct
49 Correct 41 ms 158556 KB Output is correct
50 Correct 41 ms 158580 KB Output is correct
51 Correct 42 ms 158552 KB Output is correct
52 Correct 42 ms 158480 KB Output is correct
53 Correct 40 ms 158544 KB Output is correct
54 Correct 65 ms 168272 KB Output is correct
55 Correct 308 ms 209492 KB Output is correct
56 Correct 157 ms 201832 KB Output is correct
57 Correct 145 ms 201296 KB Output is correct
58 Correct 125 ms 201300 KB Output is correct
59 Correct 122 ms 201372 KB Output is correct
60 Correct 120 ms 201552 KB Output is correct
61 Correct 156 ms 201340 KB Output is correct
62 Incorrect 236 ms 205652 KB Output isn't correct
63 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 41 ms 152404 KB Output is correct
2 Correct 37 ms 152412 KB Output is correct
3 Correct 36 ms 152412 KB Output is correct
4 Correct 37 ms 152204 KB Output is correct
5 Correct 38 ms 152404 KB Output is correct
6 Correct 37 ms 152400 KB Output is correct
7 Correct 36 ms 152412 KB Output is correct
8 Correct 36 ms 152412 KB Output is correct
9 Correct 36 ms 152400 KB Output is correct
10 Correct 37 ms 152192 KB Output is correct
11 Correct 36 ms 152400 KB Output is correct
12 Correct 37 ms 152408 KB Output is correct
13 Correct 1062 ms 301360 KB Output is correct
14 Correct 1252 ms 324492 KB Output is correct
15 Correct 1492 ms 350416 KB Output is correct
16 Correct 1787 ms 379648 KB Output is correct
17 Correct 2148 ms 413884 KB Output is correct
18 Correct 2137 ms 414528 KB Output is correct
19 Correct 2141 ms 427728 KB Output is correct
20 Correct 37 ms 152284 KB Output is correct
21 Correct 37 ms 152316 KB Output is correct
22 Correct 38 ms 152428 KB Output is correct
23 Correct 36 ms 152412 KB Output is correct
24 Correct 37 ms 152412 KB Output is correct
25 Correct 37 ms 152408 KB Output is correct
26 Correct 37 ms 152560 KB Output is correct
27 Correct 37 ms 152408 KB Output is correct
28 Correct 37 ms 152288 KB Output is correct
29 Correct 37 ms 152412 KB Output is correct
30 Correct 37 ms 152404 KB Output is correct
31 Correct 37 ms 152408 KB Output is correct
32 Correct 39 ms 152320 KB Output is correct
33 Correct 36 ms 152400 KB Output is correct
34 Correct 36 ms 152436 KB Output is correct
35 Correct 36 ms 152412 KB Output is correct
36 Correct 37 ms 152468 KB Output is correct
37 Correct 38 ms 152660 KB Output is correct
38 Correct 36 ms 152404 KB Output is correct
39 Correct 37 ms 152404 KB Output is correct
40 Correct 37 ms 152504 KB Output is correct
41 Correct 38 ms 152412 KB Output is correct
42 Correct 36 ms 152404 KB Output is correct
43 Correct 36 ms 152400 KB Output is correct
44 Correct 36 ms 152456 KB Output is correct
45 Correct 37 ms 152408 KB Output is correct
46 Correct 37 ms 152352 KB Output is correct
47 Correct 36 ms 152400 KB Output is correct
48 Correct 37 ms 152400 KB Output is correct
49 Correct 37 ms 152404 KB Output is correct
50 Correct 37 ms 152356 KB Output is correct
51 Correct 37 ms 152224 KB Output is correct
52 Correct 35 ms 152400 KB Output is correct
53 Correct 36 ms 152412 KB Output is correct
54 Correct 36 ms 152408 KB Output is correct
55 Correct 36 ms 152280 KB Output is correct
56 Correct 36 ms 152408 KB Output is correct
57 Correct 39 ms 152668 KB Output is correct
58 Correct 44 ms 158552 KB Output is correct
59 Correct 42 ms 158556 KB Output is correct
60 Correct 40 ms 158548 KB Output is correct
61 Correct 46 ms 158456 KB Output is correct
62 Correct 43 ms 158800 KB Output is correct
63 Correct 40 ms 158548 KB Output is correct
64 Correct 42 ms 158800 KB Output is correct
65 Correct 43 ms 158544 KB Output is correct
66 Correct 41 ms 158556 KB Output is correct
67 Correct 41 ms 158580 KB Output is correct
68 Correct 42 ms 158552 KB Output is correct
69 Correct 42 ms 158480 KB Output is correct
70 Correct 40 ms 158544 KB Output is correct
71 Correct 65 ms 168272 KB Output is correct
72 Correct 308 ms 209492 KB Output is correct
73 Correct 157 ms 201832 KB Output is correct
74 Correct 145 ms 201296 KB Output is correct
75 Correct 125 ms 201300 KB Output is correct
76 Correct 122 ms 201372 KB Output is correct
77 Correct 120 ms 201552 KB Output is correct
78 Correct 156 ms 201340 KB Output is correct
79 Incorrect 236 ms 205652 KB Output isn't correct
80 Halted 0 ms 0 KB -