QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#168618#6635. Strange Keyboarducup-team1209#AC ✓147ms366336kbC++202.9kb2023-09-08 18:05:282023-09-08 18:05:28

Judging History

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

  • [2023-09-08 18:05:28]
  • 评测
  • 测评结果:AC
  • 用时:147ms
  • 内存:366336kb
  • [2023-09-08 18:05:28]
  • 提交

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; 
        ll 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();
    }
    nd = 0; 
}

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: 3ms
memory: 34392kb

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: 63ms
memory: 235100kb

input:

1
1413 4867
yumnkkghwtqnhpmmsbfwypcwudihegsvtdaykueuqozagpgdnhbwzoqrqswyctzonwmjdjmmpyzbyihhpyznmqltcuckqmcybbdafoexqztxhzlfjowznisnxtgoiqjztogldyegajmegalqgawrzotuntocmsagvkzdnwhmaewuxiuhvintpzwyrkgapnmegpveyuuorrfvrfakjyetfaoqtvwghlslsvmahrkvzkwpaviufknrpuapicqdnn
yumnkkghwtqnhpmmsbfwypcwudihegsvt...

output:

10

result:

ok 1 number(s): "10"

Test #3:

score: 0
Accepted
time: 59ms
memory: 98704kb

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: 147ms
memory: 45188kb

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: 102ms
memory: 268060kb

input:

1
7 4864
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

205

result:

ok 1 number(s): "205"

Test #6:

score: 0
Accepted
time: 55ms
memory: 114268kb

input:

10
7 4923
baaabbabbbabbbbbabaabaabababbabbaaaabbaaabbbbabbaaababaaaaabaaabbabbbabbaaaabbaabbbaabbaaababaaababbabaababbaababbabaaabaabbaaabaaaababbabbabaabaabaabaabbbbaabaabbbaababbabbabaaaabbbaabbbaaaabaaaaababbaabaaaaababbbbaaaabbbaababbaabbabaabaaaaababaababaabaaaaabababababbabbbabababaaabaababbaa...

output:

4287
228
3671
549
129
372
475
534
336
288

result:

ok 10 numbers

Test #7:

score: 0
Accepted
time: 56ms
memory: 50868kb

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
-1
442
1839
2
5
31
26
122
696
280
77
1
839
11
273
7
178
421
228
6
6
82
116
1
-1
293
519
5
160
15
126
13
31
619
4...

result:

ok 100 numbers

Test #8:

score: 0
Accepted
time: 84ms
memory: 366336kb

input:

1
1 5000
abaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

5023990000

result:

ok 1 number(s): "5023990000"

Test #9:

score: 0
Accepted
time: 128ms
memory: 264708kb

input:

1
100000 4817
acdcaaccca
cdbbbbabba
bcbccbcdbd
cdabaddaad
ddbdbabcac
dadadbbcba
aabcbbcabc
cdcadbbbda
dabacdbabd
dcbadbdabd
cdcbacbada
cadbbadbac
dbadbccdcd
babaddcdca
aaaddacccc
dabdcdadbb
abbbadbdaa
bcdbbacdaa
bcbcbddadb
bddaccbaba
baddadaaac
adbadbdaaa
cacbbbcdbc
abccdcacdb
abaacddbbc
acbbbcbcdc
...

output:

618356

result:

ok 1 number(s): "618356"

Test #10:

score: 0
Accepted
time: 52ms
memory: 150976kb

input:

10
3901 4952
srsofqyvrt
tazndzviuq
jcomfoxkiw
huzfqiecss
hhdtqpaohy
qcrokphbtf
xzkxssibix
hokmdpzydu
jreaeulsjt
vmxdsazajq
jawyofqbck
cwmzupygdm
rgsahqyxqt
kckgorfgvi
kcawvezmyb
mpcyhdifgn
xiwtmwttmp
mzknfqoifl
vbpvvdnqpy
anpdgjnbew
scekjovqpr
lzhfocjvld
oswjfhjdby
pmdbjddkpv
yjbnjcdtmc
gmpjgtpksa
r...

output:

904
208656
4560
30873
28272
5377
1326
93956
93720
282610

result:

ok 10 numbers