比赛 20160419x 评测结果 AAAAAAAAEE
题目名称 退票 最终得分 80
用户昵称 zys 运行时间 3.182 s
代码语言 C++ 内存使用 2.58 MiB
提交时间 2016-04-19 16:43:41
显示代码纯文本
#define maxn 210
#define maxl 100010
#define hash_mul 1313731ll
#define ll unsigned long long
#define inf 0x2fffffffffffffff

#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

int n,m,len[maxn];
char s[maxl];ll hash[maxl],pow_mul[maxl];

struct Matrix{
	ll s[maxn][maxn];
	void clear(){memset(s,0,sizeof(s));}
	void unit(){
		for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)s[i][j]=inf;
		for(int i=1;i<=n;i++)s[i][i]=0;return;
	}
	friend Matrix operator * (Matrix a,Matrix b){
		Matrix ret;for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)ret.s[i][j]=inf;
		for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)
		    for(int k=1;k<=n;k++)ret.s[i][j]=min(ret.s[i][j],a.s[i][k]+b.s[k][j]);
		return ret;
	}
}res,tranc;

Matrix quick_pow(Matrix a,int x)
{
	Matrix ret;ret.unit();
	while(x>0){
		if(x&1)ret=ret*a;
		a=a*a;x>>=1ll;
	}return ret;
}

inline int cal(int x,int y)//hash[len[x]-l]*pow_mul^l+p=hash[len[x]] p=hash[len[x]]-hash[len[x]-l]*pow_mul[l]
{
	int ret=len[y]-len[y-1],p=0;
	for(int l=1,r=min(len[x],len[y]);l<=r;l++)
		if(x==y&&l==r)break;
	    else if(hash[len[y-1]+l]==(hash[len[x]]-hash[len[x]-l]*pow_mul[l]))p=l;
	return ret-p;
}

inline void read()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++){
		scanf("%s",s+len[i-1]+1);
		len[i]=strlen(s+len[i-1]+1)+len[i-1];
		hash[len[i-1]+1]=s[len[i-1]+1];
		for(int k=len[i-1]+2;k<=len[i];k++)
		    hash[k]=hash[k-1]*hash_mul+s[k];
	}
	pow_mul[0]=1ll;
	for(int i=1;i<=len[n];i++)pow_mul[i]=pow_mul[i-1]*hash_mul;
}

inline void solve()
{
	res.unit();
	for(int i=1;i<=n;i++)res.s[i][i]=len[i]-len[i-1];
	for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)
	    tranc.s[i][j]=cal(i,j);
	res=res*quick_pow(tranc,m-1);
	ll ans=inf;
	for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)
	    if(res.s[i][j]<ans)ans=res.s[i][j];
	printf("%lld\n",ans);
}

int main()
{
	freopen("ticketa.in","r",stdin);
	freopen("ticketa.out","w",stdout);
	read();solve();
}