QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#190309 | #6635. Strange Keyboard | ucup-team870 | TL | 537ms | 243496kb | C++17 | 1.9kb | 2023-09-28 17:00:42 | 2023-09-28 17:00:42 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define IOS {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);}
#define rep(i,j,k) for(int i=j;i<=k;++i)
#define per(i,j,k) for(int i=j;i>=k;--i)
#define ll long long
#define vi vector<int>
#define db double
const ll mod=1231453023109121,INF=1e18;
const int N=5005,bs=131,M=1e6+6;
void Min(ll &x,ll y){x=min(x,y);}
// unordered_map<ll,vi>mp;
// unordered_map<ll,ll>res;
char s[M]; string S[M];
ll l[N],dis[N],dp[N][N],ans[N];
int nxt[M][26],cnt=1; ll res[M];
void slv(){
int n,k;cin>>n>>k;
// mp.clear(); res.clear();
rep(i,0,k)l[i]=INF;
rep(i,1,n){
cin>>S[i]; int len=S[i].length();
Min(l[len%k],k+len);
}
dis[0]=0; rep(i,1,k-1)dis[i]=INF;
vi vis(k+1);
while(1){
ll mi=INF; int x;
rep(i,0,k-1){
if(!vis[i] && dis[i]<mi){
mi=dis[i]; x=i;
}
}
if(mi==INF)break; vis[x]=1;
rep(i,0,k-1)Min(dis[i],dis[x]+l[(i-x+k)%k]);
}
rep(i,0,1e6+2)res[i]=INF;
rep(i,1,n){
int now=1,len=S[i].size();
rep(j,0,len-1){
int c=S[i][j]-'a';
if(!nxt[now][c])nxt[now][c]=++cnt;
now=nxt[now][c];
int le=len-1-j,x=((-le)%k+k)%k;
if(dis[x]<INF)Min(res[now],(dis[x]+le)/k+1);
}
}
cin>>s+1; int m=strlen(s+1); //下次用n吧..
rep(l,1,m)rep(r,l,m)dp[l][r]=INF;
rep(l,1,m){
int now=1;
rep(r,l,m){
now=nxt[now][s[r]-'a'];
if(!now)break;
dp[l][r]=res[now];
}
}
rep(i,0,cnt)memset(nxt[i],0,sizeof nxt[i]); cnt=1;
// cout<<dp[1][3]<<' '<<dp[4][5]<<'\n';
rep(i,1,m){
ans[i]=dp[1][i];
rep(j,1,i-1)Min(ans[i], ans[j]+dp[j+1][i]);
}
cout<<(ans[m]>=INF?-1:ans[m])<<'\n';
}
signed main(){
IOS
int T;cin>>T;
while(T--)slv();
}
/*
1
2 3
defgh
abc
abcde
*/
詳細信息
Test #1:
score: 100
Accepted
time: 4ms
memory: 47172kb
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: 108ms
memory: 243496kb
input:
1 1413 4867 yumnkkghwtqnhpmmsbfwypcwudihegsvtdaykueuqozagpgdnhbwzoqrqswyctzonwmjdjmmpyzbyihhpyznmqltcuckqmcybbdafoexqztxhzlfjowznisnxtgoiqjztogldyegajmegalqgawrzotuntocmsagvkzdnwhmaewuxiuhvintpzwyrkgapnmegpveyuuorrfvrfakjyetfaoqtvwghlslsvmahrkvzkwpaviufknrpuapicqdnn yumnkkghwtqnhpmmsbfwypcwudihegsvt...
output:
10
result:
ok 1 number(s): "10"
Test #3:
score: 0
Accepted
time: 537ms
memory: 110988kb
input:
10 446 4905 afigcjhcgagabbiccehjcjajigghgbjjadccicghggijjdfeciaccgheedjdhgfjdfdbgidbbdjaiehhceeehchhabhaideggjbjajgfgicfdggahhbjgdebccbgbiedhehaebdccdfdffaacjcfbgjeegbahhbgcdjigijajheidchbddicehhhjbeiaajgedhdcjiefdgdbjjfaegheeidieheecaicciaajbabiidcecefgiicccdidegeica afigcjhcgagabbiccehjcjajigghgbj...
output:
3 2 2 11 6 5 1 1 1 1
result:
ok 10 numbers
Test #4:
score: -100
Time Limit Exceeded
input:
100 140 4879 baabaababbababbaabababaaababbbabbbbbbabbababbbabbbbabbbbbbaabbbbbbbbabaabbbaabaabbbaabbabaabaabbbabbbababbbaabbabaaaaabbaaabbbabb baa baabaababbababbaabababaaababbbabbbbbbabbab baabaababbababbaabababaaabab baabaababbababbaabababaaababbbabbb baabaababbababbaabababaaababbbabbbbbbabbababbb...