QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#209848 | #6635. Strange Keyboard | lefy | WA | 65ms | 77852kb | C++14 | 1.7kb | 2023-10-10 18:27:11 | 2023-10-10 18:27:12 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int inf=1e9;
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;
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: 3ms
memory: 43180kb
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: 21ms
memory: 44208kb
input:
1 1413 4867 yumnkkghwtqnhpmmsbfwypcwudihegsvtdaykueuqozagpgdnhbwzoqrqswyctzonwmjdjmmpyzbyihhpyznmqltcuckqmcybbdafoexqztxhzlfjowznisnxtgoiqjztogldyegajmegalqgawrzotuntocmsagvkzdnwhmaewuxiuhvintpzwyrkgapnmegpveyuuorrfvrfakjyetfaoqtvwghlslsvmahrkvzkwpaviufknrpuapicqdnn yumnkkghwtqnhpmmsbfwypcwudihegsvt...
output:
10
result:
ok 1 number(s): "10"
Test #3:
score: 0
Accepted
time: 31ms
memory: 43360kb
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: 65ms
memory: 43240kb
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: 61ms
memory: 77852kb
input:
1 7 4864 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
205
result:
ok 1 number(s): "205"
Test #6:
score: -100
Wrong Answer
time: 32ms
memory: 71200kb
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'