HDU 2243 考研路茫茫——单词情结
考研路茫茫——单词情结
This problem will be judged on HDU. Original ID: 2243
64-bit integer IO format: %I64d Java class name: Main
一天,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 解题:Trie图+矩阵快速幂
#include <stdio.h>
#include <cstring>
#include <queue>
#include <iostream>
using namespace std;
using ULL = unsigned long long;
const int maxn = ;
struct Matrix{
ULL m[maxn][maxn],n;
void init(int sz,bool one){
memset(m,,sizeof m);
n = sz;
if(one) for(int i = ; i < n; ++i) m[i][i] = ;
}
Matrix(int sz,bool one = false){
init(sz,one);
}
Matrix operator*(const Matrix &rhs){
Matrix ret(n);
for(int k = ; k < n; ++k){
for(int i = ; i < n; ++i)
for(int j = ; j < n; ++j)
ret.m[i][j] += m[i][k]*rhs.m[k][j];
}
return ret;
}
Matrix operator^(int index){
Matrix ret(n,true);
while(index){
if(index&) ret = ret*(*this);
index >>= ;
*this = (*this)*(*this);
}
return ret;
}
void out(){
for(int i = ; i < n; ++i){
for(int j = ; j < n; ++j)
cout<<m[i][j]<<" ";
cout<<endl;
}
}
};
struct Trie{
int ch[maxn*maxn][],fail[maxn*maxn],cnt[maxn*maxn],tot;
void init(){
tot = ;
newnode();
}
int newnode(){
memset(ch[tot],,sizeof ch[tot]);
fail[tot] = cnt[tot] = ;
return tot++;
}
void insert(char *str,int root = ){
for(int i = ; str[i]; ++i){
int &x = ch[root][str[i]-'a'];
if(!x) x = newnode();
root = x;
}
++cnt[root];
}
void build(int root = ){
int q[maxn*maxn],hd = ,tl = ;
for(int i = ; i < ; ++i)
if(ch[root][i]) q[tl++] = ch[root][i];
while(hd < tl){
root = q[hd++];
cnt[root] += cnt[fail[root]];
for(int i = ; i < ; ++i){
int &x = ch[root][i],y = ch[fail[root]][i];
if(x){
fail[x] = y;
q[tl++] = x;
}else x = y;
}
}
}
ULL solve(int m){
Matrix a();
a.m[][] = a.m[][] = ;
a.m[][] = ;
Matrix b = a^m;
ULL ret = b.m[][];
a.init(tot*,false);
for(int i = ; i < tot; ++i){
if(cnt[i]) continue;
for(int j = ; j < ; ++j){
int x = ch[i][j];
if(cnt[x]) continue;
++a.m[i][x];
a.m[i][x + tot] = a.m[i][x];
}
}
for(int i = tot; i < *tot; ++i) a.m[i][i] = ;
b = a^m;
for(int i = tot; i < *tot; ++i)
ret -= b.m[][i];
return ret;
}
}ac;
int main(){
int n,m;
char str[];
while(~scanf("%d%d",&n,&m)){
ac.init();
for(int i = ; i < n; ++i){
scanf("%s",str);
ac.insert(str);
}
ac.build();
printf("%I64u\n",ac.solve(m));
}
return ;
}
HDU 2243 考研路茫茫——单词情结的更多相关文章
- hdu 2243 考研路茫茫——单词情结(AC自动+矩阵)
考研路茫茫——单词情结 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 2243 考研路茫茫——单词情结(AC自动机+矩阵)
考研路茫茫——单词情结 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 2243 考研路茫茫——单词情结 ac自动机+矩阵快速幂
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2243 题意:给定N(1<= N < 6)个长度不超过5的词根,问长度不超过L(L <23 ...
- HDU 2243 考研路茫茫——单词情结 求长度小于等于L的通路总数的方法
http://acm.hdu.edu.cn/showproblem.php?pid=2243 这是一题AC自动机 + 矩阵快速幂的题目, 首先知道总答案应该是26^1 + 26^2 + 26^3 .. ...
- HDU 2243 考研路茫茫——单词情结(AC自动机+矩阵快速幂)
http://acm.hdu.edu.cn/showproblem.php?pid=2243 题意: 给出m个模式串,求长度不超过n的且至少包含一个模式串的字符串个数. 思路: 如果做过poj2778 ...
- HDU 2243考研路茫茫——单词情结 (AC自动机+矩阵快速幂)
背单词,始终是复习英语的重要环节.在荒废了3年大学生涯后,Lele也终于要开始背单词了. 一天,Lele在某本单词书上看到了一个根据词根来背单词的方法.比如"ab",放在单词前一般 ...
- Hdu 2243 考研路茫茫——单词情结 (AC自己主动机+矩阵)
哎哟喂.中文题. . .不说题意了. 首先做过POJ 2778能够知道AC自己主动机是能够求出长度为L的串中不含病毒串的数量的. POJ 2778的大概思路就是先用全部给的病毒串建一个AC自己主动机. ...
- hdu 2243 考研路茫茫——单词情结 AC自动机 矩阵幂次求和
题目链接 题意 给定\(N\)个词根,每个长度不超过\(5\). 问长度不超过\(L(L\lt 2^{31})\),只由小写字母组成的,至少包含一个词根的单词,一共可能有多少个? 思路 状态(AC自动 ...
- HDU 2243 考研路茫茫――单词情结 ——(AC自动机+矩阵快速幂)
和前几天做的AC自动机类似. 思路简单但是代码200余行.. 假设solve_sub(i)表示长度为i的不含危险单词的总数. 最终答案为用总数(26^1+26^2+...+26^n)减去(solve_ ...
随机推荐
- AWVS11使用教程——Acunetix Web Vulnerability Scanner 11.x
AWVS11使用教程 一:普通扫描. 二:报告生成. 三:登陆扫描. Acunetix Web Vulnerability Scanner(简称AWVS)是一款知名的网络漏洞扫描工具,它通过网络爬虫测 ...
- usb被占用时,可以用这些方法进行adb无线调试
转自: http://www.cnblogs.com/shangdawei/p/4480278.html 可用wifi.网口. 1.先要获取root权限 如果手机没有命令行工具,请先在手机端安装终端模 ...
- 英文ubuntu中的乱码,输入法问题 集合
英文ubuntu文本文件默认编码是utf-8,windows下是gbk,所以产生乱码问题. 1.前言 运行命令查看系统编码 $locale 结果如下: LANG=en_US.UTF-8 LANGUAG ...
- Reference for shell scripting
${var} 和 $var的区别 http://stackoverflow.com/questions/8748831/when-do-we-need-curly-braces-in-variable ...
- Datapatch AND What to do if the status of a datapatch action was not SUCCESS due to finding non-ignorable errors
1. Enterprise Manager: Starting version 12.1 Enterprise Manager now calls datapatch to complete post ...
- [转]List of Visual Studio Project Type GUIDs
本文转自:http://www.codeproject.com/Reference/720512/List-of-Visual-Studio-Project-Type-GUIDs There isn' ...
- javascript 找出数字数组中最大的数
找出数字数组中最大的数 var Match = (function(){ var arr = null; var len = 0; return { max:function(arr,len){ ar ...
- 前端之HTML样式
<!doctype html> h5的文档声明 <html> 网页的根标签(根元素 html)--所有的代码都放置在此内 <head> <meta chars ...
- 亲身经历,Java面试题整理
博主在2015年暑期参加过一些Java开发工程师实习的面试和笔试,在此将重点整理出来,以供大家学习. 资料1: 一.单继承 1.1Java类是否支持多重继承? 答:继承的基本原则是: 子类继承父类的所 ...
- qt read excel
void exceladapter::readfile(QString filename, QString sheetname, int colNo){ QSqlDatabase db = QSqlD ...