Problem description Back words, is always an important part of reviewing English. After wasting 3 years of college life, Lele finally began to recite words. One day, Lele in a word book to see a way to memorize words based on the root. For example, "AB", put in front of the word generally said "contrary, bad, leave" and so on.
So Lele thought, if the back of the root of n, then these roots in the end will appear in the word. The more accurate description is: the length of not more than L, consisting only of lowercase letters, at least a word containing a root, how many of them altogether? This does not consider whether the words have practical meaning.
For example, there are 2 root AA and AB, there may be 104 words with a length of not more than 3, respectively (2) Aa,ab, (26) Aaa,aab,aac...aaz, (26) Aba,abb,abc...abz, (25) Baa,caa,daa ... Zaa, (25) Bab,cab,dab...zab.
This is only a small case. And for other complex points of the situation, Lele is not a number, please help him now. Input This topic contains multiple sets of data, please process to the end of the file. Each group of data occupies two rows. The first line has two positive integers n and L. (0<n<6,0<l<2^31) The second line has N roots, each of which consists only of lowercase letters, not exceeding 5 in length. The middle of two stems is separated by a space. Output for each set of data, print out the total number of possible words in a row. Because the results can be very large, you only need to output the value of the 2^64 of the total number of words. Sample Input2 3AA AB1 2a sample Output10452 Authorlinle
#include <iostream>#include<cstring>#include<cstdio>#include<cmath>#include<queue>#include<iomanip>#definell unsigned __int64using namespacestd;ll n,l,e;Charts[Ten][Ten],s[Ten];//int nextt[10],match[10];//bool visit[10];//bool KMP (char Sa[],char sb[])//{//nextt[1] = 0;//int i,j,k,l1 = strlen (sa+1), L2 = strlen (sb+1);//For (i = 2;i<=l1;i++)// {//ll t = nextt[i-1];//while (t&&sa[i]!=sa[t+1]) t = nextt[t];//if (sa[i] = = sa[t+1]) t++;//nextt[i] = t;// }//match[0] = 0;//For (i = 1;i<=l2;i++)// {//ll t = match[i-1];//while (t&&sb[i]!=sa[t+1]) t = nextt[t];//if (sb[i] = = sa[t+1]) t++;//match[i] = t;//if (t = = L1) return 1;// }//return 0;//}structmtr{ll an[ -][ -];}; MTR tmmmt;mtr Mul (mtr a,mtr b) {mtr C; memset (c.an,0,sizeof(c.an)); inti,j,k; for(i =0; i<e;i++) for(j =0; j<e;j++) for(k =0; k<e;k++) C.an[i][j]+ = a.an[i][k]*B.an[k][j]; returnC;} MTR Mpow (mtr a,ll t) {mtr R; inti,j; memset (r.an,0,sizeof(r.an)); for(i =0; i<e;i++) R.an[i][i]=1; while(t) {if(t%2) R =Mul (r,a); A=Mul (a,a); T/=2; } returnR;}structnode{Node* nextt[ -],*fail; ll num; Node () { for(LL i =0;i< -; i++) Nextt[i]=NULL; Fail=NULL; Num=e; }};node*Root;voidInsert () {ll L=strlen (s), I; Node* k =Root; for(i =0; i<l;i++) {ll ID= s[i]-'a'; if(K->nextt[id] = =NULL) {k->nextt[id] =Newnode (); if(I! = L1) e++; } k= k->Nextt[id]; } k->num =0;}voidbuild () {Queue<node*>Q; Node* k =Root; for(LL i =0;i< -; i++) if(k->nextt[i]!=NULL) {k->nextt[i]->fail =K; Q.push (k-Nextt[i]); } while(!Q.empty ()) {Node* k =Q.front (); Q.pop (); for(LL i =0;i< -; i++) if(K->nextt[i]! =NULL) {Node* t = k->fail; while(T!=root&&t->nextt[i] = = NULL) T = t->fail; if(t->nextt[i]!=null) T = t->Nextt[i]; K->nextt[i]->fail =T; Q.push (k-Nextt[i]); } }}voidAsk () {Queue<node*>Q; Q.push (root); while(!Q.empty ()) {Node* k =Q.front (); Q.pop (); ll a= k->num; if(A = =0)Continue; for(LL i =0;i< -; i++) {node* Temp =K; BOOLBL =1; while(temp!=NULL) {ll B=1; if(temp->nextt[i]!=null) b = temp->nextt[i]->num; if(b = =0) {BL=0; Tmmmt.an[b][a]++; Break; } temp= temp->fail; } if(!BL)Continue; Temp=K; while(temp!=NULL) { if(temp->nextt[i]!=NULL) {ll B= temp->nextt[i]->num; Tmmmt.an[b][a]++; BL=0; Break; } temp= temp->fail; } if(BL) tmmmt.an[1][a]++; if(k->nextt[i]!=null) Q.push (k->Nextt[i]); } }}intMain () {inti,j,k; while(cin>>n>>L) {e=1; memset (tmmmt.an,0,sizeof(tmmmt.an)); Root=Newnode (); E++; for(i =0; i<n;i++) scanf ("%s", s), insert ();//For (i = 0;i<n;i++)// {//if (visit[i]) continue;//For (j = 0;j<n;j++)// {//if (i = = j) Continue;//if (KMP (Ts[i],ts[j]))//Visit[j] = 1;// }// }//For (i = 0;i<n;i++)// {// //if (visit[i]) continue;//ll L = strlen (ts[i]+1);//For (j = 0;j<=l;j++)//S[j] = ts[i][j+1];//Insert ();// }build (); Ask (); tmmmt.an[0][0] = -;//For (j = 0;j<e;j++)// {//ll sum = 0;//For (i = 0;i<e;i++)// {//Sum+=tmmmt.an[i][j];// }//Tmmmt.an[1][j] = 26-sum;// }tmmmt.an[e][0] = Tmmmt.an[e][e] =1; E++;//For (i = 1;i<e-1;i++)// {//For (j = 1;j<e-1;j++)// {//COUT<<SETW (3) <<tmmmt.an[i][j];// }//cout<<endl;// }TMMMT = Mpow (tmmmt,l+1);//For (i = 0;i<e;i++)// {//For (j = 0;j<e;j++)// {//cout<<tmmmt.an[i][j]<< "";// }//cout<<endl;// }printf"%i64u\n", tmmmt.an[e-1][1]); } return 0;}
It's kind of a.
hdu2243 the boundless--word complex