您的位置:首页 > 产品设计 > UI/UE

【AC自动机+矩阵快速幂】 POJ 2778 DNA Sequence

2014-09-30 18:37 344 查看
先构建AC自动机,然后通过AC自动机构建矩阵,最后矩阵快速幂即可。。。

#include <iostream>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 300005
#define maxm 300005
#define eps 1e-10
#define mod 100000
#define INF 1e9
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid
#define rson o<<1 | 1, mid+1, R
typedef long long LL;
typedef unsigned long long ULL;
//typedef int LL;
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
void scanf(int &__x){__x=0;char __ch=getchar();while(__ch==' '||__ch=='\n')__ch=getchar();while(__ch>='0'&&__ch<='9')__x=__x*10+__ch-'0',__ch = getchar();}
LL gcd(LL _a, LL _b){if(!_b) return _a;else return gcd(_b, _a%_b);}
// head

struct AC
{
int next[maxn][4];
int fail[maxn];
int end[maxn];
char s[maxn];
queue<int> q;
int top, now, root;

int newnode(void)
{
end[top] = 0;
fail[top] = -1;
for(int i = 0; i < 4; i++)
next[top][i] = -1;
return top++;
}

void init(void)
{
top = 0;
root = newnode();
}

int hash(char ss)
{
if(ss == 'A') return 0;
if(ss == 'C') return 1;
if(ss == 'G') return 2;
if(ss == 'T') return 3;
}

void insert(void)
{
int len = strlen(s);
now = root;
for(int i = 0; i < len; i++) {
int t = hash(s[i]);
if(next[now][t] == -1) next[now][t] = newnode();
now = next[now][t];
}
end[now] = 1;
}

void build(void)
{
now = root;
for(int i = 0; i < 4; i++)
if(next[now][i] == -1) next[now][i] = root;
else {
fail[next[now][i]] = root;
q.push(next[now][i]);
}
while(!q.empty()) {
now = q.front(), q.pop();
end[now] |= end[fail[now]];
for(int i = 0; i < 4; i++)
if(next[now][i] == -1) next[now][i] = next[fail[now]][i];
else {
fail[next[now][i]] = next[fail[now]][i];
q.push(next[now][i]);
}
}
}
};

struct matrix
{
LL mat[105][105];
LL res[105][105];
LL mid[105][105];
int n;

void init(void)
{
memset(res, 0, sizeof res);
for(int i = 0; i < n; i++) res[i][i] = 1;
memset(mat, 0, sizeof mat);
}

void work(int c)
{
while(c) {
if(c%2) {
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++) {
LL tmp = 0;
for(int k = 0; k < n; k++)
tmp = (tmp + res[i][k] * mat[k][j]) % mod;
mid[i][j] = tmp;
}
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
res[i][j] = mid[i][j];
}
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++) {
LL tmp = 0;
for(int k = 0; k < n; k++)
tmp = (tmp + mat[i][k] * mat[k][j]) % mod;
mid[i][j] = tmp;
}
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
mat[i][j] = mid[i][j];
c /= 2;
}
}
};

int n, m;
AC ac;
void read(void)
{
ac.init();
while(m--) {
scanf("%s", ac.s);
ac.insert();
}
ac.build();
}

matrix t;

void work(void)
{
t.n = ac.top;
t.init();
for(int i = 0; i < ac.top; i++)
for(int j = 0; j < 4; j++)
if(!ac.end[ac.next[i][j]])
t.mat[i][ac.next[i][j]]++;
t.work(n);
LL ans = 0;
for(int i = 0; i < ac.top; i++) ans = (ans + t.res[0][i]) % mod;
printf("%I64d\n", ans);
}

int main(void)
{
while(scanf("%d%d", &m, &n)!=EOF) {
read();
work();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: