Submission #776841

# Submission time Handle Problem Language Result Execution time Memory
776841 2023-07-08T10:16:38 Z danikoynov Copy and Paste 3 (JOI22_copypaste3) C++14
100 / 100
2519 ms 517672 KB
/**
 ____ ____ ____ ____ ____ ____
||l |||e |||i |||n |||a |||d ||
||__|||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|/__\|

**/

#include<bits/stdc++.h>
#define endl '\n'

using namespace std;
typedef long long ll;

void speed()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}


const int maxn = 2510;
const ll base = 29, inf = 1e18;

int n;
ll a, b, c, h[maxn], pw[maxn];
string s;
vector < int >  occ[maxn * maxn];
ll hs_dp[maxn * maxn], pt[maxn * maxn];
int nxt[maxn][maxn];

ll range_hash[maxn][maxn];
ll get_hash(int i, int j)
{
    return range_hash[i][j];
}

unordered_map < ll, ll > rev;
void solve()
{
    cin >> n >> s >> a >> b >> c;
    s = "#" + s;
    pw[0] = 1;
    for (int i = 1; i <= n; i ++)
    {
        h[i] = h[i - 1] * base + (s[i] - 'a' + 1);
        pw[i] = pw[i - 1] * base;
    }
    vector < ll > cp;
    for (int i = 1; i <= n; i ++)
        for (int j = i; j <= n; j ++)
        {
            range_hash[i][j] = h[j] - h[i - 1] * pw[j - i + 1];
            cp.push_back(range_hash[i][j]);
        }
    ///cp.erase(unique(cp.begin(), cp.end()), cp.end());
    ///sort(cp.begin(), cp.end());
    int cnt = 0;
    for (int i = 0; i < cp.size(); i ++)
    {
        if (rev[cp[i]] == 0)
        rev[cp[i]] = i + 1;
    }

    for (int i = 1; i <= n; i ++)
        for (int j = i; j <= n; j ++)
            range_hash[i][j] = rev[range_hash[i][j]];


    for (int i = n; i > 0; i --)
    {

            ////cout << "------------" << endl;
        for (int p = i; p <= n; p ++)
        {
            int cur_hs = range_hash[i][p];
            occ[cur_hs].push_back(i);
            /**cout << "hash " << i << " " << p << endl;
            cout << "occurrences: ";
            for (int ds : occ[cur_hs])
                cout << ds << " ";
            cout << endl;*/
            if (occ[cur_hs].size() == 1)
            {
                hs_dp[cur_hs] = hs_dp[range_hash[i + 1][p]] + a;

                nxt[p - i + 1][p] = n + 1;
                pt[cur_hs] = -1;
            }
            else
            {
                hs_dp[cur_hs] = min(hs_dp[cur_hs], hs_dp[range_hash[i + 1][p]] + a);
                if (pt[cur_hs] == -1)
                {
                    pt[cur_hs] ++;
                }
                while(occ[cur_hs][pt[cur_hs]] > p)
                    pt[cur_hs] ++;
                pt[cur_hs] --;
                ///cout << i << endl;
                if (pt[cur_hs] == -1)
                    nxt[p - i + 1][p] = n + 1;
                else
                    nxt[p - i + 1][p] = occ[cur_hs][pt[cur_hs]] + (p - i);
            ///cout << "step " << i << " " << p << " " << nxt[p - i + 1][p] << endl;
            }
        }



        for (int k = i; k <= n; k ++)
        {
            int clip_hash, clip_len = k - i + 1;
            clip_hash = get_hash(i, k);
            ll cost = hs_dp[clip_hash] + b + c;

            if (occ[get_hash(i, k)].size() < 2)
                break;
            int pos = k, pt = (int)(occ[clip_hash].size()) - 1;
            ///dp[get_hash(i, k)] = min(dp[get_hash(i, k)], cos);
            //cout << "-------------------" << endl;
            ///cout << i << " " << k << endl;
            while(pos <= n)
            {
                //cout << pos << " " << cost << endl;
                int to = nxt[clip_len][pos];
                if (to > n)
                    break;
                cost = cost + (to - pos - clip_len) * a;
                cost = cost + c;
                ///cout << "stop " << occ[clip_hash][pt] << endl;
                ///cost = cost + (occ[clip_hash][pt] - pos) * a;
                ///cost = cost + c;
                pos = to;
                hs_dp[range_hash[i][pos]] = min(hs_dp[range_hash[i][pos]], cost);

                /**int to = pos + clip_len - 1;

                if (to > n)
                {
                    break;
                }

                if (get_hash(pos, to) == clip_hash)
                {

                    cost = cost + c;
                    pos = pos + clip_len;
                    hs_dp[get_hash(i, to)] = min(hs_dp[get_hash(i, to)], cost);
                }
                else
                {
                    pos ++;
                    cost += a;
                }*/
            }

        }
        for (int k = i; k <= n; k ++)
        {
            hs_dp[range_hash[i][k]] = min(hs_dp[range_hash[i][k]], hs_dp[range_hash[i][k - 1]] + a);
        }

    }

    /**for (int i = 1; i <= n; i ++, cout << endl)
        for (int j = i; j <= n; j ++)
            cout << hs_dp[get_hash(i, j)] << " ";*/

    cout << hs_dp[range_hash[1][n]] << endl;


}

