HDU 2243 Entrance Examination Road boundless--word complex AC automata + matrix fast Power

Source: Internet
Author: User

Links: http://acm.hdu.edu.cn/showproblem.php?pid=2243

Test instructions: given n (1<= N < 6) a root with a length not exceeding 5, the number of words with a length not exceeding L (l <231) containing at least one root word; MoD 264.

Base: The POJ 2778DNA sequence is the number of legitimate strings with a given length without a pattern string, and the string length is equal to the upper bound of int;

1.mod 264 Direct use of unsigned long long natural overflow can be, said some implicit. It is also easy to think of the use of built-in type directly, otherwise high-precision MoD 2^64 to kneel;

2. The subject is an upgraded version of the POJ 2778, inside the range, not a specific length, and still contain at least one, then directly from the negative solution, that is, the total number-does not contain the number of roots;

3. The operation of the Matrix, the realization of continuous discrete exponential summation; Add a column to the matrix, all 1, the added row is 1 except the lower right corner, and the others are 0;

$\begin{pmatrix}
A\rightarrow \begin{pmatrix}
A & 1\\
0 & 1
\end{pmatrix}
\end{pmatrix}$

$ans = \sum \limits_{i=0}^{\rm size}mat\left [0 \right]\left [i \right]$

The same solution: $\sum \limits_{i=0}^{\rm size}26^i$ only need to establish two-dimensional matrix

\begin{vmatrix}
& 1\\
0 & 1
\end{vmatrix} immediately after the direct matrix to the power;

15MS 1632k
#include <iostream>#include<cstdio>#include<cstring>#include<string.h>#include<algorithm>#include<vector>#include<cmath>#include<stdlib.h>#include<time.h>#include<stack>#include<Set>#include<map>#include<queue>using namespacestd;#defineRep0 (I,L,R) for (int i = (l); i < (R); i++)#defineREP1 (I,L,R) for (int i = (l); I <= (r); i++)#defineRep_0 (i,r,l) for (int i = (r); i > (l); i--)#defineRep_1 (i,r,l) for (int i = (r); I >= (l); i--)#defineMS0 (a) memset (A,0,sizeof (a))#defineMS1 (a) memset (A,-1,sizeof (a))#defineMSi (a) memset (A,0x3f,sizeof (a))#defineINF 0x3f3f3f3f#defineLson L, M, RT << 1#defineRson m+1, R, RT << 1|1#defineSqr (a) (a) * (a)typedef pair<int,int>PII;#defineA First#defineB Second#defineMK Make_pairtypedef __int64 LL;TYPEDEF unsignedLong Longull;template<typename t>voidRead1 (T &m) {T x=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} M= x*F;} Template<typename t>voidRead2 (T &a,t &b) {Read1 (a); Read1 (b);} Template<typename t>voidRead3 (T &a,t &b,t &c) {Read1 (a); Read1 (b); Read1 (c);} Template<typename t>void out(T a) {if(a>9) out(ATen); Putchar (A%Ten+'0');}intT,kase =0, I,j,k,n,m,top;#defineMoD 100000structmatrix{ull d[Panax Notoginseng][Panax Notoginseng]; intSz//can't also make ull, otherwise int = Ull will go wrongMatrix (intR) {MS0 (d); sz =R;} voidinit () { for(inti =0; I < sz;i++) D[i][i]=1; } Matrixoperator*(Constmatrix& a)Const{Matrix ans (A.SZ); for(inti =0; I < sz;i++) for(intj =0; J < sz;j++){ for(intK =0; k < sz;k++) Ans.d[i][j]+ = d[i][k]*A.d[k][j]; } returnans; } voidDebug () {Rep0 (I,0, SZ) {Rep0 (J,0, SZ) cout<<d[i][j]<<" "; cout<<Endl; } }}; Matrix Pow (Matrix a,ll m) {matrix ans (A.SZ); Ans.init (); while(m) {if(m&1) ans = ans*A; A= A *A; M>>=1; } returnans;}Const intSigma_size = -;Const intMAXN =Panax Notoginseng;structaho_corasick{intCh[maxn][sigma_size]; intVAL[MAXN],F[MAXN],LAST[MAXN],CNT[MAXN]; intsz; Map<string,int>MS; Aho_corasick () {}voidInit () {sz =1; val[0] =0; MS0 (ch[0]); MS0 (CNT); Ms.clear ();} intIdxCharc) {returnC-'a';} voidInsert (Char*s,intv) { intU =0, n =strlen (s); for(inti =0; I < n;i++){ intc =idx (s[i]); if(!Ch[u][c]) {MS0 (Ch[sz]); VAL[SZ]=0; CH[U][C]= sz++; } u=Ch[u][c]; } Val[u]=v; } voidGetfail () {Queue<int>Q; f[0] =0; for(intc =0; c < sigma_size;c++) {//Initialize Queue intU = ch[0][c]; if(u) {f[u] =0; Q.push (U); Last[u] =0;} } while(!Q.empty ()) { intR =Q.front (); Q.pop (); for(intc =0; c < sigma_size;c++){ intU =Ch[r][c]; if(!u) {Ch[r][c] = Ch[f[r]][c];Continue;}//Implementing Compressionq.push (U); intv =F[r]; while(v &&!ch[v][c]) v =F[v]; F[u]=Ch[v][c]; Last[u]= Val[f[u]]?F[u]:last[f[u]]; }}} Matrix Find () {Matrix ans (sz+1); for(inti =0; I < sz;i++){ if(Val[i] | | last[i])Continue; for(intj =0; J < sigma_size;j++){ intv =Ch[i][j]; if(Val[v] | | last[v])Continue; ANS.D[I][V]++; } Ans.d[i][sz]=1;//add a column to save the previous Sigma and;} Ans.d[sz][sz]=1;//Lower right corner returnans; }}ac;Charp[ the];intMain () {ull n,l; //freopen ("Data.txt", "R", stdin); while(SCANF ("%i64d%i64d", &n,&l) = =2) {ac.init (); REP1 (i,1, N) {scanf ("%s", p); Ac. Insert (P,i); } ac.getfail (); Matrix ans=AC. Find (); Ans=Pow (ans,l); Ull CNT=0; Rep0 (i,0, ANS.SZ) cnt + = ans.d[0][i]; Matrix A (2);//Seek the sum a.d[0][0] = -, a.d[0][1] = a.d[1][1] =1; A=Pow (a,l); printf ("%i64u\n", a.d[0][0]+a.d[0][1]-CNT); } return 0;}

HDU 2243 Entrance Examination Road boundless--word complex AC automata + matrix fast Power

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.