QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#211583 | #6635. Strange Keyboard | kkio | WA | 83ms | 70660kb | C++20 | 2.6kb | 2023-10-12 18:47:33 | 2023-10-12 18:47:34 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int maxk=5005,maxn=1e6+10,inf=0x3f3f3f3f;
int ch[maxn][26],tot,val[maxn],n,K;
vector<int> leaf[maxn];
string s[maxn],T;
vector<int> Len;
bool vis[maxk];
int dis[maxk],dep[maxn];
inline int getpre(int x)
{
if(~val[x])return val[x]+1;
val[x]=inf;
for(int v:leaf[x])
{
int len=dep[v]-dep[x];
if(len%K)return val[x]=min(val[x],dis[K-len%K]+len/K+1);
else val[x]=min(val[x],len/K);
}
return val[x]+1;
}
int dp[maxn];
void solve()
{
cin>>n>>K;
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[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 v:cy)printf("%d ",v);putchar('\n');
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));
}
}
//for(int i=0;i<K;i++)printf("%d ",dis[i]);putchar('\n');
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];
//printf("%d %d\n",now,getpre(now));
dp[j+1]=min(dp[j+1],dp[i]+getpre(now));
}
}
if(dp[T.size()]!=inf)printf("%d\n",dp[T.size()]);
else puts("-1");
}
int main()
{
int T;scanf("%d",&T);
while(T--)solve();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 4ms
memory: 63516kb
input:
2 2 3 defgh abc abcde 1 1 a b
output:
3 -1
result:
ok 2 number(s): "3 -1"
Test #2:
score: -100
Wrong Answer
time: 83ms
memory: 70660kb
input:
1 1413 4867 yumnkkghwtqnhpmmsbfwypcwudihegsvtdaykueuqozagpgdnhbwzoqrqswyctzonwmjdjmmpyzbyihhpyznmqltcuckqmcybbdafoexqztxhzlfjowznisnxtgoiqjztogldyegajmegalqgawrzotuntocmsagvkzdnwhmaewuxiuhvintpzwyrkgapnmegpveyuuorrfvrfakjyetfaoqtvwghlslsvmahrkvzkwpaviufknrpuapicqdnn yumnkkghwtqnhpmmsbfwypcwudihegsvt...
output:
58
result:
wrong answer 1st numbers differ - expected: '10', found: '58'