QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#466318#6635. Strange Keyboarducup-team191#TL 0ms3604kbC++232.2kb2024-07-07 18:12:222024-07-07 18:12:22

Judging History

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

  • [2024-07-07 18:12:22]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3604kb
  • [2024-07-07 18:12:22]
  • 提交

answer

#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
using pii=pair<int,int>;
using ll=long long;
using vi=vector<int>;
using vl=vector<ll>;
#define pb push_back
#define all(a) begin(a),end(a)

const int N=5010,MOD=1e9+7,B1=32984723,B2=345932;
const char en='\n';
const ll LLINF=1ll<<60;

void ad(int&a,int b)
{
	a+=b;
	if (a>=MOD) a-=MOD;
}

int add(int a,int b)
{
	if (a+b>=MOD) return a+b-MOD;
	return a+b;
}

void su(int&a,int b)
{
	a-=b;
	if (a<0) a+=MOD;
}

int sub(int a,int b)
{
	if (a<b) return a-b+MOD;
	return a-b;
}

int mul(int a,int b)
{
	return (a*1ll*b)%MOD;
}

int pot(int n,int k)
{
	if (k==0) return 1;
	int r=pot(n,k/2);
	r=mul(r,r);
	if (k%2) r=mul(r,n);
	return r;
}

void dod(pii&a,char c)
{
	a={add(mul(a.x,B1),c),add(mul(a.y,B2),c)};
}

string re;
ll t,di[N],rr[N],n,k,dp[N];
bool bio[N];

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin>>t;
	while (t--)
	{
		cin>>n>>k;
		vector<string> v(n);
		for (auto&x: v) cin>>x;
		cin>>re;
		for (int i=0;i<k;++i) di[i]=LLINF,rr[i]=LLINF,bio[i]=0;
		for (auto x: v) di[x.size()%k]=min(di[x.size()%k],(int)x.size()+k);
		rr[0]=0;
		for (int z=0;;++z)
		{
			ll nm=LLINF;
			int kak=-1;
			for (int i=0;i<k;++i) if (!bio[i] && rr[i]<nm)
			{
				nm=rr[i];
				kak=i;
			}
			if (kak==-1) break;
			bio[kak]=1;
			for (int i=kak;i<k;++i) rr[i]=min(rr[i],rr[kak]+di[i-kak]);
			for (int i=0;i<kak;++i) rr[i]=min(rr[i],rr[kak]+di[i+k-kak]);
		}
		vector<pair<pii,ll>> mo;
		for (auto x: v)
		{
			pii ha={0,0};
			int xs=x.size();
			for (int i=0;i<xs;++i)
			{
				dod(ha,x[i]);
				int moo=((i+1-xs)%k+k)%k;
				ll co=(rr[moo]+xs-(i+1))/k+1;
				//cout<<x<<' '<<i+1<<' '<<co<<' '<<moo<<' '<<rr[moo]<<en;
				mo.pb({ha,co});
			}
		}
		sort(all(mo));
		int rs=re.size();
		for (int i=0;i<=rs;++i) dp[i]=LLINF;
		dp[0]=0;
		for (int i=0;i<rs;++i)
		{
			pii ha={0,0};
			for (int j=i;j<rs;++j)
			{
				dod(ha,re[j]);
				auto it=lower_bound(all(mo),pair<pii,ll>(ha,0ll));
				if (it==mo.end() || it->x!=ha) continue;
				dp[j+1]=min(dp[j+1],dp[i]+it->y);
			}
		}
		if (dp[rs]==LLINF) cout<<-1<<en;
		else cout<<dp[rs]<<en;
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3604kb

input:

2
2 3
defgh
abc
abcde
1 1
a
b

output:

3
-1

result:

ok 2 number(s): "3 -1"

Test #2:

score: -100
Time Limit Exceeded

input:

1
1413 4867
yumnkkghwtqnhpmmsbfwypcwudihegsvtdaykueuqozagpgdnhbwzoqrqswyctzonwmjdjmmpyzbyihhpyznmqltcuckqmcybbdafoexqztxhzlfjowznisnxtgoiqjztogldyegajmegalqgawrzotuntocmsagvkzdnwhmaewuxiuhvintpzwyrkgapnmegpveyuuorrfvrfakjyetfaoqtvwghlslsvmahrkvzkwpaviufknrpuapicqdnn
yumnkkghwtqnhpmmsbfwypcwudihegsvt...

output:


result: