86C. Genetic engineering[dp]

“Multidimensional spaces are completely out of style these days, unlike genetics problems” — thought physicist Woll and changed his subject of study to bioinformatics. Analysing results of sequencing he faced the following problem concerning DNA sequences. We will further think of a DNA sequence as an arbitrary string of uppercase letters “A”, “C”, “G” and “T” (of course, this is a simplified interpretation).

Let $w$ be a long DNA sequence and $s_1, s_2, …, s_m$ — collection of short DNA sequences. Let us say that the collection filters $w$ iff $w$ can be covered with the sequences from the collection. Certainly, substrings corresponding to the different positions of the string may intersect or even cover each other. More formally: denote by $|w|$ the length of $w$, let symbols of $w$ be numbered from $1$ to $|w|$. Then for each position $i$ in $w$ there exist pair of indices $l$, $r$($1 ≤ l ≤ i ≤ r ≤ |w|$) such that the substring $w$$[l … r]$ equals one of the elements $s_1, s_2, …, s_m$ of the collection.

Woll wants to calculate the number of DNA sequences of a given length filtered by a given collection, but he doesn’t know how to deal with it. Help him! Your task is to find the number of different DNA sequences of length $n$ filtered by the collection {$s_i$}.

Answer may appear very large, so output it modulo $1000000009$.

Input

First line contains two integer numbers $n$ and $m$ ($1 ≤ n ≤ 1000$, $1 ≤ m ≤ 10$) — the length of the string and the number of sequences in the collection correspondently.

Next $m$ lines contain the collection sequences $s$$i$, one per line. Each $s_i$ is a nonempty string of length not greater than 10. All the strings consist of uppercase letters “A”, “C”, “G”, “T”. The collection may contain identical strings.

Output

Output should contain a single integer — the number of strings filtered by the collection modulo $1000000009$ ($10^9 + 9$).

Examples

input

1
2
2 1
A

output

1
1

input

1
2
3
6 2
CAT
TACT

output

1
2

Note

In the first sample, a string has to be filtered by “A”. Clearly, there is only one such string: “AA”.

In the second sample, there exist exactly two different strings satisfying the condition (see the pictures below).

img

img

题目大意

给你m个模式串,要求构造一个长度为n的串,要求串的每一位至少属于一个模式串。

题解

显然dp,最开始想了一个dp[i][j][0/1]表示当前位为j,还有i个位没填,该位是否位某个串的结尾,的方案数,写了个记忆化搜索,然后算重了。

dp[i][j][k]表示已经填了i位,结尾的一位在trie中标号为j,最后的k位目前没有属于任何一个模式串的方案数。
val[i]表示以trie数上的点i为结尾,能表示的串的最大的长度。
数组开小了2次,调了好久QAQ。

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
#include<bits/stdc++.h>
using namespace std;
int qmax(int &x,int y) {if (x<y) x=y;}
int qmin(int &x,int y) {if (x>y) x=y;}
inline int read()
{
char s;int k=0,base=1;
while((s=getchar())!='-'&&s!=EOF&&!(isdigit(s)));
if(s==EOF)exit(0);if(s=='-')base=-1,s=getchar();
while(isdigit(s)){k=k*10+(s^'0');s=getchar();}
return k*base;
}
void write(int x)
{
if(x<0){putchar('-');write(-x);}
else{if(x/10)write(x/10);putchar(x%10+'0');}
}
int Map[100],n,m,id;
struct node
{
int end,fail;
} a[150];
char ch[150][5];
int val[150];
int son[150][5];
char Ch[20];
void insert()
{
int now=0,len=strlen(Ch+1);
for (int i=1;i<=len;i++)
{
if (ch[now][Map[Ch[i]]]==0)
{
ch[now][Map[Ch[i]]]=++id;
son[now][Map[Ch[i]]]=id;
}
now=ch[now][Map[Ch[i]]];
}
a[now].end++;
val[now]=len;
}
void get_fail()
{
queue<int> q;
while (!q.empty()) q.pop();
for (int i=1;i<=4;i++)
if (ch[0][i]!=0) q.push(ch[0][i]);
while (!q.empty())
{
int u=q.front();q.pop();
for (int i=1;i<=4;i++)
{
if (ch[u][i]!=0)
{
a[ch[u][i]].fail=ch[a[u].fail][i];
qmax(val[ch[u][i]],val[a[ch[u][i]].fail]);
q.push(ch[u][i]);
} else ch[u][i]=ch[a[u].fail][i];
}
}
}
int dp[1500][160][12];
int mod=1e9+9;
int ans;
int main()
{
Map['A']=1;Map['C']=2;Map['G']=3;Map['T']=4;
n=read();m=read();
for (int i=1;i<=m;i++)
{
scanf("%s",Ch+1);
insert();
}
get_fail();
memset(dp,0,sizeof(dp));
dp[0][0][0]=1;
for (int i=0;i<n;i++)
{
for (int j=0;j<=id;j++)
for (int k=0;k<=10;k++)
if (dp[i][j][k]!=0)
{
int v;
for (int c=1;c<=4;c++)
{
v=ch[j][c];
if (val[v]>=k+1)//可以把后面剩余的k+1位都匹配掉,就可以转移到0了
{
dp[i+1][v][0]+=dp[i][j][k];
if (dp[i+1][v][0]>=mod) dp[i+1][v][0]-=mod;
} else
{
dp[i+1][v][k+1]+=dp[i][j][k];//往后转移
if (dp[i+1][v][k+1]>=mod) dp[i+1][v][k+1]-=mod;
}
}
}
}
for (int i=0;i<=id;i++)
{
ans+=dp[n][i][0];//统计答案
ans=ans>=mod?ans-=mod:ans;
}
printf("%d\n",ans);
return 0;
}