HDU2243--考研路茫茫——单词情结

Problem Description
背单词,始终是复习英语的重要环节。在荒废了3年大学生涯后,Lele也终于要开始背单词了。
一天,Lele在某本单词书上看到了一个根据词根来背单词的方法。比如"ab",放在单词前一般表示"相反,变坏,离去"等。

于是Lele想,如果背了N个词根,那这些词根到底会不会在单词里出现呢。更确切的描述是:长度不超过L,只由小写字母组成的,至少包含一个词根的单词,一共可能有多少个呢?这里就不考虑单词是否有实际意义。

比如一共有2个词根 aa 和 ab ,则可能存在104个长度不超过3的单词,分别为
(2个) aa,ab,
(26个)aaa,aab,aac...aaz,
(26个)aba,abb,abc...abz,
(25个)baa,caa,daa...zaa,
(25个)bab,cab,dab...zab。

这个只是很小的情况。而对于其他复杂点的情况,Lele实在是数不出来了,现在就请你帮帮他。
 

Input
本题目包含多组数据,请处理到文件结束。
每组数据占两行。
第一行有两个正整数N和L。(0<N<6,0<L<2^31)
第二行有N个词根,每个词根仅由小写字母组成,长度不超过5。两个词根中间用一个空格分隔开。
 

Output
对于每组数据,请在一行里输出一共可能的单词数目。
由于结果可能非常巨大,你只需要输出单词总数模2^64的值。
 

Sample Input
  
  
2 3 aa ab 1 2 a
 

Sample Output
  
  
104 52
 

Author
linle


#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 1008
#define maxm 128
#define LL unsigned __int64
LL g[maxm][maxm];
LL Cnt,first,rear;
char str[18];
struct node
{
	node * fail;
	node * next[28];
	int cnt,end;
	node()
	{
		for(int i = 0;i < 28;i++)
		{
			next[i] = NULL;
		}
		cnt = Cnt++;
		end = 0;
	}
}*q[maxn],*qq[maxn];

void Creat_Tire(char * s,node * root)
{
	int len = strlen(s);
	node * p = root;
	for(int i = 0;i < len;i++)
	{
		int id = s[i] - 'a';
		if(p -> end)	break;
		if(p -> next[id] == NULL)
		{
			p -> next[id] = qq[Cnt-1] = new node();
		}
		p = p -> next[id];
	}
	p -> end = 1;
}


void build_ac_automation(node * root)
{
	q[rear++] = root;
	node * p = NULL;
	while(first < rear)
	{
		p = q[first++];
		for(int i = 0;i < 26;i++)
		{
			if(p -> next[i] != NULL)
			{
				if(p == root)
				{
					p -> next[i] -> fail = root;
				}
				else
				{
					p -> next[i] -> fail = p -> fail -> next[i];
					if(p -> fail -> next[i] -> end)	p -> next[i] -> end = 1;
				}
				q[rear++] = p -> next[i];
			}
			else
			{
				if(p == root)
				{
					p -> next[i] = root;
				}
				else 
				{
					p -> next[i] = p -> fail -> next[i];
				}
			}
		}
	}
}


void MatrixMul(LL b[][maxm],LL c[][maxm],int sz)
{
	LL temp[maxm][maxm];
	memset(temp,0,sizeof(temp));
	for(int i = 0;i < sz;i++)
	{
		for(int j = 0;j < sz;j++)
		{
			for(int k = 0;k < sz;k++)
			{
				temp[i][j] += b[i][k] * c[k][j];
			}
		}
	}
	for(int i = 0;i < sz;i++)
	{
		for(int j = 0;j < sz;j++)
		{
			b[i][j] = temp[i][j];
		}
	}
}

void MatrixPow(LL tot[][maxm],LL a[][maxm],LL sz,LL n)
{
	while(n > 0)
	{
		if(n & 1)	MatrixMul(tot,a,sz);
		MatrixMul(a,a,sz);
		n >>= 1;
	}
}


LL Ans[maxm][maxm];
int main()
{
	//freopen("in.txt","r",stdin);
	LL n,m;
	while(scanf("%I64u%I64u",&n,&m)==2)
	{
		Cnt = first = rear = 0;
		node * root = new node();
		qq[0] = root;
		memset(Ans,0,sizeof(Ans));
		for(int i = 0; i < n;i++)
		{
			scanf("%s",str);
			Creat_Tire(str,root);
		}
		memset(g,0,sizeof(g));
		build_ac_automation(root);
		LL fuck = Cnt;
		for(int i = 0;i < fuck;i++)
		{
			node * p = qq[i];
			if(p -> end)	continue;
			for(int j = 0;j < 26;j++)
			{
					if(p -> next[j] -> end == 0)
					{
						g[p -> cnt][p -> next[j] -> cnt]++;
					}
			}
		}
		//接下来要补全这个矩阵
		for(int i = 0;i < fuck;i++)
		{
			g[i][fuck+i] = 1;
			g[i+fuck][i+fuck] = 1;
		}
		for(int i = 0;i < maxm;i++)	Ans[i][i]  = 1;
		MatrixPow(Ans,g,2*fuck+4,m+1);
		LL ans1 = 0;
		for(int i = 0;i < Cnt;i++)
		{
			ans1 += Ans[0][i+fuck];
		}
		memset(Ans,0,sizeof(Ans));
		for(int i = 0;i < 2;i++)	Ans[i][i] = 1;
		memset(g,0,sizeof(g));
		g[0][0] = 26;
		g[0][1] = g[1][1] = 1;
		MatrixPow(Ans,g,2,m+1);
		LL ans2  = Ans[0][1] - 1;
		printf("%I64u\n",ans2 - ans1 + 1);
	}
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值