QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#209795#6635. Strange KeyboardlefyWA 73ms78064kbC++141.8kb2023-10-10 17:29:212023-10-10 17:29:21

Judging History

你现在查看的是最新测评结果

  • [2023-10-10 17:29:21]
  • 评测
  • 测评结果:WA
  • 用时:73ms
  • 内存:78064kb
  • [2023-10-10 17:29:21]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int inf=1e9;
string s[1000010];
char t[5010];
int len[1000010],f[1000010],now,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));
    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;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 6ms
memory: 43468kb

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: 44224kb

input:

1
1413 4867
yumnkkghwtqnhpmmsbfwypcwudihegsvtdaykueuqozagpgdnhbwzoqrqswyctzonwmjdjmmpyzbyihhpyznmqltcuckqmcybbdafoexqztxhzlfjowznisnxtgoiqjztogldyegajmegalqgawrzotuntocmsagvkzdnwhmaewuxiuhvintpzwyrkgapnmegpveyuuorrfvrfakjyetfaoqtvwghlslsvmahrkvzkwpaviufknrpuapicqdnn
yumnkkghwtqnhpmmsbfwypcwudihegsvt...

output:

10

result:

ok 1 number(s): "10"

Test #3:

score: 0
Accepted
time: 33ms
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: 71ms
memory: 43216kb

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: 73ms
memory: 78064kb

input:

1
7 4864
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

205

result:

ok 1 number(s): "205"

Test #6:

score: -100
Wrong Answer
time: 35ms
memory: 71896kb

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'