QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#209852 | #6635. Strange Keyboard | lefy | WA | 73ms | 77952kb | C++14 | 1.8kb | 2023-10-10 18:30:53 | 2023-10-10 18:30:53 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f3f;
string s[1000010];
char t[5010];
int len[1000010],f[1000010],tot,nxt[1000010][30],cost[1000010];
void insert(int id){
int n=s[id].size()-1,now=0;
for(int i=1;i<=n;i++){
int ch=s[id][i]-'a'+1;
if(!nxt[now][ch])nxt[now][ch]=++tot;
now=nxt[now][ch];
cost[now]=min(cost[now],f[n-i]);
}
}
int dp[5010];
void solve(){
int n,K,m=0;
for(int i=1;i<=tot;cost[i]=inf,++i)for(int j=1;j<=26;j++)nxt[i][j]=0;
memset(cost,0x3f,sizeof(cost));
// memset(nxt,0,sizeof(nxt));
// memset(f,0x3f,sizeof(f));
tot=0;
scanf("%d%d",&n,&K);
for(int i=1;i<=n;i++){
cin>>s[i];s[i]=" "+s[i];
len[++m]=s[i].size()-1;
}
sort(len+1,len+1+m);m=unique(len+1,len+1+m)-len-1;
queue<int>q;q.push(0);
for(int i=1;i<=len[m]+K;i++)f[i]=inf;
f[0]=1;
while(!q.empty()){
int x=q.front();q.pop();
// cout<<x<<" "<<f[x]<<"\n";
//a>=k
if(x+K<=len[m]+K&&f[x+K]==inf)f[x+K]=f[x]+1,q.push(x+K);
//a<k
int p=lower_bound(len+1,len+1+m,x)-len-1;
for(int i=p;i>=1&&x-len[i]<K;i--)if(f[x-len[i]]==inf){
f[x-len[i]]=f[x]+1;q.push(x-len[i]);
}
}
for(int i=1;i<=n;i++)insert(i);
scanf("%s",t+1);
m=strlen(t+1);
for(int i=1;i<=m;i++)dp[i]=inf;dp[0]=0;
for(int i=0;i<m;i++)if(dp[i]!=inf){
int now=0;
for(int j=i+1;j<=m;j++){
int ch=t[j]-'a'+1;
if(!nxt[now][ch])break;
now=nxt[now][ch];
dp[j]=min(dp[j],dp[i]+cost[now]);
}
}
if(dp[m]==inf)printf("-1\n");
else printf("%d\n",dp[m]);
}
int main() {
// for(int i=1;i<=1e6;i++)cost[i]=inf;
int t;scanf("%d",&t);
while(t--)solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 4ms
memory: 43164kb
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: 23ms
memory: 44220kb
input:
1 1413 4867 yumnkkghwtqnhpmmsbfwypcwudihegsvtdaykueuqozagpgdnhbwzoqrqswyctzonwmjdjmmpyzbyihhpyznmqltcuckqmcybbdafoexqztxhzlfjowznisnxtgoiqjztogldyegajmegalqgawrzotuntocmsagvkzdnwhmaewuxiuhvintpzwyrkgapnmegpveyuuorrfvrfakjyetfaoqtvwghlslsvmahrkvzkwpaviufknrpuapicqdnn yumnkkghwtqnhpmmsbfwypcwudihegsvt...
output:
10
result:
ok 1 number(s): "10"
Test #3:
score: 0
Accepted
time: 42ms
memory: 43644kb
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: 73ms
memory: 43508kb
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: 48ms
memory: 77952kb
input:
1 7 4864 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
205
result:
ok 1 number(s): "205"
Test #6:
score: -100
Wrong Answer
time: 42ms
memory: 74008kb
input:
10 7 4923 baaabbabbbabbbbbabaabaabababbabbaaaabbaaabbbbabbaaababaaaaabaaabbabbbabbaaaabbaabbbaabbaaababaaababbabaababbaababbabaaabaabbaaabaaaababbabbabaabaabaabaabbbbaabaabbbaababbabbabaaaabbbaabbbaaaabaaaaababbaabaaaaababbbbaaaabbbaababbaabbabaabaaaaababaababaabaaaaabababababbabbbabababaaabaababbaa...
output:
4287 182 2489 509 104 354 218 185 311 234
result:
wrong answer 2nd numbers differ - expected: '228', found: '182'