QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#168576#6635. Strange Keyboarducup-team1209#WA 334ms266556kbC++202.9kb2023-09-08 17:29:452023-09-08 17:29:46

Judging History

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

  • [2023-09-08 17:29:46]
  • 评测
  • 测评结果:WA
  • 用时:334ms
  • 内存:266556kb
  • [2023-09-08 17:29:45]
  • 提交

answer

#include <bits/stdc++.h>
#define cs const
#define pb push_back
using namespace std;
using ll = long long ; 
using pi = pair <int, int> ;

void cmn(ll &x, ll y) {
    if(x > y) x = y; 
}
void cmn(int &x, int y) {
    if(x > y) x = y; 
}

cs int N = 1e6 + 5;
cs int M = 5e3 + 5; 

int T; 
int ch[N][26], nd; 
int n, k, m; 
char S[N];

vector <int> p[N];
ll e[M][M];


int norm(int x) {
    return x >= k ? x - k : 0;
}
void work(vector <ll> &F, vector <ll> d) {
    F[0] = 0; 
    for(int i = 1; i < k; i++) 
    if(d[i] < 1e18) {
        int gcd = std::__gcd(i, k);
        for(int x = 0;x < gcd;++x) {
            int N = k / gcd;
            int u = x;
            
            for(int cc = 0;cc < N * 2;++cc) {
                int v = u + i;
                if(v >= k) {
                    v -= k;
                    cmn(F[v], F[u] + d[i] + 1);
                } else {
                    cmn(F[v], F[u] + d[i]);
                }
                u = v;
            }
        }
    }
}

int a[N], le[N];

ll calc(int x, int d, cs vector <ll> &F) {
    ll ans = 1e18;
    for(auto c : p[x]) {
        // le[c] - > d 
        int dlt = le[c] - d; 
        int w = dlt / k; 
        dlt -= w * k; 
        if(dlt) w += F[k - dlt] + 1;
        cmn(ans, w);
    }
    return ans + 1; 
}

void Main() {
    scanf("%d%d", &n, &k);
    vector <ll> d(k + 1, 1e18); 
    d[k] = 1; 
    for(int i = 0; i < n; i++) {
        scanf("%s", S);
        int len = strlen(S);
        int x = 0; 
        for(int j = 0; j < len; j++) {
            int c = S[j] - 'a';
            if(!ch[x][c]) ch[x][c] = ++ nd; 
            x = ch[x][c];
            p[x].pb(i);
        }
        a[i] = x; 
        le[i] = len; 
        cmn(d[len % k], (ll) len / k + 1);
    }

    vector <ll> F(k + 1, 1e18);
    work(F, d);

    scanf("%s", S);
    m = strlen(S);

    for(int i = 0; i <= m; i++)
    for(int j = i; j <= m; j++)
    e[i][j] = 1e18; 

    vector <ll> ans(nd + 1, -1);
    for(int i = 0; i < m; i++) {
        int x = 0; 
        for(int j = i; j < m; j++) {
            int c = S[j] - 'a';
            x = ch[x][c];
            if(x == 0) break;
            if(ans[x] == -1) {
                ans[x] = calc(x, j - i + 1, F);
            }
            e[i][j + 1] = ans[x];
        } 
    }
    vector <ll> dp(m + 1, 1e18);
    dp[0] = 0; 
    for(int i = 1; i <= m; i++) {
        for(int j = 0; j < i; j++)
            cmn(dp[i], dp[j] + e[j][i]);
    }
    if(dp[m] < 1e18) {
        cout << dp[m] << '\n';
    }
    else {
        cout << -1 << '\n';
    }
    for(int i = 0; i <= nd; i++) {
        for(int j = 0; j < 26; j++)     
            ch[i][j] = 0;
        p[i].clear();
    }
}

