QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#209367 | #6635. Strange Keyboard | c20230201 | WA | 493ms | 135672kb | C++14 | 1.7kb | 2023-10-10 14:34:43 | 2023-10-10 14:34:43 |
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+dis[(dep[p]-dep[x])%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 mn=0;
for(ll x=y;x;x=(x+y)%k) dis[x]=min(dis[x],dis[(x-y+k)%k]+((x-y+k)%k+w)/k);
}
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: 4ms
memory: 31604kb
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: 82ms
memory: 41840kb
input:
1 1413 4867 yumnkkghwtqnhpmmsbfwypcwudihegsvtdaykueuqozagpgdnhbwzoqrqswyctzonwmjdjmmpyzbyihhpyznmqltcuckqmcybbdafoexqztxhzlfjowznisnxtgoiqjztogldyegajmegalqgawrzotuntocmsagvkzdnwhmaewuxiuhvintpzwyrkgapnmegpveyuuorrfvrfakjyetfaoqtvwghlslsvmahrkvzkwpaviufknrpuapicqdnn yumnkkghwtqnhpmmsbfwypcwudihegsvt...
output:
10
result:
ok 1 number(s): "10"
Test #3:
score: 0
Accepted
time: 160ms
memory: 34096kb
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: 493ms
memory: 33244kb
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: 101ms
memory: 135672kb
input:
1 7 4864 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
132
result:
wrong answer 1st numbers differ - expected: '205', found: '132'