QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#209530 | #6635. Strange Keyboard | c20230201 | WA | 1069ms | 137176kb | C++14 | 1.8kb | 2023-10-10 15:34:01 | 2023-10-10 15:34:01 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=1e6+5, inf=1e18;
ll tot,k;
ll p[maxn][26], ed[maxn], dis[5005], dep[maxn], val[maxn], dp[5005];
map<ll,ll> E;
vector<ll> vec[maxn];
void insert(string s) {
ll pos=0;
for(ll i=0;i<s.size();i++) {
ll v=s[i]-'a';
if(!p[pos][v]) p[pos][v]=++tot;
pos=p[pos][v];
}
ed[pos]=1;
return ;
}
void dfs(ll x) {
if(ed[x]) vec[x].push_back(x);
val[x]=inf;
for(ll i=0;i<26;i++) {
if(!p[x][i]) continue;
dep[p[x][i]]=dep[x]+1;
dfs(p[x][i]);
for(auto p:vec[p[x][i]]) vec[x].push_back(p);
}
for(auto p:vec[x]) val[x]=min(val[x],(dep[p]-dep[x]+k-1)/k+dis[((dep[x]-dep[p])%k+k)%k]);
return ;
}
void solve() {
ll n; cin>>n>>k;
for(ll i=0;i<=tot;i++){
vec[i].clear();
ed[i]=0;
for(ll j=0;j<26;j++) p[i][j]=0;
}
tot=0;
E.clear();
for(ll i=0;i<k;i++) dis[i]=inf;
dis[0]=0;
for(ll i=1;i<=n;i++) {
string s; cin>>s;
insert(s);
if(E.find(s.size()%k)==E.end()||E[s.size()%k]>s.size()) E[s.size()%k]=s.size();
}
for(auto p:E) {
ll y=p.first, w=p.second;
if(!y) continue;
ll d=__gcd(k,y);
for(ll v=0;v<d;v++) {
ll mn=v;
for(ll x=(v+y)%k;x!=v;x=(x+y)%k) if(dis[v]>dis[x]) v=x;
for(ll x=(mn+y)%k;x!=mn;x=(x+y)%k) dis[x]=min(dis[x],dis[(x-y+k)%k]+((x-y+k)%k+w)/k+1);
}
}
dfs(0);
string t; cin>>t;
for(ll i=0;i<=t.size();i++) dp[i]=inf;
dp[0]=0;
for(ll i=0;i<t.size();i++) {
ll x=0;
for(ll j=i+1;j<=t.size();j++) {
if(p[x][t[j-1]-'a']) x=p[x][t[j-1]-'a'];
else break;
dp[j]=min(dp[j],dp[i]+val[x]+1);
}
}
if(dp[t.size()]!=inf) cout<<dp[t.size()]<<'\n';
else cout<<"-1\n";
return ;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
ll q; cin>>q;
while(q--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 5ms
memory: 32436kb
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: 130ms
memory: 42428kb
input:
1 1413 4867 yumnkkghwtqnhpmmsbfwypcwudihegsvtdaykueuqozagpgdnhbwzoqrqswyctzonwmjdjmmpyzbyihhpyznmqltcuckqmcybbdafoexqztxhzlfjowznisnxtgoiqjztogldyegajmegalqgawrzotuntocmsagvkzdnwhmaewuxiuhvintpzwyrkgapnmegpveyuuorrfvrfakjyetfaoqtvwghlslsvmahrkvzkwpaviufknrpuapicqdnn yumnkkghwtqnhpmmsbfwypcwudihegsvt...
output:
10
result:
ok 1 number(s): "10"
Test #3:
score: 0
Accepted
time: 351ms
memory: 32520kb
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: 1069ms
memory: 31460kb
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: 113ms
memory: 137176kb
input:
1 7 4864 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
205
result:
ok 1 number(s): "205"
Test #6:
score: -100
Wrong Answer
time: 166ms
memory: 107588kb
input:
10 7 4923 baaabbabbbabbbbbabaabaabababbabbaaaabbaaabbbbabbaaababaaaaabaaabbabbbabbaaaabbaabbbaabbaaababaaababbabaababbaababbabaaabaabbaaabaaaababbabbabaabaabaabaabbbbaabaabbbaababbabbabaaaabbbaabbbaaaabaaaaababbaabaaaaababbbbaaaabbbaababbaabbabaabaaaaababaababaabaaaaabababababbabbbabababaaabaababbaa...
output:
4287 240 3671 549 129 412 475 534 338 288
result:
wrong answer 2nd numbers differ - expected: '228', found: '240'