It's well known that DNA Sequence is a sequence only contains A, C, T and G, and it's very useful to analyze a segment of DNA Sequence,For example, if a animal's DNA sequence contains segment ATC then it may mean that the animal may have a genetic disease. Until now scientists have found several those segments, the problem is how many kinds of DNA sequences of a species don't contain those segments.
Suppose that DNA sequences of a species is a sequence that consist of A, C, T and G,and the length of sequences is a given integer n.
Input
First line contains two integer m (0 <= m <= 10), n (1 <= n <=2000000000). Here, m is the number of genetic disease segment, and n is the length of sequences.
Next m lines each line contain a DNA genetic disease segment, and length of these segments is not larger than 10.
Output
An integer, the number of DNA sequences, mod 100000.
Sample Input
4 3
AT
AC
AG
AA
Sample Output
36
困擾我一天的題┭┮﹏┭┮
題意:
給你m個序列,每個序列都是一種病毒(反正就是不能有就行了), 問長度爲n的字符串中有多少種不含病毒。
思路:
能有啥思路,瞎搞唄
看作有限狀態機,每種字符串都有一個狀態(一會解釋什麼狀態),每個狀態都有A T C G個方向,從而走向不同的狀態。含有病毒的狀態是不能走的。
這裏的狀態是指什麼狀態?
每個字符串有一個狀態,狀態就是與病毒串的匹配程度。
此時我盜一個圖(參考博客https://blog.csdn.net/morgan_xww/article/details/7834801)
這是病毒串 (ACG C)建立的tire樹,每個節點代表一個狀態 (紅色有向邊表示fail指針,藍色有向邊代表狀態之間的轉移)
0的話就代表此時的字符串沒有跟任何病毒串有一點匹配(該字符串後綴跟病毒的前綴匹配程度 )
比如 T ,G,T T, TG, AG ,AT等等
從O節點的狀態可以有A T C G的走向 。走A進入狀態1,代表與病毒串的匹配程度爲狀態1;走T進入狀態0,代表跟任何病毒序列沒有一毛錢關係,走C進入狀態4代表病毒串匹配程度爲狀態4(這個狀態是有病毒的),走G進入狀態0.
解釋下狀態2的轉移過程
如果一個串處於狀態2,那麼走A進入狀態1,(與病毒串的最大匹配只能爲狀態1),走T進入狀態0,走C進入狀態4,走G進入狀態0
用矩陣dp[i][j]代表走一步從i狀態到j狀態有多少方法。
那麼就得到一個矩陣
2 1 0 0 1
2 1 1 0 0
1 1 0 1 1
2 1 0 0 1
2 1 0 0 1
那麼走n步的結果就是求從0狀態走n步能到到所有的狀態數的方法和就是答案。即j矩陣dp的n次方(快速冪求)
需要注意的是:
1.含有病毒串的節點全都要標記成成病毒串
2.不能從含有病毒串的狀態走,也不能走含有病毒串的節點(用病毒的繼承,和fail指針找後綴就可做到)
坑點:
1.矩陣開long long
2.n用long long
每種字符串只能由一種狀態
AC在狀態2
ACG在狀態3
ACC在狀態4
所以 步驟
1.構建字典樹
2.建造fail指針,標記病毒節點
3.求狀態轉移矩陣
4.矩陣快速冪求長度爲n不含病毒串的種類數目
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<cstring>
#include<string>
#include<iostream>
using namespace std;
typedef long long ll;
const int maxn=100+7;
const int branch=4;
const ll mod=100000;
const int inf=0x3f3f3f3f;
ll Dp_Matrix[maxn][maxn];
struct Node
{
int cnt;
int fail;
int net[branch];
void clear()
{
cnt=0;
fail=0;
for(int i=0; i<branch; ++i)
net[i]=0;
}
};
class AcTree
{
public:
Node *node;
int top;
AcTree()
{
node=new Node[maxn];
top=0;
node[0].clear();
}
void init()
{
top=0;
node[0].clear();
}
int hash_letter(char c)
{
switch(c)
{
case 'A':
return 0;
case 'T':
return 1;
case 'C':
return 2;
case 'G':
return 3;
}
}
void insert(char *p)
{
int now=0;
while(*p)
{
int i=hash_letter(*p);
if(!node[now].net[i])
{
node[now].net[i]=++top;
node[top].clear();
}
now=node[now].net[i];
++p;
}
node[now].cnt=1;
}
void Bulid_fail()
{
queue<int> mmp;
int now=0,to;
for(int i=0; i<branch; ++i)
{
if(node[0].net[i])
mmp.push(node[0].net[i]);
}
while(!mmp.empty())
{
now=mmp.front();
mmp.pop();
//爲這個 兒子建造fail指針
if(node[node[now].fail].cnt)//fail含有病毒
node[now].cnt=1;
for(int i=0; i<branch; ++i)
{
if(node[now].net[i])
{
if(node[now].cnt)//病毒繼承
node[node[now].net[i]].cnt=1;
to=node[now].fail;//爲這個 兒子建造fail指針
while(to>0&&node[to].net[i]==0)
to=node[to].fail;
if(node[to].net[i])
to=node[to].net[i];
node[node[now].net[i]].fail=to;
mmp.push(node[now].net[i]);
}
}
}
}
void Bulid_Matrix()
{
int now, to;
memset(Dp_Matrix,0ll,sizeof(Dp_Matrix));
for(int i=0; i<=top; ++i)
{
for(int j=0; j<branch; ++j)
{
// if(!node[i].net&&!node[node[i].net[j]].cnt)
// Dp_Matrix[i][node[i].net[j]]++;
to=i;
while(to>0&&node[to].net[j]==0)
to=node[to].fail;
if(node[to].net[j])
to=node[to].net[j];
if(!node[i].cnt &&!node[to].cnt)
Dp_Matrix[i][to]++;
}
}
}
~AcTree()
{
delete []node;
}
};
void matix_mult(ll a[maxn][maxn],ll b[maxn][maxn],int n)//結果存到a 上
{
ll ans[maxn][maxn];
for(int i=0; i<n; ++i)
{
for(int j=0; j<n; ++j)
{
ans[i][j]=0ll;
for(int k=0; k<n; ++k)
{
ans[i][j]=(ans[i][j]+a[i][k]*b[k][j])%mod;
}
}
}
for(int i=0; i<n; ++i)
{
for(int j=0; j<n; ++j)
{
a[i][j]=ans[i][j];
}
}
}
void quick_matrixpow(ll a[maxn][maxn],long long b,int top)//結果存到a 上
{
ll ans[maxn][maxn];
memset(ans,0ll,sizeof(ans));
for(int i=0; i<top; ++i)
ans[i][i]=1ll;
while(b)
{
if(b&1)
{
matix_mult(ans,a,top);
}
matix_mult(a,a,top);
b/=2;
}
for(int i=0; i<maxn; ++i)
{
for(int j=0; j<maxn; ++j)
{
a[i][j]=ans[i][j];
}
}
}
int main()
{
AcTree dch;
int top;
int m,ans;
long long n;
char str[15];//記錄病毒序列
while(~scanf("%d %lld",&m,&n))
{
dch.init();
for(int i=0; i<m; ++i)
{
scanf("%s",str);
dch.insert(str);
}
dch.Bulid_fail();
dch.Bulid_Matrix();
top=dch.top;
quick_matrixpow(Dp_Matrix,n,top+1);
ans=0;
for(int i=0; i<=top; ++i)
{
ans=(ans+Dp_Matrix[0][i])%mod;
}
printf("%d\n",ans);
}
return 0;
}