int main() {
    #ifdef zqj 
    freopen("1.in","r",stdin);
    #endif
    cin >> T; 
    while(T--) Main();
    return 0; 
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 8ms
memory: 34684kb

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: 53ms
memory: 234972kb

input:

1
1413 4867
yumnkkghwtqnhpmmsbfwypcwudihegsvtdaykueuqozagpgdnhbwzoqrqswyctzonwmjdjmmpyzbyihhpyznmqltcuckqmcybbdafoexqztxhzlfjowznisnxtgoiqjztogldyegajmegalqgawrzotuntocmsagvkzdnwhmaewuxiuhvintpzwyrkgapnmegpveyuuorrfvrfakjyetfaoqtvwghlslsvmahrkvzkwpaviufknrpuapicqdnn
yumnkkghwtqnhpmmsbfwypcwudihegsvt...

output:

10

result:

ok 1 number(s): "10"

Test #3:

score: 0
Accepted
time: 57ms
memory: 103532kb

input:

10
446 4905
afigcjhcgagabbiccehjcjajigghgbjjadccicghggijjdfeciaccgheedjdhgfjdfdbgidbbdjaiehhceeehchhabhaideggjbjajgfgicfdggahhbjgdebccbgbiedhehaebdccdfdffaacjcfbgjeegbahhbgcdjigijajheidchbddicehhhjbeiaajgedhdcjiefdgdbjjfaegheeidieheecaicciaajbabiidcecefgiicccdidegeica
afigcjhcgagabbiccehjcjajigghgbj...

output:

3
2
2
11
6
5
1
1
1
1

result:

ok 10 numbers

Test #4:

score: 0
Accepted
time: 136ms
memory: 50912kb

input:

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

output:

1
1
1
1
3
1
1
1
1
1
1
3
2
1
1
1
2
1
1
2
1
1
1
1
1
1
1
1
1
4
3
2
1
2
1
1
1
1
1
2
1
1
1
3
1
1
1
2
1
1
1
2
3
1
1
1
2
1
1
1
1
1
1
1
1
3
2
3
1
3
1
1
2
1
2
3
2
1
1
1
3
2
1
2
1
1
1
1
1
1
1
1
1
1
1
1
2
1
4
1

result:

ok 100 numbers

Test #5:

score: 0
Accepted
time: 119ms
memory: 266556kb

input:

1
7 4864
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

205

result:

ok 1 number(s): "205"

Test #6:

score: 0
Accepted
time: 103ms
memory: 211912kb

input:

10
7 4923
baaabbabbbabbbbbabaabaabababbabbaaaabbaaabbbbabbaaababaaaaabaaabbabbbabbaaaabbaabbbaabbaaababaaababbabaababbaababbabaaabaabbaaabaaaababbabbabaabaabaabaabbbbaabaabbbaababbabbabaaaabbbaabbbaaaabaaaaababbaabaaaaababbbbaaaabbbaababbaabbabaabaaaaababaababaabaaaaabababababbabbbabababaaabaababbaa...

output:

4287
228
3671
549
129
372
475
534
336
288

result:

ok 10 numbers

Test #7:

score: -100
Wrong Answer
time: 334ms
memory: 136196kb

input:

100
7 4807
abbababaababbaabbabbaabaababbaaababaabaaabbaaaabababbbaabbaaabababbaabaabbaaaaabbbbaabbbaaabbbbabaabbbaaaaabbbaabbaaaabbaaababbaaabbbbabaabbababababbbabaaabaaaabbbbabbabbbbbaabaaabaababbabaaabbaabbabbabaaababbbabbabbbaababaabaaaabaaabbbbabbaabaababbbabbbbaaaabbabbbaabbaabbbbb
aaaaababaaab...

output:

45
32
11
4
2475
132
50
330
20
6
99
25
126
6
4
14
74
108
208
11
5
67
166
2822
178
1307
548
92
386
493
279
2415
255
262
567
215
46
113
31
651
17
4
8
21
12
100
69
152
15
55
521
146
11
13
181
-77304168344
442
1839
2
5
31
26
122
696
280
77
1
839
11
273
7
178
421
228
6
6
82
116
1
-50545033148
293
519
5
16...

result:

wrong answer 56th numbers differ - expected: '-1', found: '-77304168344'