考研路茫茫——单词情结

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4843    Accepted Submission(s): 1527

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
/*
hdu 2243 考研路茫茫——单词情结(AC自动+矩阵) 给你m个子串,求包含至少一个子串的长度不大于n的字符串的种类数
所有可能: 26+26^2 + .... + 26^n
而且前面也求过一个子串都不包含的情况。即把他们的关系转换成矩阵mat
一个都不包含的情况: mat + mat^2 +..... + mat^n
对于求 次方和. mat+... mat^6 = mat+mat^2+mat^3 + mat^3*(mat+mat^2+mat^3)
于是求出两个的值然后减去即可 // 矩阵求a走m步到b的方案数 + A + A^2 + A^3 + ... + A^k的结果(两个矩阵的经典应用)
hhh-2016-04-23 22:33:39
*/
#include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <functional>
#include <map>
using namespace std;
#define lson (i<<1)
#define rson ((i<<1)|1)
typedef unsigned long long ll;
typedef unsigned int ul;
const int maxn = 40010;
int tot; struct Matrix
{
int len;
ll ma[50][50];
Matrix() {}
Matrix(int L)
{
len = L;
}
}; Matrix mult(Matrix ta,Matrix tb)
{
Matrix tc;
tc.len = ta.len;
for(int i = 0; i < ta.len; i++)
{
for(int j = 0; j < ta.len; j++)
{
tc.ma[i][j] = 0;
for(int k = 0; k < ta.len; k++){
tc.ma[i][j] = tc.ma[i][j]+(ll)ta.ma[i][k]*tb.ma[k][j];
}
}
}
return tc;
} Matrix pow_mat(Matrix a,ll n)
{
Matrix cnt;
cnt.len = a.len;
memset(cnt.ma,0,sizeof(cnt.ma));
for(int i = 0 ; i < cnt.len; i++)
cnt.ma[i][i] = 1; while(n)
{
if(n&1) cnt = mult(cnt,a);
a = mult(a,a);
n >>= 1;
}
return cnt;
} Matrix Add(Matrix ta,Matrix tb)
{
Matrix tc;
tc.len = ta.len;
for(int i = 0;i < tc.len;i++)
{
for(int j = 0;j < tc.len;j++)
{
tc.ma[i][j] = (ta.ma[i][j]+tb.ma[i][j]);
}
}
return tc;
} struct Tire
{
int nex[50][26],fail[50],ed[50];
int root,L;
int newnode()
{
for(int i = 0; i < 26; i++)
nex[L][i] = -1;
ed[L++] = 0;
return L-1;
} void ini()
{
L = 0,root = newnode();
} void inser(char buf[])
{
int len = strlen(buf);
int now = root;
for(int i = 0; i < len; i++)
{
int ta = buf[i]-'a';
if(nex[now][ta] == -1)
nex[now][ta] = newnode();
now = nex[now][ta];
}
ed[now] ++;
} void build()
{
queue<int >q;
fail[root] = root;
for(int i = 0; i < 26; i++)
if(nex[root][i] == -1)
nex[root][i] = root;
else
{
fail[nex[root][i]] = root;
q.push(nex[root][i]);
}
while(!q.empty())
{
int now = q.front();
q.pop();
if(ed[fail[now]])
ed[now] = 1;
for(int i = 0; i < 26; i++)
{
if(nex[now][i] == -1)
nex[now][i] = nex[fail[now]][i];
else
{
fail[nex[now][i]] = nex[fail[now]][i];
q.push(nex[now][i]);
}
}
}
} Matrix to_mat()
{
Matrix mat(L);
memset(mat.ma,0,sizeof(mat.ma));
for(int i = 0;i < L;i++)
{
for(int j = 0;j < 26;j++)
{
if(!ed[nex[i][j]])
mat.ma[i][nex[i][j]] ++;
}
}
return mat;
}
}; Matrix mat; Matrix cal(int n)
{
if(n == 1)
return mat;
Matrix tp = cal(n/2);
if(n & 1)
{
Matrix t = pow_mat(mat,n/2+1);
tp = Add(tp,mult(t,tp));
tp = Add(tp,t);
}
else
{
Matrix t = pow_mat(mat,n/2);
tp = Add(tp,mult(t,tp));
}
return tp;
} ll pow_mod(ll a,int n)
{
ll cnt = 1;
while(n)
{
if(n&1) cnt = cnt*a;
a = a*a;
n >>= 1;
}
return cnt;
} ll ca(int n)
{
if(n == 1)
return 26;
ll tp = ca(n/2);
if(n & 1)
{
ll t = pow_mod(26,n/2+1);
tp = tp+t+tp*t;
}
else
{
ll t = pow_mod(26,n/2);
tp = tp+t*tp;
}
return tp;
} Tire ac;
char buf[20];
int main()
{
int n,m;
while(scanf("%d%d",&m,&n) != EOF)
{
ac.ini();
for(int i = 1; i <= m; i++)
{
scanf("%s",buf);
ac.inser(buf);
}
ac.build();
mat = ac.to_mat();
Matrix ans = cal(n);
ll tans = ca(n);
ll t = 0;
for(int i = 0;i < ans.len;i++)
{
t += ans.ma[0][i];
}
printf("%I64u\n",tans-t);
}
return 0;
}

  

