답안 #933759

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
933759 2024-02-26T09:32:01 Z boris_mihov Copy and Paste 3 (JOI22_copypaste3) C++17
30 / 100
2056 ms 414676 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 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 base[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;
}

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

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

    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))
                {
                    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:168:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  168 |     std::cin >> s + 1;
      |                 ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 152400 KB Output is correct
2 Correct 37 ms 152408 KB Output is correct
3 Correct 37 ms 152412 KB Output is correct
4 Correct 36 ms 152404 KB Output is correct
5 Correct 37 ms 152400 KB Output is correct
6 Correct 39 ms 152400 KB Output is correct
7 Correct 40 ms 152456 KB Output is correct
8 Correct 37 ms 152412 KB Output is correct
9 Correct 37 ms 152664 KB Output is correct
10 Correct 38 ms 152360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 152516 KB Output is correct
2 Correct 38 ms 152368 KB Output is correct
3 Correct 1010 ms 301356 KB Output is correct
4 Correct 1257 ms 325348 KB Output is correct
5 Correct 1428 ms 349516 KB Output is correct
6 Correct 1672 ms 380016 KB Output is correct
7 Correct 2056 ms 414676 KB Output is correct
8 Correct 2017 ms 414404 KB Output is correct
9 Correct 1992 ms 414016 KB Output is correct
10 Correct 36 ms 152284 KB Output is correct
11 Correct 38 ms 152736 KB Output is correct
12 Correct 40 ms 152408 KB Output is correct
13 Correct 40 ms 152412 KB Output is correct
14 Correct 37 ms 152404 KB Output is correct
15 Correct 40 ms 152408 KB Output is correct
16 Correct 37 ms 152412 KB Output is correct
17 Correct 38 ms 152436 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 37 ms 152412 KB Output is correct
4 Correct 36 ms 152404 KB Output is correct
5 Correct 37 ms 152400 KB Output is correct
6 Correct 39 ms 152400 KB Output is correct
7 Correct 40 ms 152456 KB Output is correct
8 Correct 37 ms 152412 KB Output is correct
9 Correct 37 ms 152664 KB Output is correct
10 Correct 38 ms 152360 KB Output is correct
11 Correct 36 ms 152404 KB Output is correct
12 Correct 37 ms 152664 KB Output is correct
13 Correct 36 ms 152408 KB Output is correct
14 Correct 37 ms 152412 KB Output is correct
15 Correct 37 ms 152412 KB Output is correct
16 Correct 37 ms 152404 KB Output is correct
17 Correct 38 ms 152352 KB Output is correct
18 Correct 37 ms 152404 KB Output is correct
19 Correct 37 ms 152404 KB Output is correct
20 Correct 38 ms 152400 KB Output is correct
21 Correct 40 ms 152392 KB Output is correct
22 Correct 37 ms 152404 KB Output is correct
23 Correct 37 ms 152400 KB Output is correct
24 Correct 38 ms 152404 KB Output is correct
25 Correct 37 ms 152404 KB Output is correct
26 Correct 37 ms 152376 KB Output is correct
27 Correct 37 ms 152400 KB Output is correct
28 Correct 37 ms 152408 KB Output is correct
29 Correct 37 ms 152604 KB Output is correct
30 Correct 37 ms 152412 KB Output is correct
31 Correct 37 ms 152400 KB Output is correct
32 Correct 37 ms 152396 KB Output is correct
33 Correct 37 ms 152404 KB Output is correct
34 Correct 37 ms 152408 KB Output is correct
35 Correct 38 ms 152452 KB Output is correct
36 Correct 37 ms 152232 KB Output is correct
37 Correct 37 ms 152412 KB Output is correct
38 Correct 37 ms 152400 KB Output is correct
39 Correct 37 ms 152656 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 37 ms 152412 KB Output is correct
4 Correct 36 ms 152404 KB Output is correct
5 Correct 37 ms 152400 KB Output is correct
6 Correct 39 ms 152400 KB Output is correct
7 Correct 40 ms 152456 KB Output is correct
8 Correct 37 ms 152412 KB Output is correct
9 Correct 37 ms 152664 KB Output is correct
10 Correct 38 ms 152360 KB Output is correct
11 Correct 36 ms 152404 KB Output is correct
12 Correct 37 ms 152664 KB Output is correct
13 Correct 36 ms 152408 KB Output is correct
14 Correct 37 ms 152412 KB Output is correct
15 Correct 37 ms 152412 KB Output is correct
16 Correct 37 ms 152404 KB Output is correct
17 Correct 38 ms 152352 KB Output is correct
18 Correct 37 ms 152404 KB Output is correct
19 Correct 37 ms 152404 KB Output is correct
20 Correct 38 ms 152400 KB Output is correct
21 Correct 40 ms 152392 KB Output is correct
22 Correct 37 ms 152404 KB Output is correct
23 Correct 37 ms 152400 KB Output is correct
24 Correct 38 ms 152404 KB Output is correct
25 Correct 37 ms 152404 KB Output is correct
26 Correct 37 ms 152376 KB Output is correct
27 Correct 37 ms 152400 KB Output is correct
28 Correct 37 ms 152408 KB Output is correct
29 Correct 37 ms 152604 KB Output is correct
30 Correct 37 ms 152412 KB Output is correct
31 Correct 37 ms 152400 KB Output is correct
32 Correct 37 ms 152396 KB Output is correct
33 Correct 37 ms 152404 KB Output is correct
34 Correct 37 ms 152408 KB Output is correct
35 Correct 38 ms 152452 KB Output is correct
36 Correct 37 ms 152232 KB Output is correct
37 Correct 37 ms 152412 KB Output is correct
38 Correct 37 ms 152400 KB Output is correct
39 Correct 37 ms 152656 KB Output is correct
40 Correct 38 ms 152924 KB Output is correct
41 Correct 45 ms 158772 KB Output is correct
42 Correct 43 ms 158604 KB Output is correct
43 Correct 41 ms 158548 KB Output is correct
44 Correct 42 ms 158504 KB Output is correct
45 Correct 41 ms 158548 KB Output is correct
46 Correct 41 ms 158556 KB Output is correct
47 Correct 42 ms 158552 KB Output is correct
48 Correct 42 ms 158556 KB Output is correct
49 Correct 41 ms 158544 KB Output is correct
50 Correct 41 ms 158552 KB Output is correct
51 Correct 43 ms 158552 KB Output is correct
52 Correct 44 ms 158548 KB Output is correct
53 Correct 42 ms 158600 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 37 ms 152412 KB Output is correct
4 Correct 36 ms 152404 KB Output is correct
5 Correct 37 ms 152400 KB Output is correct
6 Correct 39 ms 152400 KB Output is correct
7 Correct 40 ms 152456 KB Output is correct
8 Correct 37 ms 152412 KB Output is correct
9 Correct 37 ms 152664 KB Output is correct
10 Correct 38 ms 152360 KB Output is correct
11 Correct 36 ms 152404 KB Output is correct
12 Correct 37 ms 152664 KB Output is correct
13 Correct 36 ms 152408 KB Output is correct
14 Correct 37 ms 152412 KB Output is correct
15 Correct 37 ms 152412 KB Output is correct
16 Correct 37 ms 152404 KB Output is correct
17 Correct 38 ms 152352 KB Output is correct
18 Correct 37 ms 152404 KB Output is correct
19 Correct 37 ms 152404 KB Output is correct
20 Correct 38 ms 152400 KB Output is correct
21 Correct 40 ms 152392 KB Output is correct
22 Correct 37 ms 152404 KB Output is correct
23 Correct 37 ms 152400 KB Output is correct
24 Correct 38 ms 152404 KB Output is correct
25 Correct 37 ms 152404 KB Output is correct
26 Correct 37 ms 152376 KB Output is correct
27 Correct 37 ms 152400 KB Output is correct
28 Correct 37 ms 152408 KB Output is correct
29 Correct 37 ms 152604 KB Output is correct
30 Correct 37 ms 152412 KB Output is correct
31 Correct 37 ms 152400 KB Output is correct
32 Correct 37 ms 152396 KB Output is correct
33 Correct 37 ms 152404 KB Output is correct
34 Correct 37 ms 152408 KB Output is correct
35 Correct 38 ms 152452 KB Output is correct
36 Correct 37 ms 152232 KB Output is correct
37 Correct 37 ms 152412 KB Output is correct
38 Correct 37 ms 152400 KB Output is correct
39 Correct 37 ms 152656 KB Output is correct
40 Correct 38 ms 152924 KB Output is correct
41 Correct 45 ms 158772 KB Output is correct
42 Correct 43 ms 158604 KB Output is correct
43 Correct 41 ms 158548 KB Output is correct
44 Correct 42 ms 158504 KB Output is correct
45 Correct 41 ms 158548 KB Output is correct
46 Correct 41 ms 158556 KB Output is correct
47 Correct 42 ms 158552 KB Output is correct
48 Correct 42 ms 158556 KB Output is correct
49 Correct 41 ms 158544 KB Output is correct
50 Correct 41 ms 158552 KB Output is correct
51 Correct 43 ms 158552 KB Output is correct
52 Correct 44 ms 158548 KB Output is correct
53 Correct 42 ms 158600 KB Output is correct
54 Correct 60 ms 168272 KB Output is correct
55 Correct 294 ms 209492 KB Output is correct
56 Correct 147 ms 201304 KB Output is correct
57 Correct 142 ms 201256 KB Output is correct
58 Correct 141 ms 199504 KB Output is correct
59 Correct 124 ms 201300 KB Output is correct
60 Correct 123 ms 201252 KB Output is correct
61 Correct 152 ms 201268 KB Output is correct
62 Incorrect 228 ms 205648 KB Output isn't correct
63 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 152400 KB Output is correct
2 Correct 37 ms 152408 KB Output is correct
3 Correct 37 ms 152412 KB Output is correct
4 Correct 36 ms 152404 KB Output is correct
5 Correct 37 ms 152400 KB Output is correct
6 Correct 39 ms 152400 KB Output is correct
7 Correct 40 ms 152456 KB Output is correct
8 Correct 37 ms 152412 KB Output is correct
9 Correct 37 ms 152664 KB Output is correct
10 Correct 38 ms 152360 KB Output is correct
11 Correct 37 ms 152516 KB Output is correct
12 Correct 38 ms 152368 KB Output is correct
13 Correct 1010 ms 301356 KB Output is correct
14 Correct 1257 ms 325348 KB Output is correct
15 Correct 1428 ms 349516 KB Output is correct
16 Correct 1672 ms 380016 KB Output is correct
17 Correct 2056 ms 414676 KB Output is correct
18 Correct 2017 ms 414404 KB Output is correct
19 Correct 1992 ms 414016 KB Output is correct
20 Correct 36 ms 152284 KB Output is correct
21 Correct 38 ms 152736 KB Output is correct
22 Correct 40 ms 152408 KB Output is correct
23 Correct 40 ms 152412 KB Output is correct
24 Correct 37 ms 152404 KB Output is correct
25 Correct 40 ms 152408 KB Output is correct
26 Correct 37 ms 152412 KB Output is correct
27 Correct 38 ms 152436 KB Output is correct
28 Correct 36 ms 152404 KB Output is correct
29 Correct 37 ms 152664 KB Output is correct
30 Correct 36 ms 152408 KB Output is correct
31 Correct 37 ms 152412 KB Output is correct
32 Correct 37 ms 152412 KB Output is correct
33 Correct 37 ms 152404 KB Output is correct
34 Correct 38 ms 152352 KB Output is correct
35 Correct 37 ms 152404 KB Output is correct
36 Correct 37 ms 152404 KB Output is correct
37 Correct 38 ms 152400 KB Output is correct
38 Correct 40 ms 152392 KB Output is correct
39 Correct 37 ms 152404 KB Output is correct
40 Correct 37 ms 152400 KB Output is correct
41 Correct 38 ms 152404 KB Output is correct
42 Correct 37 ms 152404 KB Output is correct
43 Correct 37 ms 152376 KB Output is correct
44 Correct 37 ms 152400 KB Output is correct
45 Correct 37 ms 152408 KB Output is correct
46 Correct 37 ms 152604 KB Output is correct
47 Correct 37 ms 152412 KB Output is correct
48 Correct 37 ms 152400 KB Output is correct
49 Correct 37 ms 152396 KB Output is correct
50 Correct 37 ms 152404 KB Output is correct
51 Correct 37 ms 152408 KB Output is correct
52 Correct 38 ms 152452 KB Output is correct
53 Correct 37 ms 152232 KB Output is correct
54 Correct 37 ms 152412 KB Output is correct
55 Correct 37 ms 152400 KB Output is correct
56 Correct 37 ms 152656 KB Output is correct
57 Correct 38 ms 152924 KB Output is correct
58 Correct 45 ms 158772 KB Output is correct
59 Correct 43 ms 158604 KB Output is correct
60 Correct 41 ms 158548 KB Output is correct
61 Correct 42 ms 158504 KB Output is correct
62 Correct 41 ms 158548 KB Output is correct
63 Correct 41 ms 158556 KB Output is correct
64 Correct 42 ms 158552 KB Output is correct
65 Correct 42 ms 158556 KB Output is correct
66 Correct 41 ms 158544 KB Output is correct
67 Correct 41 ms 158552 KB Output is correct
68 Correct 43 ms 158552 KB Output is correct
69 Correct 44 ms 158548 KB Output is correct
70 Correct 42 ms 158600 KB Output is correct
71 Correct 60 ms 168272 KB Output is correct
72 Correct 294 ms 209492 KB Output is correct
73 Correct 147 ms 201304 KB Output is correct
74 Correct 142 ms 201256 KB Output is correct
75 Correct 141 ms 199504 KB Output is correct
76 Correct 124 ms 201300 KB Output is correct
77 Correct 123 ms 201252 KB Output is correct
78 Correct 152 ms 201268 KB Output is correct
79 Incorrect 228 ms 205648 KB Output isn't correct
80 Halted 0 ms 0 KB -