Poj 2778 DNA Sequence AC automatic mechanism + matrix rapid Ming

Source: Internet
Author: User

The meaning of the question is very simple. Assume that the text set is A, C, T, G. Given M pattern strings, ask the text whose length is N.
How many possibilities are there...
It is really not intuitive...
The solution is to first learn the AC automatic mechanism and create a Trie graph. Based on the trie graph, we can obtain the path matrix with a length of 1, and then quickly
The path matrix with the length of N is obtained.
It is very hard to understand. I have never learned AC automatic machines. Before learning the AC automatic machine, it is said that you should first learn the Trie tree and KMP
To understand. It took nearly two days to learn about the AC automatic machine Trie diagram, and it was another day to understand this question.
The competition is coming soon. I don't know whether it is good or bad to switch from Changchun to Jinhua... It's still a weak dish...
Paste my Trie graph + quick nether (directly binary, not the algorithm written in number theory )...

# Include <stdio. h>
# Include <string. h>
# Include <algorithm>
# Include <queue>
Using namespace std;

Typedef long INT;
Const int MOD = 100000;
Const int MAX_P = 100;
Const int MAX_D = 4;
Int nIdx [256];
Char szPat [MAX_P];
INT nMatrix [MAX_P] [MAX_P];
Int B [MAX_P] [MAX_P];
Int a [MAX_P] [MAX_P];

Void InitIdx ()
{
NIdx ['a'] = 0;
NIdx ['C'] = 1;
NIdx ['T'] = 2;
NIdx ['G'] = 3;
}

Struct Trie
{
Trie * fail;
Trie * next [MAX_D];
Int no;
Bool flag;
Trie ()
{
Fail = NULL;
Memset (next, 0, sizeof (next ));
No = 0;
Flag = false;
}
};
Trie tries [MAX_D * MAX_P];
Int nP;
Trie * pRoot;

Trie * NewNode ()
{
Memset (& tries [nP], 0, sizeof (Trie ));
Tries [nP]. no = nP;
Return & tries [nP ++];
}

Void InitTrie (Trie * & pRoot)
{
NP = 0;
PRoot = NewNode ();
}

Void Insert (char * pszPat)
{
Trie * pNode = pRoot;

While (* pszPat)
{
If (pNode-> next [nIdx [* pszPat] = NULL)
{
PNode-> next [nIdx [* pszPat] = NewNode ();
}
PNode = pNode-> next [nIdx [* pszPat];
++ PszPat;
}
PNode-> flag = true;
}

Int BuildAC (Trie * pRoot)
{
Memset (nMatrix, 0, sizeof (nMatrix ));

PRoot-> fail = NULL;
Queue <Trie *> qt;
Qt. push (pRoot );
While (! Qt. empty ())
{
Trie * front = qt. front ();
Qt. pop ();

For (int I = 0; I <MAX_D; ++ I)
{
If (front-> next [I])
{
Trie * pNode = front-> fail;
While (pNode & pNode-> next [I] = NULL)
{
PNode = pNode-> fail;
}
Front-> next [I]-> fail = pNode? PNode-> next [I]: pRoot;
If (front-> next [I]-> fail-> flag = true)
{
Front-> next [I]-> flag = true;
}

Qt. push (front-> next [I]);
}
Else
{
Front-> next [I] = front = pRoot? PRoot: front-> fail-> next [I];
}

If (front-> next [I]-> flag = false)
{
NMatrix [front-> no] [front-> next [I]-> no] ++;
}
}
}

Return nP; // total number of nodes
}

Void MultyMatrix (int a [] [MAX_P], int B [] [MAX_P], INT C [] [MAX_P], int nSize)
{
For (int I = 0; I <nSize; ++ I)
{
For (int j = 0; j <nSize; ++ j)
{
INT nSum = 0;
For (int k = 0; k <nSize; ++ k)
{
NSum = (nSum + A [I] [k] * B [k] [j]) % MOD;
}
C [I] [j] = nSum;
}
}
}

Void CopyMatrix (int a [] [MAX_P], int B [] [MAX_P], int nSize)
{
For (int I = 0; I <nSize; ++ I)
{
For (int j = 0; j <nSize; ++ j)
{
A [I] [j] = B [I] [j];
}
}
}

Void MatrixPower (int m [] [MAX_P], int nSize, INT nP)
{
If (nP = 1)
{
CopyMatrix (A, M, nSize );
Return;
}

MatrixPower (M, nSize, nP/2 );
MultyMatrix (A, A, B, nSize );
If (nP % 2)
{
MultyMatrix (B, M, A, nSize );
}
Else
{
CopyMatrix (A, B, nSize );
}
}

Int main ()
{
INT nM, nN;

InitIdx ();
While (scanf ("% I64d % I64d", & nM, & nN) = 2)
{
InitTrie (pRoot );
While (nM --)
{
Scanf ("% s", szPat );
Insert (szPat );
}
Int nSize = BuildAC (pRoot );

MatrixPower (nMatrix, nSize, nN );
INT nAns = 0;
For (int I = 0; I <nSize; ++ I)
{
NAns = (nAns + A [0] [I]) % MOD;
}
Printf ("% I64d \ n", nAns % MOD );
}

Return 0;
}

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.