1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
| #include <stdio.h> #include <string.h> #include <algorithm> #include <queue> using namespace std;
typedef long 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; }
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; }
|