QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#188951#6635. Strange Keyboarducup-team004#TL 846ms6788kbC++202.2kb2023-09-26 17:51:212023-09-26 17:51:21

Judging History

你现在查看的是最新测评结果

  • [2023-09-26 17:51:21]
  • 评测
  • 测评结果:TL
  • 用时:846ms
  • 内存:6788kb
  • [2023-09-26 17:51:21]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = long long;

constexpr i64 inf = 1E18;

constexpr int N = 1E6 + 10;

int trie[N][26];
i64 val[N];
int tot;

int newNode() {
    tot++;
    std::fill(trie[tot], trie[tot] + 26, 0);
    val[tot] = inf;
    return tot;
}

void solve() {
    int N, K;
    std::cin >> N >> K;
    
    std::vector<std::string> S(N);
    for (int i = 0; i < N; i++) {
        std::cin >> S[i];
    }
    
    std::string T;
    std::cin >> T;
    
    int L = T.size();
    
    std::vector<i64> min(K, inf);
    min[0] = 0;
    for (int i = 0; i < N; i++) {
        int l = S[i].size();
        min[l % K] = std::min(min[l % K], 1LL * l + K);
    }
    
    std::vector<i64> f(K, inf);
    f[0] = 0;
    for (int i = 0; i < K; i++) {
        int g = std::gcd(K, i);
        for (int j = 0; j < g; j++) {
            for (int k = 0; k < K / g * 2; k++) {
                int a = (j + k * i) % K;
                int b = (j + (k + 1) * i) % K;
                f[b] = std::min(f[b], f[a] + min[i]);
            }
        }
    }
    
    tot = 0;
    newNode();
    
    for (int i = 0; i < N; i++) {
        int p = 1;
        for (int j = 0; j < S[i].size(); j++) {
            int x = S[i][j] - 'a';
            if (!trie[p][x]) {
                trie[p][x] = newNode();
            }
            p = trie[p][x];
            val[p] = std::min(val[p], int(S[i].size()) + K + f[(K - (S[i].size() - j - 1) % K) % K]);
        }
    }
    
    std::vector<i64> dp(L + 1, inf);
    dp[0] = 0;
    for (int i = 0; i < L; i++) {
        int p = 1;
        for (int j = i; j < L; j++) {
            int x = T[j] - 'a';
            p = trie[p][x];
            if (!p) {
                continue;
            }
            dp[j + 1] = std::min(dp[j + 1], dp[i] + val[p]);
        }
    }
    i64 ans = dp[L];
    if (ans == inf) {
        ans = -1;
    } else {
        ans = (ans - L) / K;
    }
    std::cout << ans << "\n";
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int t;
    std::cin >> t;
    
    while (t--) {
        solve();
    }
    
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 5492kb

input:

2
2 3
defgh
abc
abcde
1 1
a
b

output:

3
-1

result:

ok 2 number(s): "3 -1"

Test #2:

score: 0
Accepted
time: 120ms
memory: 6788kb

input:

1
1413 4867
yumnkkghwtqnhpmmsbfwypcwudihegsvtdaykueuqozagpgdnhbwzoqrqswyctzonwmjdjmmpyzbyihhpyznmqltcuckqmcybbdafoexqztxhzlfjowznisnxtgoiqjztogldyegajmegalqgawrzotuntocmsagvkzdnwhmaewuxiuhvintpzwyrkgapnmegpveyuuorrfvrfakjyetfaoqtvwghlslsvmahrkvzkwpaviufknrpuapicqdnn
yumnkkghwtqnhpmmsbfwypcwudihegsvt...

output:

10

result:

ok 1 number(s): "10"

Test #3:

score: 0
Accepted
time: 846ms
memory: 5844kb

input:

10
446 4905
afigcjhcgagabbiccehjcjajigghgbjjadccicghggijjdfeciaccgheedjdhgfjdfdbgidbbdjaiehhceeehchhabhaideggjbjajgfgicfdggahhbjgdebccbgbiedhehaebdccdfdffaacjcfbgjeegbahhbgcdjigijajheidchbddicehhhjbeiaajgedhdcjiefdgdbjjfaegheeidieheecaicciaajbabiidcecefgiicccdidegeica
afigcjhcgagabbiccehjcjajigghgbj...

output:

3
2
2
11
6
5
1
1
1
1

result:

ok 10 numbers

Test #4:

score: -100
Time Limit Exceeded

input:

100
140 4879
baabaababbababbaabababaaababbbabbbbbbabbababbbabbbbabbbbbbaabbbbbbbbabaabbbaabaabbbaabbabaabaabbbabbbababbbaabbabaaaaabbaaabbbabb
baa
baabaababbababbaabababaaababbbabbbbbbabbab
baabaababbababbaabababaaabab
baabaababbababbaabababaaababbbabbb
baabaababbababbaabababaaababbbabbbbbbabbababbb...

output:


result: