QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#190186#6635. Strange KeyboardqzezTL 143ms42236kbC++142.2kb2023-09-28 14:29:442023-09-28 14:29:44

Judging History

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

  • [2023-09-28 14:29:44]
  • 评测
  • 测评结果:TL
  • 用时:143ms
  • 内存:42236kb
  • [2023-09-28 14:29:44]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
template<typename T>
ostream& operator << (ostream &out,const vector<T>&x){
	if(x.empty())return out<<"[]";
	out<<'['<<x[0];
	for(int len=x.size(),i=1;i<len;i++)out<<','<<x[i];
	return out<<']';
}
template<typename T>
vector<T> ary(const T *a,int l,int r){
	return vector<T>{a+l,a+1+r};
}
template<typename T>
void debug(T x){
	cerr<<x<<'\n';
}
template<typename T,typename ...S>
void debug(T x,S ...y){
	cerr<<x<<' ',debug(y...);
}
const int N=1e6+10,M=5e3+10,V=26;
const ll INF=1e18;
int T,n,m,d;
int k,ch[N][V],ed[N],fa[N];
char a[N],b[M];
ll f[M],g[M],h[N],dp[M];
int insert(int x){
	int u=1,n=strlen(a);
	f[n%d]=min(f[n%d],n/d+1ll);
	for(int i=0;i<n;i++){
		int &v=ch[u][a[i]-'a'];
		if(!v)v=++k,fa[v]=u;
		u=v;
	}
	return u;
}
int vis[M];
void dij(){
	vector<int>p;
	for(int i=0;i<d;i++){
		vis[i]=0;
		if(f[i]<INF)p.push_back(i);
	}
	priority_queue<pair<int,int> >q;
	for(int x:p)q.push({-f[x],x});
	for(int u;!q.empty();){
		u=q.top().second,q.pop();
		if(vis[u])continue;
		vis[u]=1;
		for(int x:p){
			int v=(u+x)%d;
			ll val=f[u]+f[x]+(u+x>=d);
			if(f[v]>val){
				f[v]=val,q.push({-f[v],v});
			}
		}
	}
	g[0]=0;
	for(int i=1;i<d;i++)g[i]=min(f[d-i]+1,INF);
}
void get(){
	scanf("%d%d",&n,&d),k=1;
	fill(f,f+d,INF),f[0]=0;
	for(int i=1;i<=n;i++){
		scanf("%s",a),ed[i]=insert(i);
	}
	dij();
	// debug(ary(g,0,d-1));
	fill(h+1,h+1+k,INF);
	for(int i=1;i<=n;i++){
		for(int x=ed[i],cnt=0;x>1;x=fa[x],cnt++){
			// if(x==3)debug("x=3",cnt/d+g[cnt%d]+1);
			h[x]=min(h[x],cnt/d+g[cnt%d]+1);
		}
	}
	// for(int i=1;i<=k;i++){
	// 	debug("ch",i,ary(ch[i],0,V-1));
	// }
	scanf("%s",b+1),m=strlen(b+1);
	fill(dp,dp+1+m,INF),dp[0]=0;
	for(int i=1;i<=m;i++){
		int u=1;
		for(int j=i;j<=m;j++){
			u=ch[u][b[j]-'a'];
			if(!u)break;
			// if(i==1&&j==3)debug("ok1",u,h[u]);
			// if(i==4&&j==5)debug("ok2",u,h[u]);
			dp[j]=min(dp[j],dp[i-1]+h[u]);
		}
	}
	// debug("dp",ary(dp,0,m));
	if(dp[m]<INF)printf("%lld\n",dp[m]);
	else puts("-1");
}
void clr(){
	for(;k;k--){
		memset(ch[k],0,sizeof ch[k]);
		fa[k]=0;
	}
}
int main(){
	for(scanf("%d",&T);T--;)get();
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 7792kb

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: 22ms
memory: 10092kb

input:

1
1413 4867
yumnkkghwtqnhpmmsbfwypcwudihegsvtdaykueuqozagpgdnhbwzoqrqswyctzonwmjdjmmpyzbyihhpyznmqltcuckqmcybbdafoexqztxhzlfjowznisnxtgoiqjztogldyegajmegalqgawrzotuntocmsagvkzdnwhmaewuxiuhvintpzwyrkgapnmegpveyuuorrfvrfakjyetfaoqtvwghlslsvmahrkvzkwpaviufknrpuapicqdnn
yumnkkghwtqnhpmmsbfwypcwudihegsvt...

output:

10

result:

ok 1 number(s): "10"

Test #3:

score: 0
Accepted
time: 41ms
memory: 10000kb

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: 143ms
memory: 9884kb

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: 49ms
memory: 42236kb

input:

1
7 4864
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

205

result:

ok 1 number(s): "205"

Test #6:

score: -100
Time Limit Exceeded

input:

10
7 4923
baaabbabbbabbbbbabaabaabababbabbaaaabbaaabbbbabbaaababaaaaabaaabbabbbabbaaaabbaabbbaabbaaababaaababbabaababbaababbabaaabaabbaaabaaaababbabbabaabaabaabaabbbbaabaabbbaababbabbabaaaabbbaabbbaaaabaaaaababbaabaaaaababbbbaaaabbbaababbaabbabaabaaaaababaababaabaaaaabababababbabbbabababaaabaababbaa...

output:


result: