QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#260796#6635. Strange Keyboarducup-team1198#TL 1464ms11136kbC++203.0kb2023-11-22 15:25:502023-11-22 15:25:51

Judging History

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

  • [2023-11-22 15:25:51]
  • 评测
  • 测评结果:TL
  • 用时:1464ms
  • 内存:11136kb
  • [2023-11-22 15:25:50]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define pii pair<int, int>
#define ld long double
#define all(a) (a).begin(), (a).end()

const int MAXN = 1000100;
const long long INF = 1e18;
int go[MAXN][26];
int suff[MAXN];
int len[MAXN];
long long cost[MAXN];
int nodes_cnt = 0;

int node() {
    fill(go[nodes_cnt], go[nodes_cnt] + 26, -1);
    suff[nodes_cnt] = -1;
    cost[nodes_cnt] = INF;
    ++nodes_cnt;
    return nodes_cnt - 1;
}

void solve() {
    nodes_cnt = 0;
    node();
    len[0] = 0;

    int n, k;
    cin >> n >> k;
    vector<long long> dp(k, INF);
    vector<long long> min_len(k, INF);
    vector<string> lines(n);
    for (int i = 0; i < n; ++i) {
        cin >> lines[i];
        long long sz = lines[i].size();
        min_len[sz % k] = min(min_len[sz % k], sz);
        int cur = 0;
        for (char c : lines[i]) {
            if (go[cur][c - 'a'] == -1) {
                go[cur][c - 'a'] = node();
                len[go[cur][c - 'a']] = len[cur] + 1;
            }
            cur = go[cur][c - 'a'];
        }
    }

    // Aho Korasik
    deque<int> Q;
    Q.emplace_back(0);
    while (!Q.empty()) {
        int v = Q.front();
        Q.pop_front();
        for (int i = 0; i < 26; ++i) {
            if (go[v][i] == -1) {
                go[v][i] = v == 0 ? 0 : go[suff[v]][i];
            } else {
                Q.emplace_back(go[v][i]);
                suff[go[v][i]] = v == 0 ? 0 : go[suff[v]][i];
            }
        }
    }
    // dp r

    dp[0] = 0;
    vector<bool> was(k, false);
    for (int i = 0; i < k; ++i) {
        int mn = -1;
        for (int j = 0; j < k; ++j) {
            if (was[j])
                continue;
            if (mn == -1 || dp[j] < dp[mn])
                mn = j;
        }
        was[mn] = true;
        for (int i = 0; i < k; ++i) {
            long long extra_r = min_len[i] + mn;
            long long opt = dp[mn] + extra_r / k + 1;
            dp[(mn + i) % k] = min(dp[(mn + i) % k], opt);
        }
    }

    for (int i = 0; i < n; ++i) {
        int cur = 0;
        for (int j = 0; j < lines[i].size(); ++j) {
            cur = go[cur][lines[i][j] - 'a'];
            int rest = lines[i].size() - j - 1;
            int tmp = (k - rest % k) % k;
            cost[cur] = min(cost[cur], (rest + tmp) / k + dp[tmp] + 1);
        }
    }

    string t;
    cin >> t;
    vector<long long> final_dp(t.size() + 1, INF);
    final_dp[0] = 0;
    int cur = 0;
    for (int i = 0; i < t.size(); ++i) {
        cur = go[cur][t[i] - 'a'];
        int tmp_cur = cur;
        while (tmp_cur != 0) {
            final_dp[i + 1] = min(final_dp[i + 1], final_dp[i + 1 - len[tmp_cur]] + cost[tmp_cur]);
            tmp_cur = suff[tmp_cur];
        }
    }
    if (final_dp.back() == INF)
        cout << -1 << '\n';
    else
        cout << final_dp.back() << '\n';

}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 9888kb

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: 152ms
memory: 11136kb

input:

1
1413 4867
yumnkkghwtqnhpmmsbfwypcwudihegsvtdaykueuqozagpgdnhbwzoqrqswyctzonwmjdjmmpyzbyihhpyznmqltcuckqmcybbdafoexqztxhzlfjowznisnxtgoiqjztogldyegajmegalqgawrzotuntocmsagvkzdnwhmaewuxiuhvintpzwyrkgapnmegpveyuuorrfvrfakjyetfaoqtvwghlslsvmahrkvzkwpaviufknrpuapicqdnn
yumnkkghwtqnhpmmsbfwypcwudihegsvt...

output:

10

result:

ok 1 number(s): "10"

Test #3:

score: 0
Accepted
time: 1464ms
memory: 9996kb

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: