DNA Sequence:ac自动机 + 矩阵快速幂

分析

我们去建一个ac自动机,把每一个病毒序列的结尾都打上标记,然后遍历所有的序列,如果这个节点没有打上标记,就遍历他的字节点,如果子节点也没有打上标记,就说明这两个节点之间是可以发生转移的
我们用一个矩阵去储存任意两个点之间的转移关系,然后根据离散数学的知识可以得到把这个矩阵n次方之后就是任意两个节点之间转移n次之后的结果,用矩阵快速幂然后计数就可以了

代码


#pragma GCC optimize(3)
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <cstring>
#include <stack>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int N = 102;
const ll mod= 100000;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a){
    
    char c=getchar();T x=0,f=1;while(!isdigit(c)){
    
    if(c=='-')f=-1;c=getchar();}
while(isdigit(c)){
    
    x=(x<<1)+(x<<3)+c-'0';c=getchar();}a=f*x;}
int gcd(int a,int b){
    
    return (b>0)?gcd(b,a%b):a;}
int tr[N][4],idx,ne[N],cnt[N];
int n,m;
int q[N];
char str[N];

struct Node{
    
    
    ll a[N][N];
    Node(){
    
    
        memset(a,0,sizeof a);
    }
};

Node operator*(const Node &A,const Node &B){
    
    
    Node res;
    for(int i = 0;i <= idx;i++)
        for(int j = 0;j <= idx;j++)
            for(int k = 0;k <= idx;k++)
                res.a[i][j] = (res.a[i][j] + A.a[i][k] * B.a[k][j]) % mod;
    return res;
}

Node fastm(Node A,int x){
    
    
    Node res;
    for(int i = 0;i <= idx;i++) res.a[i][i] = 1;
    while(x){
    
    
        if(x & 1) res = res * A;
        A = A * A;
        x >>= 1;
    }
    return res;
}

void init(){
    
    
    memset(tr,0,sizeof tr);
    memset(ne,0,sizeof ne);
    memset(cnt,0,sizeof cnt);
    idx= 0 ;
}

int get(char x){
    
    
    if(x == 'A') return 0;
    if(x == 'T') return 1;
    if(x == 'C') return 2;
    return 3;
}

void insert(){
    
    
    int p = 0;
    for(int i = 0;str[i];i++){
    
    
        int t = get(str[i]);
        if(!tr[p][t]) tr[p][t] = ++idx;
        p = tr[p][t]; 
    }
    cnt[p] = 1;
}

void build(){
    
    
    int hh = 0,tt = -1;
    for(int i = 0;i < 4;i++)
        if(tr[0][i])
            q[++tt] = tr[0][i];
    while(hh <= tt){
    
    
        int t = q[hh++];
        for(int i = 0;i < 4;i++){
    
    
            int c = tr[t][i];
            if(!c) tr[t][i] = tr[ne[t]][i];
            else{
    
    
                ne[c] = tr[ne[t]][i];
                q[++tt] = c;
            }
            cnt[tr[t][i]] |= cnt[tr[ne[t]][i]];
        }
    }
}



int main(){
    
    
    while(~scanf("%d%d",&n,&m)){
    
    
        init();
        for(int i = 1;i <= n;i++){
    
    
            scanf("%s",str);
            insert();
        }
        build();
        Node res;
        for(int i = 0;i <= idx;i++)
            if(!cnt[i])
                for(int j = 0;j < 4;j++){
    
    
                    if(!cnt[tr[i][j]])
                        res.a[i][tr[i][j]]++;
            }
        Node x = fastm(res,m);
        ll ans = 0;
        for(int i = 0;i <= idx;i++){
    
    
            ans = (ans + x.a[0][i]) % mod;
        }
        dl(ans);
    }
    return 0;

}

/**
*  ┏┓   ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃       ┃
* ┃   ━   ┃ ++ + + +
*  ████━████+
*  ◥██◤ ◥██◤ +
* ┃   ┻   ┃
* ┃       ┃ + +
* ┗━┓   ┏━┛
*   ┃   ┃ + + + +Code is far away from  
*   ┃   ┃ + bug with the animal protecting
*   ┃    ┗━━━┓ 神兽保佑,代码无bug 
*   ┃        ┣┓
*    ┃        ┏┛
*     ┗┓┓┏━┳┓┏┛ + + + +
*    ┃┫┫ ┃┫┫
*    ┗┻┛ ┗┻┛+ + + +
*/

猜你喜欢

转载自blog.csdn.net/tlyzxc/article/details/113776461