hdu 2243 考研路茫茫——单词情结(AC自动+矩阵)的更多相关文章

  1. hdu 2243 考研路茫茫——单词情结 AC自动机 矩阵幂次求和

    题目链接 题意 给定\(N\)个词根,每个长度不超过\(5\). 问长度不超过\(L(L\lt 2^{31})\),只由小写字母组成的,至少包含一个词根的单词,一共可能有多少个? 思路 状态(AC自动 ...

  2. hdu 2243 考研路茫茫——单词情结 ac自动机+矩阵快速幂

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=2243 题意:给定N(1<= N < 6)个长度不超过5的词根,问长度不超过L(L <23 ...

  3. HDU 2243 考研路茫茫——单词情结(AC自动机+DP+快速幂)

    题目链接 错的上头了... 这题是DNA的加强版,26^1 +26^2... - A^1-A^2... 先去学了矩阵的等比数列求和,学的是第二种方法,扩大矩阵的方法.剩下就是各种模板,各种套. #in ...

  4. HDU 2243 考研路茫茫——单词情结(AC自动机+矩阵)

    考研路茫茫——单词情结 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  5. HDU 2243 考研路茫茫——单词情结

    考研路茫茫——单词情结 Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID ...

  6. hdu_2243_考研路茫茫——单词情结(AC自动机+矩阵)

    题目链接:hdu_2243_考研路茫茫——单词情结 题意: 让你求包含这些模式串并且长度不小于L的单词种类 题解: 这题是poj2788的升级版,没做过的强烈建议先做那题. 我们用poj2778的方法 ...

  7. HDU 2243 考研路茫茫——单词情结 求长度小于等于L的通路总数的方法

    http://acm.hdu.edu.cn/showproblem.php?pid=2243 这是一题AC自动机 + 矩阵快速幂的题目, 首先知道总答案应该是26^1 + 26^2 + 26^3 .. ...

  8. HDU 2243考研路茫茫——单词情结 (AC自动机+矩阵快速幂)

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

  9. HDU 2243 考研路茫茫——单词情结(AC自动机+矩阵快速幂)

    http://acm.hdu.edu.cn/showproblem.php?pid=2243 题意: 给出m个模式串,求长度不超过n的且至少包含一个模式串的字符串个数. 思路: 如果做过poj2778 ...

随机推荐

  1. JAVA_SE基础——60.初识Object

    java是面向对象的语言,核心思想:找适合 的对象做适合 的事情:方式一:自定义类,然后通过自定义的类创建对象.方式二:sun提供了很多的类给我使用,我们只需要认识这些类,我们就可以通过这些类创建对象 ...

  2. ASP.NET MVC中错误处理方式

    /// <summary> /// 标记了HandleError,并指明错误处理页为AboutError.aspx /// </summary> /// <returns ...

  3. LeetCode & Q53-Maximum Subarray-Easy & 动态规划思路分析

    Array DP Divide and Conquer Description: Find the contiguous subarray within an array (containing at ...

  4. python识别验证码——PIL,pytesser,pytesseract的安装

    1.使用Python识别验证码需要安装Python的图像处理模块(PIL.pytesser.pytesseract) (安装过程需要pip,在我的Python中已经安装pip了,pip的安装就不在赘述 ...

  5. Django REST framework+Vue 打造生鲜超市(一)

    一.项目介绍 1.1.掌握的技术 Vue + Django Rest Framework 前后端分离技术 彻底玩转restful api 开发流程 Django Rest Framework 的功能实 ...

  6. 分贝块---dBblock

    分贝,用英语来表达的话,是decibel,是量度两个相同单位之数量比例的计量单位,主要用于度量声音强度,常用dB表示. 块,block,在百度百科中,指数据库中的最小存储和处理单位,包含块本身的头信息 ...

  7. Linux CentOS7.0 (03)安装验证 docker

    一.安装docker 1.升级 Linux 的软件包和内核 sudo yum update 2.安装 docker (1) sudo yum install docker  (2).验证docker安 ...

  8. 新概念英语(1-67)The weekend

    新概念英语(1-67)The weekend What are the Johnsons going to do at the weekend? A:Hello. Were you at the bu ...

  9. leetcode算法: Average of Levels in Binary Tree

    Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...

  10. Django REST framework+Vue 打造生鲜超市(四)

    五.商品列表页 5.1.django的view实现商品列表页 (1)goods/view_base.py 在goods文件夹下面新建view_base.py,为了区分django和django res ...