F-Postgraduate Road boundless-word complex crawling in process ... crawling failed Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%i64d &%i64u Submit Status Appoint Description:system Crawler (2013-05-30)
Description recite the words, is always review the important link of English. After 3 years of wasted college career, Lele also finally began to recite the words.
One day, Lele in a word book to see a word from the root to recite the method. For example, "AB", put in the word before the general expression "on the contrary, become bad, leave" and so on.
So Lele thought, if the back of the n root, that these stems in the end will appear in the word. The more accurate description is: The length is not more than L, composed of lowercase letters, at least one word of the root, how many may be altogether. It doesn't take into account whether the word has practical meaning.
For example, a total of 2 roots AA and AB, there may be 104 length of no more than 3 words, 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 just a small situation. And for other complex points of the situation, Lele is really not out of the number, now please help him.
Input This topic contains many sets of data, please handle to file end.
Each group of data occupies two rows.
The first row has two positive integers n and L. (0<n<6,0<l<2^31)
The second line has n roots, each of which consists of only lowercase letters, not exceeding 5 in length. The middle of two stems is separated by a space.
Output for each set of data, please print out the total number of possible words in one line.
Because the result can be very large, you only need to output the value of the word total modulus 2^64.
Sample Input
2 3 AA ab 1 2 A
Sample Output
104 52 Ideas: First Use AC automata to find the state, and then use the matrix fast power to find the number of words that do not contain roots. To find out all the words count, subtract on it. Error point: L input with int can not be +1 otherwise it took.
#include <iostream> #include <cstring> #include <cstdio> #include <queue> #define for (I,A,B)
(int i=a;i<=b;++i) #define CLR (f,z) memset (f,z,sizeof (f)) #define LL Unsigned The long long using namespace std;
const int msize=90;
const int sig=26;
Class Matrix {public:ll f[37][37];
int n;
Matrix () {};
Matrix (int x) {n=x;
for (i,0,x-1) for (j,0,x-1) f[i][j]=0;
Matrix operator* (const matrix&b) const {Matrix C=matrix (n);
for (i,0,n-1) for (j,0,n-1) for (k,0,n-1) c.f[i][j]+=f[i][k]*b.f[k][j];
return C; } void Out () {for (i,0,n-1) for (j,0,n-1) printf ("%llu%c", f[i][j],j==n-1?)
\ n ': ');
}
};
Class Ac_machine {Public:int f[msize],val[msize],ch[msize][sig],sz;
void Clear () {sz=1;
CLR (ch[0],0); val[0]=0;
int idx (char x) {return x ' a ';
} void Insert (Char*s,int v) {int u=0,c;
for (int i=0;s[i];++i) {c=idx (s[i]);
if (!ch[u][c]) {clr (ch[sz],0); val[sz]=0; ch[u][c]=sz++;
} U=ch[u][c];
} val[u]=v;
} void Getfail () {int u,v,r;
queue<int>q;
for (c,0,sig-1) {u=ch[0][c];
if (u) {q.push (u); f[u]=0; }} while (! Q.empty ()) {R=q.front ();
Q.pop ();
val[r]|=val[F[r]];
for (c,0,sig-1) {u=ch[r][c];
if (!u) {ch[r][c]=ch[f[r]][c];continue;
} q.push (U);
V=F[R];
while (V&&!ch[v][c]) v=f[v];
F[U]=CH[V][C]; The Matrix Getmatrix () {matrix Ret=matrix (sz+1);//one more for Sum to (i,0,sz-1) for (j,0,sig-1) if
(!val[Ch[i][j]]) ret.f[i][ch[i][j]]++;
for (I,0,sz) ret.f[i][sz]=1;
return ret;
}
};
Matrix M_pow (Matrix A,int M) {matrix Ret=matrix (A.N);
for (i,0,a.n-1) ret.f[i][i]=1;
while (m) {if (m&1) ret=ret*a;
a=a*a;m>>=1;
return ret;
} Char s[77];
Ac_machine AC;
int main () {int n,m;while (~SCANF ("%d%d", &n,&m)) {ac.clear ();
for (i,1,n) {scanf ("%s", s);
Ac.insert (s,1);
} ac.getfail ();
Matrix Ret=ac.getmatrix ();
Ret.out ();
Ret=m_pow (RET,M);
LL ans=0;
for (i,0,ret.n-1) ans+=ret.f[0][i];
Ret=matrix (2); --ans;
The initial is an extra ret.f[0][0]=1;ret.f[0][1]=1;
ret.f[1][0]=0;ret.f[1][1]=26;
Ret.out ();
Ret=m_pow (RET,M);
LL tans=0;
tans=ret.f[0][1]+ret.f[1][1];//pushed a--tans again;
Ans=tans-ans;
cout<<ans<<endl;
printf ("%llu\n", ans);
return 0;
}