QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#210947 | #6635. Strange Keyboard | zyxawa | WA | 319ms | 143536kb | C++14 | 1.7kb | 2023-10-11 21:50:07 | 2023-10-11 21:50:09 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
int p,n,m,k,tot=1000000,t[1000001][26],vis[5001];
long long dis[5001],dp[5001],val[1000001];
vector <int> str[1000001];
char s[1000001];
map <int,int> len;
priority_queue <pair<long long,int>,vector<pair<long long,int> >,greater<pair<long long,int> > > q;
void insert(int d){
int p=0,cnt=str[d].size(),now=0;
for(auto i:str[d]){
if(!t[p][i]) t[p][i]=++tot;
p=t[p][i],now++,val[p]=min(val[p],(cnt-now)/k+dis[(k-(cnt-now)%k)%k]+1);
// cout<<now<<" "<<(cnt-now)/k<<" "<<(cnt-now)%k<<" "<<(cnt-now)/k+dis[(k-(cnt-now)%k)%k]+1<<endl;
}
// val[p]=0;
}
int main(){
scanf("%d",&p);
while(p--){
scanf("%d%d",&n,&k),len.clear();
for(int i=1;i<=n;i++) str[i].clear();
for(int i=0;i<=tot;i++) val[i]=1e16,memset(t[i],0,sizeof(t[i]));
for(int i=0;i<=5000;i++) dp[i]=dis[i]=1e16,tot=vis[i]=0;
for(int i=1;i<=n;i++){
scanf("%s",s+1),m=strlen(s+1);
if(!len.count(m%k)) len[m%k]=m/k+1;
else len[m%k]=min(len[m%k],m/k+1);
for(int j=1;j<=m;j++) str[i].push_back(s[j]-97);
}
scanf("%s",s+1),m=strlen(s+1),dp[0]=dis[0]=0;
q.push({0,0});
while(!q.empty()){
int x=q.top().second;
q.pop();
if(vis[x]) continue;
vis[x]=1;
for(auto i:len){
int y=(x+i.first)%k,w=(x+i.first>=k);
if(dis[y]>dis[x]+i.second+w) dis[y]=dis[x]+i.second+w,q.push({dis[y],y});
}
}
for(int i=1;i<=n;i++) insert(i);
for(int i=0;i<m;i++){
for(int j=i+1,p=0;j<=m;j++){
if(!t[p][s[j]-97]) break;
p=t[p][s[j]-97];
// cout<<val[p]<<endl;
dp[j]=min(dp[j],dp[i]+val[p]);
}
}
if(dp[m]>=1e16) printf("-1\n");
else printf("%lld\n",dp[m]);
}
return 0;
}
/*
abc abc defgh
2
2 3
defgh
abc
abcde
1 1
a
b
*/
詳細信息
Test #1:
score: 100
Accepted
time: 19ms
memory: 136780kb
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: 81ms
memory: 143536kb
input:
1 1413 4867 yumnkkghwtqnhpmmsbfwypcwudihegsvtdaykueuqozagpgdnhbwzoqrqswyctzonwmjdjmmpyzbyihhpyznmqltcuckqmcybbdafoexqztxhzlfjowznisnxtgoiqjztogldyegajmegalqgawrzotuntocmsagvkzdnwhmaewuxiuhvintpzwyrkgapnmegpveyuuorrfvrfakjyetfaoqtvwghlslsvmahrkvzkwpaviufknrpuapicqdnn yumnkkghwtqnhpmmsbfwypcwudihegsvt...
output:
10
result:
ok 1 number(s): "10"
Test #3:
score: 0
Accepted
time: 147ms
memory: 137664kb
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: 319ms
memory: 136712kb
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: 63ms
memory: 141580kb
input:
1 7 4864 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
200
result:
wrong answer 1st numbers differ - expected: '205', found: '200'