Submission #50840597


Source Code Expand

/**
 *  @the_hyp0cr1t3
 *  02.03.2024 18:35
**/
#include <bits/stdc++.h>

auto chmin = [](auto &x, auto y) {
    x = std::min(x, y);
};

auto prefix_function(const std::string &s) {
    int n = s.size();
    std::vector<int> pi(n);
    for (int z = 1; z < n; z++) {
        int p = pi[z - 1];
        while (p and s[z] != s[p])
            p = pi[p - 1];
        pi[z] = p + (s[z] == s[p]);
    }
    return pi;
}

int main() {
    std::cin.tie(nullptr)->sync_with_stdio(false);

    int n;
    std::cin >> n;
    std::vector<std::string> s(n);
    for (auto &x : s)
        std::cin >> x;

    std::vector d(n, std::vector<int>(n));
    std::vector dp(1 << n, std::vector<int>(n, 3e5));
    for (int i = 0; i < n; i++) {
        int m = 1 << i;
        for (int j = 0; j < n; j++) {
            auto pi = prefix_function(s[j] + '.' + s[i]);
            int a = s[i].length(), b = s[j].length();
            chmin(d[i][j], -pi.back());
            for (int k = 1; k <= a; k++)
                if (pi[b + k] == b) m |= 1 << j;
        }
        for (int j = m; j > 0; j = m & (j - 1))
            chmin(dp[j][i], (int)s[i].length());
    }

    for (int m = 1; m < 1 << n; m++) {
        for (int i = 0; i < n; i++) {
            if (~m >> i & 1) continue;
            for (int j = 0; j < n; j++) {
                if (m >> j & 1) continue;
                chmin(dp[m | (1 << j)][j], dp[m][i] + d[i][j] + (int)s[j].length());
            }
        }
    }

    std::cout << std::ranges::min(dp.back()) << '\n';
}

Submission Info

Submission Time
Task G - Compress Strings
User the_hyp0cr1t3
Language C++ 20 (gcc 12.2)
Score 0
Code Size 1570 Byte
Status WA
Exec Time 820 ms
Memory 129044 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 600
Status
AC × 3
AC × 37
WA × 2
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 02_random2_00.txt, 02_random2_01.txt, 02_random2_02.txt, 02_random2_03.txt, 02_random2_04.txt, 02_random2_05.txt, 02_random2_06.txt, 02_random2_07.txt, 02_random2_08.txt, 02_random2_09.txt, 02_random2_10.txt, 02_random2_11.txt, 02_random2_12.txt, 02_random2_13.txt, 02_random2_14.txt, 02_random2_15.txt, 02_random2_16.txt, 02_random2_17.txt, 02_random2_18.txt, 02_random2_19.txt, 02_random2_20.txt, 02_random2_21.txt, 02_random2_22.txt, 02_random2_23.txt, 03_random3_00.txt, 03_random3_01.txt, 03_random3_02.txt, 04_handmade_00.txt, 04_handmade_01.txt, 04_handmade_02.txt, 04_handmade_03.txt, 04_handmade_04.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 3532 KiB
00_sample_01.txt AC 1 ms 3608 KiB
00_sample_02.txt AC 1 ms 3528 KiB
01_random_00.txt AC 15 ms 6532 KiB
01_random_01.txt AC 3 ms 3892 KiB
01_random_02.txt AC 1 ms 3668 KiB
01_random_03.txt AC 1 ms 3620 KiB
02_random2_00.txt WA 693 ms 127304 KiB
02_random2_01.txt WA 693 ms 127712 KiB
02_random2_02.txt AC 676 ms 128468 KiB
02_random2_03.txt AC 684 ms 128032 KiB
02_random2_04.txt AC 678 ms 128312 KiB
02_random2_05.txt AC 674 ms 128568 KiB
02_random2_06.txt AC 665 ms 127656 KiB
02_random2_07.txt AC 662 ms 127996 KiB
02_random2_08.txt AC 651 ms 128356 KiB
02_random2_09.txt AC 654 ms 127312 KiB
02_random2_10.txt AC 640 ms 128460 KiB
02_random2_11.txt AC 639 ms 129044 KiB
02_random2_12.txt AC 686 ms 126448 KiB
02_random2_13.txt AC 685 ms 126456 KiB
02_random2_14.txt AC 688 ms 126496 KiB
02_random2_15.txt AC 686 ms 126456 KiB
02_random2_16.txt AC 681 ms 126516 KiB
02_random2_17.txt AC 680 ms 126516 KiB
02_random2_18.txt AC 661 ms 126428 KiB
02_random2_19.txt AC 664 ms 126632 KiB
02_random2_20.txt AC 664 ms 126536 KiB
02_random2_21.txt AC 656 ms 126456 KiB
02_random2_22.txt AC 653 ms 126548 KiB
02_random2_23.txt AC 655 ms 126556 KiB
03_random3_00.txt AC 670 ms 126540 KiB
03_random3_01.txt AC 664 ms 126452 KiB
03_random3_02.txt AC 667 ms 126460 KiB
04_handmade_00.txt AC 1 ms 3532 KiB
04_handmade_01.txt AC 820 ms 126456 KiB
04_handmade_02.txt AC 4 ms 5252 KiB
04_handmade_03.txt AC 665 ms 126528 KiB
04_handmade_04.txt AC 659 ms 126508 KiB