QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#149171#6635. Strange Keyboard275307894aTL 609ms37940kbC++141.8kb2023-08-24 07:13:462023-08-24 07:13:49

Judging History

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

  • [2023-08-24 07:13:49]
  • 评测
  • 测评结果:TL
  • 用时:609ms
  • 内存:37940kb
  • [2023-08-24 07:13:46]
  • 提交

answer

#include<bits/stdc++.h>
#define Gc() getchar() 
#define Me(x,y) memset(x,y,sizeof(x))
#define Mc(x,y) memcpy(x,y,sizeof(x))
#define d(x,y) ((m)*(x-1)+(y))
#define R(n) (rnd()%(n)+1)
#define Pc(x) putchar(x)
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
using namespace std;using ll=long long;using db=double;using lb=long db;using ui=unsigned;using ull=unsigned long long;using pii=pair<int,int>;using LL=__int128;
const int N=1e6+5,M=5e3+5,K=14348907+5,mod=998244353,Mod=mod-1;const db eps=1e-9;const int INF=1e9+7;mt19937 rnd(time(0));
int Le,n,k,g[M],f[M],tg[M],dp[M];string s[N],t;
namespace Trie{
	int son[N][26],f[N],Ct;
	void Cl(){for(int i=1;i<=Ct;i++) Me(son[i],0),f[i]=0;Ct=1;}
	void Ins(string c){
		int x=1,Le=c.size();
		for(int i=0;i<c.size();i++) {
			c[i]-='a';if(!son[x][c[i]]) son[x][c[i]]=++Ct,f[Ct]=INF;x=son[x][c[i]];
			f[x]=min(f[x],f[(Le-i)%k]+(Le-i)/k);
		}
	}
}
void Solve(){
	int i,j;cin>>n>>k;Me(g,0x3f);Trie::Cl();
	for(i=1;i<=n;i++) cin>>s[i],Le=s[i].size(),g[Le%k]=min(g[Le%k],Le/k+1);
	Me(f,0x3f);f[0]=0;Me(tg,0);
	for(i=0;i<k;i++){
		int Mi=-1;for(j=0;j<k;j++) if(!tg[j]&&(Mi==-1||f[Mi]>f[j])) Mi=j;
		for(j=0;j<Mi;j++) f[j]=min(f[j],f[Mi]+g[Mi-j]);
		for(j=Mi+1;j<k;j++) f[j]=min(f[j],f[Mi]+g[k-(j-Mi)]+1);
	}
	for(i=1;i<=n;i++) Trie::Ins(s[i]);
	cin>>t;for(i=0;i<t.size();i++) t[i]-='a';
	Me(dp,0x3f);dp[0]=0;
	for(i=0;i<t.size();i++){
		int x=1;
		for(j=i;j<t.size();j++) if(Trie::son[x][t[j]]){
			x=Trie::son[x][t[j]];
			dp[j+1]=min(dp[j+1],dp[i]+Trie::f[x]+1);
		}else break;
	}
	printf("%d\n",dp[t.size()]>=1e9?-1:dp[t.size()]);
}
int main(){
	ios::sync_with_stdio(false);cin.tie(0);
	int t;
	cin>>t;
	// t=1;
	while(t--) Solve();
	cerr<<clock()*1.0/CLOCKS_PER_SEC<<'\n';
}

詳細信息

Test #1:

score: 100
Accepted
time: 6ms
memory: 36624kb

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: 67ms
memory: 37940kb

input:

1
1413 4867
yumnkkghwtqnhpmmsbfwypcwudihegsvtdaykueuqozagpgdnhbwzoqrqswyctzonwmjdjmmpyzbyihhpyznmqltcuckqmcybbdafoexqztxhzlfjowznisnxtgoiqjztogldyegajmegalqgawrzotuntocmsagvkzdnwhmaewuxiuhvintpzwyrkgapnmegpveyuuorrfvrfakjyetfaoqtvwghlslsvmahrkvzkwpaviufknrpuapicqdnn
yumnkkghwtqnhpmmsbfwypcwudihegsvt...

output:

10

result:

ok 1 number(s): "10"

Test #3:

score: 0
Accepted
time: 609ms
memory: 36416kb

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...

output:


result: