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

首先建立Trie和失败指针,然后你会发现对于每个节点 i 匹配AGCT时只有以下几种情况:

i 节点有关于当前字符的儿子节点 j 且安全,则i 到 j找到一条长度为 1的路。

i 节点有关于当前字符的儿子节点 j 且 不安全,则i 到 j没有路。

i 节点有关于当前字符的儿子节点但是能通过失败指针找到一个安全的节点j,那么 i 到 j 找到一条长度为1的路。

关于节点安全的定义:

当前节点不是末节点且当前节点由失败指针指回跟节点的路径上不存在不安全节点,那么这个节点就是安全节点。

然后问题就转化成了从root到其它所有的安全节点有多少条长度为m的路径。

设A为此张Trie图的对应矩阵,那么A^m的第一行的sigma即为答案。


无意之间又自cha了一份自己在POJ AC的代码。。。

 数据:

3 10

AT

GATC

TGAC


正解:48233


这一份代码跑出50333竟然也过了。。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <cmath>
#include <stack>
#include <map>

#pragma comment(linker, "/STACK:1024000000");
#define EPS (1e-8)
#define LL long long
#define ULL unsigned long long
#define _LL __int64
#define INF 0x3f3f3f3f

using namespace std;

const int Mod = 100000;
const int MAXN = 110;
const int MAXS = 4;

struct Mat
{
    LL mat[MAXN][MAXN];
    int r,c;

    void Init(int val,int R,int C)
    {
        r = R,c = C;
        for(int i = 1;i <= r; ++i)
            for(int j = 1;j <= c; ++j)
                if(i != j)
                    mat[i][j] = 0;
                else
                    mat[i][j] = val;
    }
};

Mat MatrixMult(Mat a,Mat b)
{
    Mat p;
    p.Init(0,a.r,b.c);

    for(int i = 1;i <= a.r; ++i)
    {
        for(int j = 1;j <= b.c; ++j)
        {
            for(int k = 1;k <= b.r; ++k)
            {
                p.mat[i][j] += a.mat[i][k]*b.mat[k][j];
                p.mat[i][j] %= Mod;
            }
        }
    }

    return p;
}

Mat QuickMult(_LL k,Mat coe)
{
    Mat p;

    p.Init(1,coe.r,coe.c);

    while(k >= 1)
    {
        if(k&1)
            p = MatrixMult(p,coe);
        coe = MatrixMult(coe,coe);
        k >>= 1;
    }

    return p;
}

struct N
{
    int next[MAXS],flag,fail;
}st[110];

int Top;

int creat()
{
    for(int i = 0;i < MAXS; ++i)
        st[Top].next[i] = -1;
    st[Top].fail = -1,st[Top].flag = 0;
    return Top++;
}

char s[12];

int sel(char c)
{
    if(c == 'A')
        return 0;
    if(c == 'G')
        return 1;
    if(c == 'C')
        return 2;
    return 3;
}

void Get_Trie(int root,char *s)
{
    int site = 1;
    while(s[site] != '\0')
    {
       if(st[root].next[sel(s[site])] == -1)
            st[root].next[sel(s[site])] = creat();
        root = st[root].next[sel(s[site])];
        ++site;
    }

    st[root].flag++;
}

queue<int> q;

int Get_Fail(int site,int tar)
{
    while(site != -1 && st[site].next[tar] == -1)
        site = st[site].fail;
    if(site == -1)
        return 0;
    return st[site].next[tar];
}

void Get_Fail(int root)
{
    st[root].fail = -1;

    q.push(root);

    int f;

    while(q.empty() == false)
    {
        f = q.front();
        q.pop();
        for(int i = 0;i < MAXS; ++i)
        {
            if(st[f].next[i] != -1)
            {
                st[st[f].next[i]].fail = Get_Fail(st[f].fail,i);
                q.push(st[f].next[i]);
            }
        }
    }
}

bool mark[110];

bool Check(int site)
{
    if(site == -1)
        return true;
    if(st[site].flag != 0 || Check(st[site].fail) == false)
        return false;
    return true;
}

int Check(int site,int tar)
{
    if(site == -1)
        return 0;
    if(st[site].next[tar] != -1)
    {
        if(st[st[site].next[tar]].flag != 0)
            return -1;
        return st[site].next[tar];
    }
    return Check(st[site].fail,tar);
}

void Cal_Mat(int root,Mat &p)
{
    memset(mark,false,sizeof(mark));

    mark[root] = true;

    q.push(root);

    int f;

    while(q.empty() == false)
    {
        f = q.front();
        q.pop();
        for(int i = 0;i < MAXS; ++i)
        {
            if(st[f].next[i] != -1 && Check(st[f].next[i]))
            {
                p.mat[f+1][st[f].next[i]+1]++;
                q.push(st[f].next[i]);
            }
            else if(st[f].next[i] == -1)
            {
                int tmp =  Check(f,i);
                if(tmp != -1)
                    p.mat[f+1][tmp+1]++;
            }
        }
    }
}

int main()
{
    int root,i,n,m;

    while(scanf("%d %d",&n,&m) != EOF)
    {
        Top = 0;
        root = creat();
        for(i = 1;i <= n; ++i)
        {
            scanf("%s",s+1);
            Get_Trie(root,s);
        }

        Get_Fail(root);

        Mat p;
        p.Init(0,Top,Top);
        Cal_Mat(root,p);

        p = QuickMult(m,p);

        LL ans = 0;

        for(i = 1;i <= Top; ++i)
            ans += p.mat[1][i],ans %= Mod;
        printf("%lld\n",ans);
    }

    return 0;
}


附上正解代码。。。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <cmath>
#include <stack>
#include <map>

#pragma comment(linker, "/STACK:1024000000");
#define EPS (1e-8)
#define LL long long
#define ULL unsigned long long
#define _LL __int64
#define INF 0x3f3f3f3f

using namespace std;

const int Mod = 100000;
const int MAXN = 110;
const int MAXS = 4;

struct Mat
{
    LL mat[MAXN][MAXN];
    int r,c;

    void Init(int val,int R,int C)
    {
        r = R,c = C;
        for(int i = 1;i <= r; ++i)
            for(int j = 1;j <= c; ++j)
                if(i != j)
                    mat[i][j] = 0;
                else
                    mat[i][j] = val;
    }
};

Mat MatrixMult(Mat a,Mat b)
{
    Mat p;
    p.Init(0,a.r,b.c);

    for(int i = 1;i <= a.r; ++i)
    {
        for(int j = 1;j <= b.c; ++j)
        {
            for(int k = 1;k <= b.r; ++k)
            {
                p.mat[i][j] += a.mat[i][k]*b.mat[k][j];
                p.mat[i][j] %= Mod;
            }
        }
    }

    return p;
}

Mat QuickMult(_LL k,Mat coe)
{
    Mat p;

    p.Init(1,coe.r,coe.c);

    while(k >= 1)
    {
        if(k&1)
            p = MatrixMult(p,coe);
        coe = MatrixMult(coe,coe);
        k >>= 1;
    }

    return p;
}

struct N
{
    int next[MAXS],flag,fail;
}st[110];

int Top;

int creat()
{
    //memset(st[Top].next,-1,sizeof(st[Top].next));
    for(int i = 0;i < MAXS; ++i)
        st[Top].next[i] = -1;
    st[Top].fail = -1,st[Top].flag = 0;
    return Top++;
}

char s[12];

int sel(char c)
{
    if(c == 'A')
        return 0;
    if(c == 'G')
        return 1;
    if(c == 'C')
        return 2;
    return 3;
}

void Get_Trie(int root,char *s)
{
    int site = 1;
    while(s[site] != '\0')
    {
        if(st[root].next[sel(s[site])] == -1)
            st[root].next[sel(s[site])] = creat();
        root = st[root].next[sel(s[site])];
        ++site;
    }

    st[root].flag++;
}

queue<int> q;