int main()
{
    solve();
    return 0;
}

Compilation message

copypaste3.cpp: In function 'void solve()':
copypaste3.cpp:60:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   60 |     for (int i = 0; i < cp.size(); i ++)
      |                     ~~^~~~~~~~~~~
copypaste3.cpp:120:26: warning: unused variable 'pt' [-Wunused-variable]
  120 |             int pos = k, pt = (int)(occ[clip_hash].size()) - 1;
      |                          ^~
copypaste3.cpp:59:9: warning: unused variable 'cnt' [-Wunused-variable]
   59 |     int cnt = 0;
      |         ^~~
# Verdict Execution time Memory Grader output
1 Correct 63 ms 148176 KB Output is correct
2 Correct 64 ms 148236 KB Output is correct
3 Correct 65 ms 148264 KB Output is correct
4 Correct 69 ms 148240 KB Output is correct
5 Correct 66 ms 148212 KB Output is correct
6 Correct 65 ms 148224 KB Output is correct
7 Correct 68 ms 148212 KB Output is correct
8 Correct 65 ms 148280 KB Output is correct
9 Correct 64 ms 148248 KB Output is correct
10 Correct 63 ms 148300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 64 ms 148416 KB Output is correct
2 Correct 65 ms 148244 KB Output is correct
3 Correct 252 ms 204856 KB Output is correct
4 Correct 268 ms 212440 KB Output is correct
5 Correct 333 ms 221472 KB Output is correct
6 Correct 367 ms 231704 KB Output is correct
7 Correct 414 ms 243464 KB Output is correct
8 Correct 401 ms 243436 KB Output is correct
9 Correct 401 ms 243388 KB Output is correct
10 Correct 66 ms 148176 KB Output is correct
11 Correct 67 ms 148284 KB Output is correct
12 Correct 72 ms 148292 KB Output is correct
13 Correct 65 ms 148308 KB Output is correct
14 Correct 64 ms 148228 KB Output is correct
15 Correct 70 ms 148428 KB Output is correct
16 Correct 64 ms 148416 KB Output is correct
17 Correct 73 ms 148248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 63 ms 148176 KB Output is correct
2 Correct 64 ms 148236 KB Output is correct
3 Correct 65 ms 148264 KB Output is correct
4 Correct 69 ms 148240 KB Output is correct
5 Correct 66 ms 148212 KB Output is correct
6 Correct 65 ms 148224 KB Output is correct
7 Correct 68 ms 148212 KB Output is correct
8 Correct 65 ms 148280 KB Output is correct
9 Correct 64 ms 148248 KB Output is correct
10 Correct 63 ms 148300 KB Output is correct
11 Correct 64 ms 148312 KB Output is correct
12 Correct 65 ms 148384 KB Output is correct
13 Correct 64 ms 148376 KB Output is correct
14 Correct 67 ms 148352 KB Output is correct
15 Correct 69 ms 148452 KB Output is correct
16 Correct 68 ms 148332 KB Output is correct
17 Correct 68 ms 148248 KB Output is correct
18 Correct 65 ms 148172 KB Output is correct
19 Correct 66 ms 148300 KB Output is correct
20 Correct 68 ms 148316 KB Output is correct
21 Correct 65 ms 148428 KB Output is correct
22 Correct 65 ms 148440 KB Output is correct
23 Correct 64 ms 148556 KB Output is correct
24 Correct 74 ms 148556 KB Output is correct
25 Correct 66 ms 148516 KB Output is correct
26 Correct 67 ms 148572 KB Output is correct
27 Correct 64 ms 148512 KB Output is correct
28 Correct 65 ms 148516 KB Output is correct
29 Correct 65 ms 148520 KB Output is correct
30 Correct 64 ms 148436 KB Output is correct
31 Correct 64 ms 148448 KB Output is correct
32 Correct 65 ms 148460 KB Output is correct
33 Correct 65 ms 148540 KB Output is correct
34 Correct 65 ms 148196 KB Output is correct
35 Correct 65 ms 148236 KB Output is correct
36 Correct 65 ms 148200 KB Output is correct
37 Correct 65 ms 148300 KB Output is correct
38 Correct 66 ms 148356 KB Output is correct
39 Correct 65 ms 148496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 63 ms 148176 KB Output is correct
2 Correct 64 ms 148236 KB Output is correct
3 Correct 65 ms 148264 KB Output is correct
4 Correct 69 ms 148240 KB Output is correct
5 Correct 66 ms 148212 KB Output is correct
6 Correct 65 ms 148224 KB Output is correct
7 Correct 68 ms 148212 KB Output is correct
8 Correct 65 ms 148280 KB Output is correct
9 Correct 64 ms 148248 KB Output is correct
10 Correct 63 ms 148300 KB Output is correct
11 Correct 64 ms 148312 KB Output is correct
12 Correct 65 ms 148384 KB Output is correct
13 Correct 64 ms 148376 KB Output is correct
14 Correct 67 ms 148352 KB Output is correct
15 Correct 69 ms 148452 KB Output is correct
16 Correct 68 ms 148332 KB Output is correct
17 Correct 68 ms 148248 KB Output is correct
18 Correct 65 ms 148172 KB Output is correct
19 Correct 66 ms 148300 KB Output is correct
20 Correct 68 ms 148316 KB Output is correct
21 Correct 65 ms 148428 KB Output is correct
22 Correct 65 ms 148440 KB Output is correct
23 Correct 64 ms 148556 KB Output is correct
24 Correct 74 ms 148556 KB Output is correct
25 Correct 66 ms 148516 KB Output is correct
26 Correct 67 ms 148572 KB Output is correct
27 Correct 64 ms 148512 KB Output is correct
28 Correct 65 ms 148516 KB Output is correct
29 Correct 65 ms 148520 KB Output is correct
30 Correct 64 ms 148436 KB Output is correct
31 Correct 64 ms 148448 KB Output is correct
32 Correct 65 ms 148460 KB Output is correct
33 Correct 65 ms 148540 KB Output is correct
34 Correct 65 ms 148196 KB Output is correct
35 Correct 65 ms 148236 KB Output is correct
36 Correct 65 ms 148200 KB Output is correct
37 Correct 65 ms 148300 KB Output is correct
38 Correct 66 ms 148356 KB Output is correct
39 Correct 65 ms 148496 KB Output is correct
40 Correct 64 ms 149268 KB Output is correct
41 Correct 68 ms 150304 KB Output is correct
42 Correct 70 ms 151884 KB Output is correct
43 Correct 69 ms 151892 KB Output is correct
44 Correct 73 ms 151952 KB Output is correct
45 Correct 74 ms 151920 KB Output is correct
46 Correct 70 ms 151912 KB Output is correct
47 Correct 68 ms 150684 KB Output is correct
48 Correct 69 ms 151360 KB Output is correct
49 Correct 70 ms 151600 KB Output is correct
50 Correct 71 ms 151644 KB Output is correct
51 Correct 70 ms 151240 KB Output is correct
52 Correct 68 ms 151240 KB Output is correct
53 Correct 70 ms 151912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 63 ms 148176 KB Output is correct
2 Correct 64 ms 148236 KB Output is correct
3 Correct 65 ms 148264 KB Output is correct
4 Correct 69 ms 148240 KB Output is correct
5 Correct 66 ms 148212 KB Output is correct
6 Correct 65 ms 148224 KB Output is correct
7 Correct 68 ms 148212 KB Output is correct
8 Correct 65 ms 148280 KB Output is correct
9 Correct 64 ms 148248 KB Output is correct
10 Correct 63 ms 148300 KB Output is correct
11 Correct 64 ms 148312 KB Output is correct
12 Correct 65 ms 148384 KB Output is correct
13 Correct 64 ms 148376 KB Output is correct
14 Correct 67 ms 148352 KB Output is correct
15 Correct 69 ms 148452 KB Output is correct
16 Correct 68 ms 148332 KB Output is correct
17 Correct 68 ms 148248 KB Output is correct
18 Correct 65 ms 148172 KB Output is correct
19 Correct 66 ms 148300 KB Output is correct
20 Correct 68 ms 148316 KB Output is correct
21 Correct 65 ms 148428 KB Output is correct
22 Correct 65 ms 148440 KB Output is correct
23 Correct 64 ms 148556 KB Output is correct
24 Correct 74 ms 148556 KB Output is correct
25 Correct 66 ms 148516 KB Output is correct
26 Correct 67 ms 148572 KB Output is correct
27 Correct 64 ms 148512 KB Output is correct
28 Correct 65 ms 148516 KB Output is correct
29 Correct 65 ms 148520 KB Output is correct
30 Correct 64 ms 148436 KB Output is correct
31 Correct 64 ms 148448 KB Output is correct
32 Correct 65 ms 148460 KB Output is correct
33 Correct 65 ms 148540 KB Output is correct
34 Correct 65 ms 148196 KB Output is correct
35 Correct 65 ms 148236 KB Output is correct
36 Correct 65 ms 148200 KB Output is correct
37 Correct 65 ms 148300 KB Output is correct
38 Correct 66 ms 148356 KB Output is correct
39 Correct 65 ms 148496 KB Output is correct
40 Correct 64 ms 149268 KB Output is correct
41 Correct 68 ms 150304 KB Output is correct
42 Correct 70 ms 151884 KB Output is correct
43 Correct 69 ms 151892 KB Output is correct
44 Correct 73 ms 151952 KB Output is correct
45 Correct 74 ms 151920 KB Output is correct
46 Correct 70 ms 151912 KB Output is correct
47 Correct 68 ms 150684 KB Output is correct
48 Correct 69 ms 151360 KB Output is correct
49 Correct 70 ms 151600 KB Output is correct
50 Correct 71 ms 151644 KB Output is correct
51 Correct 70 ms 151240 KB Output is correct
52 Correct 68 ms 151240 KB Output is correct
53 Correct 70 ms 151912 KB Output is correct
54 Correct 89 ms 162832 KB Output is correct
55 Correct 113 ms 168736 KB Output is correct
56 Correct 254 ms 210312 KB Output is correct
57 Correct 249 ms 210552 KB Output is correct
58 Correct 221 ms 210656 KB Output is correct
59 Correct 265 ms 210668 KB Output is correct
60 Correct 225 ms 210804 KB Output is correct
61 Correct 150 ms 185548 KB Output is correct
62 Correct 114 ms 169312 KB Output is correct
63 Correct 249 ms 202920 KB Output is correct
64 Correct 230 ms 203120 KB Output is correct
65 Correct 159 ms 191168 KB Output is correct
66 Correct 181 ms 191168 KB Output is correct
67 Correct 223 ms 210756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 63 ms 148176 KB Output is correct
2 Correct 64 ms 148236 KB Output is correct
3 Correct 65 ms 148264 KB Output is correct
4 Correct 69 ms 148240 KB Output is correct
5 Correct 66 ms 148212 KB Output is correct
6 Correct 65 ms 148224 KB Output is correct
7 Correct 68 ms 148212 KB Output is correct
8 Correct 65 ms 148280 KB Output is correct
9 Correct 64 ms 148248 KB Output is correct
10 Correct 63 ms 148300 KB Output is correct
11 Correct 64 ms 148416 KB Output is correct
12 Correct 65 ms 148244 KB Output is correct
13 Correct 252 ms 204856 KB Output is correct
14 Correct 268 ms 212440 KB Output is correct
15 Correct 333 ms 221472 KB Output is correct
16 Correct 367 ms 231704 KB Output is correct
17 Correct 414 ms 243464 KB Output is correct
18 Correct 401 ms 243436 KB Output is correct
19 Correct 401 ms 243388 KB Output is correct
20 Correct 66 ms 148176 KB Output is correct
21 Correct 67 ms 148284 KB Output is correct
22 Correct 72 ms 148292 KB Output is correct
23 Correct 65 ms 148308 KB Output is correct
24 Correct 64 ms 148228 KB Output is correct
25 Correct 70 ms 148428 KB Output is correct
26 Correct 64 ms 148416 KB Output is correct
27 Correct 73 ms 148248 KB Output is correct
28 Correct 64 ms 148312 KB Output is correct
29 Correct 65 ms 148384 KB Output is correct
30 Correct 64 ms 148376 KB Output is correct
31 Correct 67 ms 148352 KB Output is correct
32 Correct 69 ms 148452 KB Output is correct
33 Correct 68 ms 148332 KB Output is correct
34 Correct 68 ms 148248 KB Output is correct
35 Correct 65 ms 148172 KB Output is correct
36 Correct 66 ms 148300 KB Output is correct
37 Correct 68 ms 148316 KB Output is correct
38 Correct 65 ms 148428 KB Output is correct
39 Correct 65 ms 148440 KB Output is correct
40 Correct 64 ms 148556 KB Output is correct
41 Correct 74 ms 148556 KB Output is correct
42 Correct 66 ms 148516 KB Output is correct
43 Correct 67 ms 148572 KB Output is correct
44 Correct 64 ms 148512 KB Output is correct
45 Correct 65 ms 148516 KB Output is correct
46 Correct 65 ms 148520 KB Output is correct
47 Correct 64 ms 148436 KB Output is correct
48 Correct 64 ms 148448 KB Output is correct
49 Correct 65 ms 148460 KB Output is correct
50 Correct 65 ms 148540 KB Output is correct
51 Correct 65 ms 148196 KB Output is correct
52 Correct 65 ms 148236 KB Output is correct
53 Correct 65 ms 148200 KB Output is correct
54 Correct 65 ms 148300 KB Output is correct
55 Correct 66 ms 148356 KB Output is correct
56 Correct 65 ms 148496 KB Output is correct
57 Correct 64 ms 149268 KB Output is correct
58 Correct 68 ms 150304 KB Output is correct
59 Correct 70 ms 151884 KB Output is correct
60 Correct 69 ms 151892 KB Output is correct
61 Correct 73 ms 151952 KB Output is correct
62 Correct 74 ms 151920 KB Output is correct
63 Correct 70 ms 151912 KB Output is correct
64 Correct 68 ms 150684 KB Output is correct
65 Correct 69 ms 151360 KB Output is correct
66 Correct 70 ms 151600 KB Output is correct
67 Correct 71 ms 151644 KB Output is correct
68 Correct 70 ms 151240 KB Output is correct
69 Correct 68 ms 151240 KB Output is correct
70 Correct 70 ms 151912 KB Output is correct
71 Correct 89 ms 162832 KB Output is correct
72 Correct 113 ms 168736 KB Output is correct
73 Correct 254 ms 210312 KB Output is correct
74 Correct 249 ms 210552 KB Output is correct
75 Correct 221 ms 210656 KB Output is correct
76 Correct 265 ms 210668 KB Output is correct
77 Correct 225 ms 210804 KB Output is correct
78 Correct 150 ms 185548 KB Output is correct
79 Correct 114 ms 169312 KB Output is correct
80 Correct 249 ms 202920 KB Output is correct
81 Correct 230 ms 203120 KB Output is correct
82 Correct 159 ms 191168 KB Output is correct
83 Correct 181 ms 191168 KB Output is correct
84 Correct 223 ms 210756 KB Output is correct
85 Correct 888 ms 293692 KB Output is correct
86 Correct 2519 ms 516416 KB Output is correct
87 Correct 2273 ms 517064 KB Output is correct
88 Correct 2289 ms 517408 KB Output is correct
89 Correct 2268 ms 517612 KB Output is correct
90 Correct 2334 ms 517672 KB Output is correct
91 Correct 561 ms 255652 KB Output is correct
92 Correct 490 ms 246996 KB Output is correct
93 Correct 1870 ms 430188 KB Output is correct
94 Correct 1835 ms 442588 KB Output is correct
95 Correct 1462 ms 376660 KB Output is correct
96 Correct 1524 ms 376512 KB Output is correct
97 Correct 2283 ms 517668 KB Output is correct