POJ 2778 DNA Sequence AC自动机DP的矩阵优化

本文探讨了如何通过分析DNA序列来识别潜在的遗传性疾病,包括DNA序列的组成、长度限制以及特定疾病段落的排除策略。通过构建AC自动机并进行矩阵运算,解决了在给定长度下构建不包含特定致病DNA序列的方法数量问题。
摘要由CSDN通过智能技术生成

传送门:点击打开链接

DNA Sequence

Time Limit: 1000MS


Memory Limit: 65536K

Total Submissions: 12215


Accepted: 4650

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

Source

题意:给出n个致病DNA,构造一个长度为m的DNA。要求其中不含任何一个致病的DNA,问有多少种构造的方法 ,对10W取MOD

思路:由于m很大,不能用普通的AC自动机的DP来做。先构造好AC自动机。再把构建一个矩阵mp。mp[i][j]表示自动机上的i点能安全转移到j点。什么叫安全转移呢?就是转移之后不会构造出致病DNA,其实只要保证转移之后的j点的last[j]=0,val[j]!=0即可。然后再对这个矩阵用二分幂求k次方。得到的矩阵mp[i][j]表示的就是从i点到j点走k条边的方法数。算一下现在的Sigm(mp[0][i])。就可以得到答案。

代码:

#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cstring>
#define SIGMA_SIZE 4
#define maxn 110
#include<queue>
#define MOD 100000
using namespace std;
int ch[maxn][SIGMA_SIZE];
int val[maxn];
int last[maxn], f[maxn];
int cnt;
inline int idx(char c)
{
	if (c == 'A') return 0;
	else if (c == 'C') return 1;
	else if (c == 'G') return 2;
	return 3;
}
void insert(char s[])
{
	int len = strlen(s);
	int u = 0;
	for (int i = 0; i<len; i++)
	{
		int v = idx(s[i]);
		if (!ch[u][v]) ch[u][v] = ++cnt;
		u = ch[u][v];
	}
	val[u] = 1;
}
struct Matrix
{
	__int64 g[maxn][maxn];
	int r, c;
	Matrix operator * (const Matrix &b) const
	{
		Matrix res;
		res.r = r;
		res.c = b.c;
		memset(res.g, 0, sizeof(res.g));
		for (int i = 0; i <= r; i++)
			for (int j = 0; j <= b.c; j++)
				for (int k = 0; k <= c; k++) res.g[i][j] = (res.g[i][j] + g[i][k] * b.g[k][j]) % MOD;
		return res;
	}
	void ONE(int M)
	{
		r = M;
		c = M;
		memset(g, 0, sizeof(g));
		for (int i = 0; i <= M; i++) g[i][i] = 1;
	}
};
Matrix bin(Matrix a, int k)
{
	Matrix res;
	if (k == 0)
	{
		res.ONE(a.r);
		return res;
	}
	else if (k == 1)
		return a;
	res = bin(a, k >> 1);
	res = res*res;
	if (k & 1) res = res*a;
	return res;
}
bool vis[maxn];
int mp[maxn][maxn];
void bfs()
{
	memset(vis, 0, sizeof(vis));
	vis[0] = 1;
	queue<int>q;
	q.push(0);
	while (!q.empty())
	{
		int u = q.front();
		q.pop();
		for (int i = 0; i<4; i++)
		{
			int p = u;
			while (p&&!ch[p][i]) p = f[p];
			p = ch[p][i];
			if (val[p]) continue;
			if (last[p]) continue;
			mp[u][p]++;
			if (!vis[p]) q.push(p);
			vis[p] = 1;
		}
	}
}
void slove(int m)
{
	memset(mp, 0, sizeof(mp));
	bfs();
	Matrix tmp;
	tmp.r = cnt;
	tmp.c = cnt;
	for (int i = 0; i <= cnt; i++)
		for (int j = 0; j <= cnt; j++) tmp.g[i][j] = mp[i][j];
	tmp = bin(tmp, m);
	int ans = 0;
	for (int i = 0; i <= cnt; i++) ans = (ans + tmp.g[0][i]) % MOD;
	printf("%d\n", ans);
}
void getFail()
{
	queue<int>q;
	f[0] = 0;
	for (int c = 0; c<SIGMA_SIZE; c++)
	{
		int u = ch[0][c];
		if (u)
		{
			f[u] = 0;
			q.push(u);
			last[u] = 0;
		}
	}
	while (!q.empty())
	{
		int r = q.front();
		q.pop();
		for (int c = 0; c<SIGMA_SIZE; c++)
		{
			int u = ch[r][c];
			if (!u)
			{
				//ch[r][c] = ch[f[r]][c];
				continue;
			}
			q.push(u);
			int v = f[r];
			while (v&&!ch[v][c]) v = f[v];
			f[u] = ch[v][c];
			last[u] = val[f[u]] ? f[u] : last[f[u]];
		}
	}
}
int main()
{
	int n, m;
	while (~scanf("%d %d", &n, &m))
	{
		memset(ch, 0, sizeof(ch));
		memset(val, 0, sizeof(val));
		cnt = 0;
		char s[15];
		for (int i = 1; i <= n; i++)
		{
			scanf("%s", s);
			insert(s);
		}
		getFail();
		slove(m);
	}
	return 0;
}
这份代码需要交C++,交G++会RE的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值