This is a great problem. The application of AC automata in the construction transfer diagram DFA
The DFA transfer diagram is a diagram showing the state of the transfer process, and the DFA diagram can be used to find out the number of any DNA length, any state
To find out the DFA matrix using automata, there
| DP[N][0] dp[n][1] [... dp[n][m] |=|dp[1][0] dp[1][1] ... dp[1][m] | * dfa^ (n-1) (M refers to the total number of States)
DP boundary Matrix |dp[1][0] dp[1][1] ... dp[1][m] | That is the first line of the DFA, so the dp[n] matrix is dfa^n
It is obvious that a certain kind of state transfer equation can be obtained by automata, which is not only simple in thought, but also fast-accelerating state transfer with matrix, and the complexity of log N
I am through this learning http://hi.baidu.com/ccsu_010/item/7847a3c17f6fe2bc0d0a7b89, thanks
accepted796k360msc++3171b#include<cstdio> #include <cstring> #include <iostream> #include < Algorithm>using namespace Std;typedef long long ll;const int M = 110;const int mod = 100000;struct mat{ll a[m][m]; Mat () {memset (a,0,sizeof (a)); }};mat dfa;struct node{node *son[4]; Node *fail; BOOL Flag; int id;} Trie[m],*que[m],*root;int Index (char x) {switch (x) {case ' A ': return 0; Case ' C ': return 1; Case ' G ': return 2; Case ' T ': return 3; }}struct ac{int Head,tail,sz; Node *createnode () {trie[sz].flag=0; memset (trie[sz].son,0,sizeof (Trie[sz].son)); Trie[sz].fail=null; Trie[sz].id=sz; Return &trie[sz++]; } void Ini () {head=tail=0; sz=0; Root=createnode (); } void Insert (char str[]) {node *cur=root; for (int i=0;str[i];i++) {int val=index (str[i]); if (Cur->flag) break; If the currently inserted is already a dangerous fragment then you can not continue to insert (for this problem)//To continue to insert it can be but it is all marked as dangerous fragment if (Cur->flag) cur->son[val]->flag=true; if (cur->son[val]==null) {cur->son[val]=createnode (); } cur=cur->son[val]; } cur->flag=true; } void Acfun () {que[head++]=root; while (Tail
POJ 2778 DNA Sequence (AC automata to determine DFA transfer graph + Matrix fast power)