QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#135465#6635. Strange KeyboardPlentyOfPenaltyAC ✓146ms248056kbC++201.9kb2023-08-05 15:48:502023-08-05 15:48:51

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-08-05 15:48:51]
  • 评测
  • 测评结果:AC
  • 用时:146ms
  • 内存:248056kb
  • [2023-08-05 15:48:50]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=1e6,MK=5000;
const long long INF=1e18;
int Q,n,K,m,len[N+10],c[N+10][26],vis[N+10][26],cnt,TIM,tmp;
long long mn[MK+10],g[MK+10],cst[N+10],dp[MK+10];
string s[N+10],T;
void Ins(int x,int id,int nw){
	//cout<<"INS "<<x<<" "<<id<<","<<nw<<"\n";
	cst[x]=min(cst[x],g[K-(len[id]-nw)%K]+1+1+(len[id]-nw)/K);
	//cout<<"CST="<<cst[x]<<"\n";
	if(nw==len[id])return;
	int t=s[id][nw]-'a';
	if(vis[x][t]!=TIM)vis[x][t]=TIM,c[x][t]=++cnt,cst[cnt]=INF;
	Ins(c[x][t],id,nw+1);
}
int main(){
#ifdef memset0
	freopen("E-big.in","r",stdin);
#endif
	ios::sync_with_stdio(0),cin.tie(0);
	cin>>Q;
	while(Q--){
		cin>>n>>K;
		for(int i=1;i<K;++i)mn[i]=g[i]=INF;
		//cin>>s[0];
		for(int i=1;i<=n;++i){
			cin>>s[i];
			//s[i]=s[0];
			len[i]=s[i].size();
			mn[len[i]%K]=min(mn[len[i]%K],1ll+len[i]/K);
		}
		fill(g,g+K,INF);
		g[K]=-1;
		g[0]=0;
		vector<pair<int,long long>> e;
		for(int i=1;i<K;i++){
			if(mn[i]!=INF){
				e.emplace_back(i,mn[i]);
			}
		}
		priority_queue<pair<long long,int>> q;
		q.push({0,0});
		while(q.size()){
			auto [d,u]=q.top();
			q.pop();
			if(g[u]+d)continue;
			for(const auto &[t,w]:e){
				int v=(u+t)%K;
				// fprintf(stderr,"%d -> %d %lld\n : %lld %lld\n",u,v,w,g[u],g[v]);
				if(g[u]+w<g[v]){
					g[v]=g[u]+w+(u+t>=K);
					q.push({-g[v],v});
				}
			}
		}
		//for(int i=0;i<K;++i)cout<<"G "<<i<<"="<<g[i]<<"\n";
		cnt=1,++TIM;
		for(int i=1;i<=n;++i)Ins(1,i,0);
		cin>>T,m=T.size();
		for(int i=1;i<=m;++i)dp[i]=INF;
		for(int i=0;i<m;++i)if(dp[i]!=INF){
			tmp=1;
			//cout<<"DP "<<i<<"="<<dp[i]<<"\n";
			for(int j=i+1;j<=m;++j){
				if(vis[tmp][T[j-1]-'a']!=TIM)break;
				tmp=c[tmp][T[j-1]-'a'];
				dp[j]=min(dp[j],dp[i]+cst[tmp]);
			}
		}
		cout<<(dp[m]==INF?-1ll:dp[m])<<endl;
	}
	return 0;
}
/*
2
2 3
defgh
abc
abcde
1 1
a
b
*/

詳細信息

Test #1:

score: 100
Accepted
time: 3ms
memory: 42000kb

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: 18ms
memory: 43600kb

input:

1
1413 4867
yumnkkghwtqnhpmmsbfwypcwudihegsvtdaykueuqozagpgdnhbwzoqrqswyctzonwmjdjmmpyzbyihhpyznmqltcuckqmcybbdafoexqztxhzlfjowznisnxtgoiqjztogldyegajmegalqgawrzotuntocmsagvkzdnwhmaewuxiuhvintpzwyrkgapnmegpveyuuorrfvrfakjyetfaoqtvwghlslsvmahrkvzkwpaviufknrpuapicqdnn
yumnkkghwtqnhpmmsbfwypcwudihegsvt...

output:

10

result:

ok 1 number(s): "10"

Test #3:

score: 0
Accepted
time: 49ms
memory: 45064kb

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: 146ms
memory: 42396kb

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: 53ms
memory: 100732kb

input:

1
7 4864
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

205

result:

ok 1 number(s): "205"

Test #6:

score: 0
Accepted
time: 40ms
memory: 95476kb

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: 77ms
memory: 48468kb

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: 0
Accepted
time: 12ms
memory: 248056kb

input:

1
1 5000
abaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

5023990000

result:

ok 1 number(s): "5023990000"

Test #9:

score: 0
Accepted
time: 43ms
memory: 94940kb

input:

1
100000 4817
acdcaaccca
cdbbbbabba
bcbccbcdbd
cdabaddaad
ddbdbabcac
dadadbbcba
aabcbbcabc
cdcadbbbda
dabacdbabd
dcbadbdabd
cdcbacbada
cadbbadbac
dbadbccdcd
babaddcdca
aaaddacccc
dabdcdadbb
abbbadbdaa
bcdbbacdaa
bcbcbddadb
bddaccbaba
baddadaaac
adbadbdaaa
cacbbbcdbc
abccdcacdb
abaacddbbc
acbbbcbcdc
...

output:

618356

result:

ok 1 number(s): "618356"

Test #10:

score: 0
Accepted
time: 19ms
memory: 95792kb

input:

10
3901 4952
srsofqyvrt
tazndzviuq
jcomfoxkiw
huzfqiecss
hhdtqpaohy
qcrokphbtf
xzkxssibix
hokmdpzydu
jreaeulsjt
vmxdsazajq
jawyofqbck
cwmzupygdm
rgsahqyxqt
kckgorfgvi
kcawvezmyb
mpcyhdifgn
xiwtmwttmp
mzknfqoifl
vbpvvdnqpy
anpdgjnbew
scekjovqpr
lzhfocjvld
oswjfhjdby
pmdbjddkpv
yjbnjcdtmc
gmpjgtpksa
r...

output:

904
208656
4560
30873
28272
5377
1326
93956
93720
282610

result:

ok 10 numbers