QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#114241 | #6635. Strange Keyboard | jeffqi | TL | 267ms | 4680kb | C++23 | 1.9kb | 2023-06-21 17:26:54 | 2023-06-21 17:26:55 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define vi vector<int>
#define vll vector<ll>
#define eb emplace_back
#define pb push_back
#define all(v) v.begin(),v.end()
#define sz(v) ((int)v.size())
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fi first
#define se second
#define umap unordered_map
using namespace std;
namespace qiqi {
const int inf = 1e9;
struct Trie {
struct Node {
vi ch; int k;
Node() {
ch.assign(26,-1);
k = inf;
}
};
vector<Node> a;
Trie() {
a.eb();
}
int ins(int x,int c,int k) {
if (a[x].ch[c] == -1) {
a[x].ch[c] = sz(a);
a.eb();
}
x = a[x].ch[c];
a[x].k = min(a[x].k,k);
return x;
}
};
void main() {
int n,m;
cin >> n >> m;
vi c(m,inf); c[0] = 0;
vector<string> s(n);
for (int i = 0; i < n; i++) {
cin >> s[i]; int k = sz(s[i]);
c[k%m] = min(c[k%m],k/m+1);
}
for (int i = 1; i < m; i++) {
if (c[i] != inf) {
int g = __gcd(i,m);
for (int j = 0; j < g; j++) {
for (int k = 0,x = i,y = (x+i)%m; k < 2*m/g; k++,x = y,y = (y+i)%m) {
c[y] = min(c[y],c[x]+c[i]+(y < x));
}
}
}
}
Trie trie;
for (int i = 0; i < n; i++) {
int x = sz(s[i]),p = 0;
for (int j = 0; j < x; j++) {
int y = c[(m-(x-j-1)%m)%m]+(x-j-1+m-1)/m+1;
if (y != inf) {
p = trie.ins(p,s[i][j]-'a',y);
}
}
}
string t; cin >> t;
n = sz(t);
vi f(n+1,inf); f[0] = 0;
for (int i = 0; i < n; i++) {
if (f[i] != inf) {
int p = 0;
for (int j = i; j < n; j++) {
p = trie.a[p].ch[t[j]-'a'];
if (p == -1) {
break;
}
f[j+1] = min(f[j+1],f[i]+trie.a[p].k);
}
}
}
if (f[n] == inf) {
cout << "-1\n";
return;
}
cout << f[n] << '\n';
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T; cin >> T;
while (T--) qiqi::main();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3516kb
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: 267ms
memory: 4680kb
input:
1 1413 4867 yumnkkghwtqnhpmmsbfwypcwudihegsvtdaykueuqozagpgdnhbwzoqrqswyctzonwmjdjmmpyzbyihhpyznmqltcuckqmcybbdafoexqztxhzlfjowznisnxtgoiqjztogldyegajmegalqgawrzotuntocmsagvkzdnwhmaewuxiuhvintpzwyrkgapnmegpveyuuorrfvrfakjyetfaoqtvwghlslsvmahrkvzkwpaviufknrpuapicqdnn yumnkkghwtqnhpmmsbfwypcwudihegsvt...
output:
10
result:
ok 1 number(s): "10"
Test #3:
score: -100
Time Limit Exceeded
input:
10 446 4905 afigcjhcgagabbiccehjcjajigghgbjjadccicghggijjdfeciaccgheedjdhgfjdfdbgidbbdjaiehhceeehchhabhaideggjbjajgfgicfdggahhbjgdebccbgbiedhehaebdccdfdffaacjcfbgjeegbahhbgcdjigijajheidchbddicehhhjbeiaajgedhdcjiefdgdbjjfaegheeidieheecaicciaajbabiidcecefgiicccdidegeica afigcjhcgagabbiccehjcjajigghgbj...