int Get_Fail(int site,int tar)
{
    while(site != -1 && st[site].next[tar] == -1)
        site = st[site].fail;
    if(site == -1)
        return 0;
    return st[site].next[tar];
}

void Get_Fail(int root)
{
    st[root].fail = -1;

    q.push(root);

    int f;

    while(q.empty() == false)
    {
        f = q.front();
        q.pop();
        for(int i = 0;i < MAXS; ++i)
        {
            if(st[f].next[i] != -1)
            {
                st[st[f].next[i]].fail = Get_Fail(st[f].fail,i);
                q.push(st[f].next[i]);
            }
        }
    }
}

bool mark[110];

bool Check(int site)
{
    if(site == -1)
        return true;
    if(st[site].flag != 0 || Check(st[site].fail) == false)
        return false;
    return true;
}

int Check(int site,int tar)
{
    if(site == -1)
        return 0;
    if(st[site].next[tar] != -1)
    {
        if(st[st[site].next[tar]].flag != 0)
            return -1;
        return st[site].next[tar];
    }
    return Check(st[site].fail,tar);
}

void Cal_Mat(int root,Mat &p)
{
    memset(mark,false,sizeof(mark));

    mark[root] = true;

    q.push(root);

    int f;

    while(q.empty() == false)
    {
        f = q.front();
        q.pop();
        for(int i = 0;i < MAXS; ++i)
        {
            if(st[f].next[i] != -1 && Check(st[f].next[i]))
            {
                p.mat[f+1][st[f].next[i]+1]++;
                q.push(st[f].next[i]);
            }
            else if(st[f].next[i] == -1)
            {
                int tmp =  Check(f,i);
                if(tmp != -1 && Check(tmp))
                    p.mat[f+1][tmp+1]++;
            }
        }
    }
}

int main()
{
    int root,i,n,m;

    while(scanf("%d %d",&n,&m) != EOF)
    {
        Top = 0;
        root = creat();
        for(i = 1;i <= n; ++i)
        {
            scanf("%s",s+1);
            Get_Trie(root,s);
        }

        Get_Fail(root);

        Mat p;
        p.Init(0,Top,Top);

        Cal_Mat(root,p);

        p = QuickMult(m,p);

        LL ans = 0;

        for(i = 1;i <= Top; ++i)
            ans += p.mat[1][i],ans %= Mod;
        printf("%lld\n",ans);
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java解决POJ3233—矩阵幂序列问题的代码和解释: ```java import java.util.Scanner; public class Main { static int n, k, m; static int[][] A, E; public static void main(String[] args) { Scanner sc = new Scanner(System.in); n = sc.nextInt(); k = sc.nextInt(); m = sc.nextInt(); A = new int[n][n]; E = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { A[i][j] = sc.nextInt() % m; E[i][j] = (i == j) ? 1 : 0; } } int[][] res = matrixPow(A, k); int[][] ans = matrixAdd(res, E); printMatrix(ans); } // 矩阵乘法 public static int[][] matrixMul(int[][] a, int[][] b) { int[][] c = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % m; } } } return c; } // 矩阵快速幂 public static int[][] matrixPow(int[][] a, int b) { int[][] res = E; while (b > 0) { if ((b & 1) == 1) { res = matrixMul(res, a); } a = matrixMul(a, a); b >>= 1; } return res; } // 矩阵加法 public static int[][] matrixAdd(int[][] a, int[][] b) { int[][] c = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { c[i][j] = (a[i][j] + b[i][j]) % m; } } return c; } // 输出矩阵 public static void printMatrix(int[][] a) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { System.out.print(a[i][j] + " "); } System.out.println(); } } } ``` 解释: 1. 首先读入输入的n、k、m和矩阵A,同时初始化单位矩阵E。 2. 然后调用matrixPow函数求出A的k次幂矩阵res。 3. 最后将res和E相加得到结果ans,并输出。 4. matrixMul函数实现矩阵乘法,matrixPow函数实现矩阵快速幂,matrixAdd函数实现矩阵加法,printMatrix函数实现输出矩阵。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值