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

考研路茫茫——单词情结

Time Limit: 1000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 2243
64-bit integer IO format: %I64d      Java class name: Main
背单词,始终是复习英语的重要环节。在荒废了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

解题:Trie图+矩阵快速幂

可以将$A+A^2+A^2+cdots+A^n$转而求[egin{bmatrix} A & A \ 0 & 1 end{bmatrix}^n]
然后矩阵快速幂加速
  1 #include <stdio.h>
  2 #include <cstring>
  3 #include <queue>
  4 #include <iostream>
  5 using namespace std;
  6 using ULL = unsigned long long;
  7 const int maxn = 65;
  8 struct Matrix{
  9     ULL m[maxn][maxn],n;
 10     void init(int sz,bool one){
 11         memset(m,0,sizeof m);
 12         n = sz;
 13         if(one) for(int i = 0; i < n; ++i) m[i][i] = 1;
 14     }
 15     Matrix(int sz,bool one = false){
 16         init(sz,one);
 17     }
 18     Matrix operator*(const Matrix &rhs){
 19         Matrix ret(n);
 20         for(int k = 0; k < n; ++k){
 21             for(int i = 0; i < n; ++i)
 22                 for(int j = 0; j < n; ++j)
 23                     ret.m[i][j] += m[i][k]*rhs.m[k][j];
 24         }
 25         return ret;
 26     }
 27     Matrix operator^(int index){
 28         Matrix ret(n,true);
 29         while(index){
 30             if(index&1) ret = ret*(*this);
 31             index >>= 1;
 32             *this = (*this)*(*this);
 33         }
 34         return ret;
 35     }
 36     void out(){
 37         for(int i = 0; i < n; ++i){
 38             for(int j = 0; j < n; ++j)
 39                 cout<<m[i][j]<<" ";
 40             cout<<endl;
 41         }
 42     }
 43 };
 44 struct Trie{
 45     int ch[maxn*maxn][26],fail[maxn*maxn],cnt[maxn*maxn],tot;
 46     void init(){
 47         tot = 0;
 48         newnode();
 49     }
 50     int newnode(){
 51         memset(ch[tot],0,sizeof ch[tot]);
 52         fail[tot] = cnt[tot] = 0;
 53         return tot++;
 54     }
 55     void insert(char *str,int root = 0){
 56         for(int i = 0; str[i]; ++i){
 57             int &x = ch[root][str[i]-'a'];
 58             if(!x) x = newnode();
 59             root = x;
 60         }
 61         ++cnt[root];
 62     }
 63     void build(int root = 0){
 64         int q[maxn*maxn],hd = 0,tl = 0;
 65         for(int i = 0; i < 26; ++i)
 66             if(ch[root][i]) q[tl++] = ch[root][i];
 67         while(hd < tl){
 68             root = q[hd++];
 69             cnt[root] += cnt[fail[root]];
 70             for(int i = 0; i < 26; ++i){
 71                 int &x = ch[root][i],y = ch[fail[root]][i];
 72                 if(x){
 73                     fail[x] = y;
 74                     q[tl++] = x;
 75                 }else x = y;
 76             }
 77         }
 78     }
 79     ULL solve(int m){
 80         Matrix a(2);
 81         a.m[0][0] = a.m[0][1] = 26;
 82         a.m[1][1] = 1;
 83         Matrix b = a^m;
 84         ULL ret = b.m[0][1];
 85         a.init(tot*2,false);
 86         for(int i = 0; i < tot; ++i){
 87             if(cnt[i]) continue;
 88             for(int j = 0; j < 26; ++j){
 89                 int x = ch[i][j];
 90                 if(cnt[x]) continue;
 91                 ++a.m[i][x];
 92                 a.m[i][x + tot] = a.m[i][x];
 93             }
 94         }
 95         for(int i = tot; i < 2*tot; ++i) a.m[i][i] = 1;
 96         b = a^m;
 97         for(int i = tot; i < 2*tot; ++i)
 98             ret -= b.m[0][i];
 99         return ret;
100     }
101 }ac;
102 int main(){
103     int n,m;
104     char str[20];
105     while(~scanf("%d%d",&n,&m)){
106         ac.init();
107         for(int i = 0; i < n; ++i){
108             scanf("%s",str);
109             ac.insert(str);
110         }
111         ac.build();
112         printf("%I64u
",ac.solve(m));
113     }
114     return 0;
115 }
View Code
原文地址:https://www.cnblogs.com/crackpotisback/p/4938668.html