QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#207122 | #6635. Strange Keyboard | Famiglistmo | WA | 266ms | 67388kb | C++14 | 2.2kb | 2023-10-08 09:41:52 | 2023-10-08 09:41:52 |
Judging History
answer
#include<bits/stdc++.h>
#define pii pair<int, int>
#define fi first
#define se second
using namespace std;
typedef long long ll;
const int N = 1e6 + 6;
const int K = 5005;
string s[N], t;
map<int, int> cost;
int ch[N][26], dis[K], mn[N];
int T, n, k, m, siz;
ll f[K];
void insert(string s) {
int p = 1;
for(auto c : s) {
int &to = ch[p][c - 'a'];
if(!to) to = ++ siz;
p = to;
}
}
inline int add(int a, int b) { return a + b >= k ? a + b - k : a + b; }
void solve() {
for(int i = 1; i <= siz; ++ i)
memset(ch[i], 0, sizeof(ch[i]));
siz = 1, cost.clear();
cin >> n >> k;
for(int i = 1; i <= n; ++ i) {
cin >> s[i], insert(s[i]);
int len = s[i].size(), l = len % k;
if(l == 0) continue;
if(cost.find(l) == cost.end())
cost[l] = len / k + 1;
else cost[l] = min(cost[l], len / k + 1);
}
cin >> t, m = t.size();
memset(dis, 127, (k + 5) << 2), dis[0] = 0;
for(auto edge : cost)
for(int j = 0, lim = __gcd(k, edge.fi); j < lim; ++ j)
for(int cur = j, cnt = 0, nex; cnt < 2; cnt += cur == j)
nex = add(cur, edge.fi), dis[nex] = min(dis[nex], dis[cur] + edge.se + (cur > nex)), cur = nex;
// for(int i = 0; i < k; ++ i)
// cout << dis[i] << " "; cout << endl;
memset(mn, 127, (siz + 5) << 2);
for(int i = 1; i <= n; ++ i) {
int p = 1, len = s[i].size();
for(int j = 0; j < len; ++ j) {
p = ch[p][s[i][j] - 'a'];
int rst = len - j - 1;
mn[p] = min(mn[p], rst / k + dis[rst % k]);
}
}
// for(int i = 1; i <= siz; ++ i)
// cout << mn[i] << " " ; cout << endl;
memset(f, 127, (m + 5) << 3), f[0] = 0;
for(int i = 0; i <= m; ++ i)
for(int j = i + 1, p = 1; j <= m; ++ j) {
p = ch[p][t[j - 1] - 'a'];
if(!p) break;
if(!(mn[p] >> 29))
f[j] = min(f[j], f[i] + mn[p] + 1);
}
if(f[m] >> 60) cout << "-1\n";
else cout << f[m] << '\n';
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> T;
while(T --) solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 36776kb
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: 39ms
memory: 36856kb
input:
1 1413 4867 yumnkkghwtqnhpmmsbfwypcwudihegsvtdaykueuqozagpgdnhbwzoqrqswyctzonwmjdjmmpyzbyihhpyznmqltcuckqmcybbdafoexqztxhzlfjowznisnxtgoiqjztogldyegajmegalqgawrzotuntocmsagvkzdnwhmaewuxiuhvintpzwyrkgapnmegpveyuuorrfvrfakjyetfaoqtvwghlslsvmahrkvzkwpaviufknrpuapicqdnn yumnkkghwtqnhpmmsbfwypcwudihegsvt...
output:
10
result:
ok 1 number(s): "10"
Test #3:
score: 0
Accepted
time: 93ms
memory: 36396kb
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: 266ms
memory: 36608kb
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: -100
Wrong Answer
time: 57ms
memory: 67388kb
input:
1 7 4864 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
135
result:
wrong answer 1st numbers differ - expected: '205', found: '135'