Description
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个病毒的DNA序列,问一串长度为n的DNA序列有多少种不包含这些病毒DNA序列的可能。(DNA序列尽由ATCG四种字符组成)。
//==============================================================
N的规模这么大,肯定没有办法朴素动归。很显然要用到矩阵的快速幂。
这题中m的规模和每个子串的长度很小,可以建立trie树再借助AC自动机来构造转移矩阵。求个快速幂就行了。
初始状态为一个1*tot的矩阵(tot指的是trie树中节点的个数,下同),其中(1,0)位置的值为1,对应trie树中的根节点,表示序列长度为零时,序列尾端不能匹配到trie树上的任意位置。
当序列长度为i时,得到的转移矩阵Ti表示当前串的尾部匹配到trie树上各节点的情况数(0节点的数值表示当前串的尾部匹配不到trie树上的任意串)
怎么构造转移矩阵?
考虑第i个节点,如果接下去加入的字符为A,并且它有一个打了标记的A儿子j(即存在病毒DNA的组成为根到j)那么就不能转移到j,同理,如果i沿着失败指针一直往上走,找到某个节点有A的儿子,就不能匹配。不然从自己开始沿失败指针走到最近的含有A的节点指向那个儿子。找不到指向0节点(即根,表示加入A匹配不到trie树中的串)。
程序中引入一个-1节点,在构造转移方程的时候会简便一些。
AC CODE
program pku_2778;
var trie:array[-1..100,1..4] of longint;
//============================================================================
procedure init;
var i,j,now,m:longint;
begin
end;
//============================================================================
procedure get_fail;
var l,r,i,j:longint;
begin
end;
//============================================================================
procedure quick(x:longint);
var i,j,k:longint;
begin
end;
//============================================================================
begin
end.