QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#211633 | #6635. Strange Keyboard | kkio | WA | 397ms | 207376kb | C++20 | 2.6kb | 2023-10-12 19:36:42 | 2023-10-12 19:36:42 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int maxk=5005,maxn=1e6+10,inf=1e18;
int ch[maxn][26],tot,n,K;
vector<int> leaf[maxn];
string s[maxn],T;
vector<int> Len;
bool vis[maxk];
typedef long long ll;
ll dis[maxk],val[maxn];
int dep[maxn];
inline int getpre(int x)
{
if(~val[x])return val[x];
val[x]=inf;
for(int v:leaf[x])
{
int len=dep[v]-dep[x];
val[x]=min(val[x],dis[K-len%K]+(len+K-1)/K+1);
}
return val[x];
}
ll dp[maxn];
void solve()
{
cin>>n>>K;
Len.clear();
for(int i=1;i<=n;i++)
cin>>s[i],Len.push_back(s[i].size());
cin>>T;
sort(Len.begin(),Len.end());
Len.resize(unique(Len.begin(),Len.end())-Len.begin());
memset(dis,0x3f,sizeof dis);
dis[K]=0;
dis[0]=0;
for(int l:Len)
{
if(l==0)continue;
for(int i=0;i<K;i++)vis[i]=0;
for(int i=0;i<K;i++)
if(!vis[i])
{
vector<int> cy;
int now=i;
int z=l%K;
do{
cy.push_back(now);
vis[now]=1;
now+=z;while(now>=K)now-=K;
}while(now!=i);
int len=cy.size();
for(int i=0;i<len;i++)cy.push_back(cy[i]);
for(int i=0;i<cy.size()-1;i++)dis[cy[i+1]]=min(dis[cy[i+1]],dis[cy[i]]+(cy[i]+l>=K?(cy[i]+l-cy[i+1])/K+1:1));
}
}
tot=0;memset(ch[0],0,sizeof ch[0]);
for(int i=1;i<=n;i++)
{
int now=0;
vector<int> path;
for(int j=0;j<s[i].size();j++)
{
int c=s[i][j]-'a';
if(!ch[now][c])
{
++tot;ch[now][c]=tot;
dep[tot]=dep[now]+1;
memset(ch[tot],0,sizeof ch[tot]);
val[tot]=-1;
leaf[tot].clear();
}
now=ch[now][c];
path.push_back(now);
}
for(int v:path)leaf[v].push_back(now);
}
for(int i=1;i<=T.size();i++)dp[i]=inf;
for(int i=0;i<T.size();i++)
{
int now=0;
for(int j=i;j<T.size();j++)
{
int c=T[j]-'a';
if(!ch[now][c])break;
now=ch[now][c];
dp[j+1]=min(dp[j+1],dp[i]+getpre(now));
}
}
if(dp[(int)T.size()]!=inf)printf("%lld\n",dp[(int)T.size()]);
else puts("-1");
}
int main()
{
//freopen("data.in","r",stdin);
//freopen("1.out","w",stdout);
int T;scanf("%d",&T);
while(T--)solve();
return 0;
}
/*
1
5 4
cabd
bcca
ddab
dabd
cba
cbc
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 4ms
memory: 63128kb
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: 68ms
memory: 70624kb
input:
1 1413 4867 yumnkkghwtqnhpmmsbfwypcwudihegsvtdaykueuqozagpgdnhbwzoqrqswyctzonwmjdjmmpyzbyihhpyznmqltcuckqmcybbdafoexqztxhzlfjowznisnxtgoiqjztogldyegajmegalqgawrzotuntocmsagvkzdnwhmaewuxiuhvintpzwyrkgapnmegpveyuuorrfvrfakjyetfaoqtvwghlslsvmahrkvzkwpaviufknrpuapicqdnn yumnkkghwtqnhpmmsbfwypcwudihegsvt...
output:
10
result:
ok 1 number(s): "10"
Test #3:
score: 0
Accepted
time: 148ms
memory: 61996kb
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: 397ms
memory: 61260kb
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: 91ms
memory: 107364kb
input:
1 7 4864 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
205
result:
ok 1 number(s): "205"
Test #6:
score: 0
Accepted
time: 63ms
memory: 97524kb
input:
10 7 4923 baaabbabbbabbbbbabaabaabababbabbaaaabbaaabbbbabbaaababaaaaabaaabbabbbabbaaaabbaabbbaabbaaababaaababbabaababbaababbabaaabaabbaaabaaaababbabbabaabaabaabaabbbbaabaabbbaababbabbabaaaabbbaabbbaaaabaaaaababbaabaaaaababbbbaaaabbbaababbaabbabaabaaaaababaababaabaaaaabababababbabbbabababaaabaababbaa...
output:
4287 228 3671 549 129 372 475 534 336 288
result:
ok 10 numbers
Test #7:
score: 0
Accepted
time: 104ms
memory: 71284kb
input:
100 7 4807 abbababaababbaabbabbaabaababbaaababaabaaabbaaaabababbbaabbaaabababbaabaabbaaaaabbbbaabbbaaabbbbabaabbbaaaaabbbaabbaaaabbaaababbaaabbbbabaabbababababbbabaaabaaaabbbbabbabbbbbaabaaabaababbabaaabbaabbabbabaaababbbabbabbbaababaabaaaabaaabbbbabbaabaababbbabbbbaaaabbabbbaabbaabbbbb aaaaababaaab...
output:
45 32 11 4 2475 132 50 330 20 6 99 25 126 6 4 14 74 108 208 11 5 67 166 2822 178 1307 548 92 386 493 279 2415 255 262 567 215 46 113 31 651 17 4 8 21 12 100 69 152 15 55 521 146 11 13 181 -1 442 1839 2 5 31 26 122 696 280 77 1 839 11 273 7 178 421 228 6 6 82 116 1 -1 293 519 5 160 15 126 13 31 619 4...
result:
ok 100 numbers
Test #8:
score: -100
Wrong Answer
time: 40ms
memory: 207376kb
input:
1 1 5000 abaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
-1
result:
wrong answer 1st numbers differ - expected: '5023990000', found: '-1'