QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#488065#6635. Strange Keyboarducup-team052#AC ✓404ms279108kbC++232.1kb2024-07-23 15:54:042024-07-23 15:54:04

Judging History

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

  • [2024-07-23 15:54:04]
  • 评测
  • 测评结果:AC
  • 用时:404ms
  • 内存:279108kb
  • [2024-07-23 15:54:04]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

const int N = 1e6 + 5, M = 5005;
#define int long long
const int inf=0x3f3f3f3f3f3f3f3f;

char s[N];
int len[N],n,k,dis[M];
int ch[N][26],w[N],dep[N],cnt,m,mnl[M];
vector<int> v[N];
int f[M];
vector<int> can;
struct Node
{
	int id,val;
	Node(int a=0,int b=0) {id=a,val=b;}
	bool operator < (const Node &x) const {return val>x.val;}
};
void init()
{
	for(int i=0;i<k;i++) dis[i]=inf;
	dis[0]=0;
	priority_queue<Node> q;
	q.emplace(0,0);
	while(!q.empty())
	{
		auto [v,w]=q.top(); q.pop();
		if(dis[v]!=w) continue;
		for(int l:can)
		{
			int tl=(v-l+k)%k;
			int tw=dis[v]+(tl+mnl[l])/k+1;
			if(dis[tl]>tw) dis[tl]=tw,q.emplace(tl,tw);
		}
	}
	// for(int i=0;i<k;i++) printf("%d%c",dis[i]," \n"[i==k-1]);
	for(int i=1;i<=cnt;i++)
	{
		w[i]=inf;
		for(int j:v[i])
		{
			int cl=(len[j]-dep[i]);
			int cw=cl/k+dis[cl%k];
			w[i]=min(w[i],cw);
		}
	}
}
void ins(int len,int id)
{
	int cur=0;
	for(int i=1;i<=len;i++)
	{
		if(!ch[cur][s[i]-'a']) ch[cur][s[i]-'a']=++cnt,dep[cnt]=dep[cur]+1;
		cur=ch[cur][s[i]-'a'];
		v[cur].push_back(id);
	}
}

void work()
{
	cin>>n>>k;
	for(int i=0;i<=cnt;i++)
	{
		v[i].clear();
		for(int j=0;j<26;j++) ch[i][j]=0;
	}
	cnt=0;
	for(int i=0;i<k;i++) mnl[i]=inf;
	for(int i=1;i<=n;i++)
	{
		scanf("%s",s+1);
		len[i]=strlen(s+1);
		mnl[len[i]%k]=min(mnl[len[i]%k],len[i]);
		ins(len[i],i);
	}
	can.clear();
	for(int i=0;i<k;i++) if(mnl[i]!=inf) can.push_back(i);
	// for(int i:can) printf("%d ",i); cout<<"\n";
	init();
	scanf("%s",s+1); m=strlen(s+1);
	f[0]=0; for(int i=1;i<=m;i++) f[i]=inf;
	for(int i=0;i<m;i++)
	{
		if(f[i]==inf) continue;
		int cur=0;
		for(int j=i+1;j<=m;j++)
		{
			if(!ch[cur][s[j]-'a']) break;
			cur=ch[cur][s[j]-'a'];
			f[j]=min(f[j],f[i]+1+w[cur]);
		}
		// for(int i=1;i<=m;i++) printf("%d%c",f[i]," \n"[i==m]);
	}
	if(f[m]==inf) printf("-1\n");
	else printf("%lld\n",f[m]);
}

signed main() {
#ifdef xay5421
	freopen("a.in","r",stdin);
#endif
	int T;
	cin>>T;
	while (T--) {
		work();
	}
	return 0;
}

詳細信息

Test #1:

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

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: 55ms
memory: 21184kb

input:

1
1413 4867
yumnkkghwtqnhpmmsbfwypcwudihegsvtdaykueuqozagpgdnhbwzoqrqswyctzonwmjdjmmpyzbyihhpyznmqltcuckqmcybbdafoexqztxhzlfjowznisnxtgoiqjztogldyegajmegalqgawrzotuntocmsagvkzdnwhmaewuxiuhvintpzwyrkgapnmegpveyuuorrfvrfakjyetfaoqtvwghlslsvmahrkvzkwpaviufknrpuapicqdnn
yumnkkghwtqnhpmmsbfwypcwudihegsvt...

output:

10

result:

ok 1 number(s): "10"

Test #3:

score: 0
Accepted
time: 135ms
memory: 11228kb

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: 404ms
memory: 12196kb

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: 88ms
memory: 91296kb

input:

1
7 4864
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

205

result:

ok 1 number(s): "205"

Test #6:

score: 0
Accepted
time: 85ms
memory: 77556kb

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: 90ms
memory: 17792kb

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: 59ms
memory: 279108kb

input:

1
1 5000
abaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

5023990000

result:

ok 1 number(s): "5023990000"

Test #9:

score: 0
Accepted
time: 86ms
memory: 88620kb

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: 59ms
memory: 82528kb

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