POJ

( ext{Description})

传送门

( ext{Solution})

首先建出一棵 ( ext{AC}) 自动姬,对于自动姬里的每个节点代表这个节点到根路径这样的字符串是进行到某种 ( ext{DNA}) 序列的后缀,那么对于某个节点,如果它的 (fail) 能跳到一个节点是某个病毒的最后一个字符,显然这个节点是不合法的。

注意对于某个节点的某种字符没有后继(这里还包含 (t[fail[u]][ch]) 也没有的情况),它就可以直接连向 (0) 了,表示重新开始一轮匹配,这里的 (0) 在那一串中相当于 (ch)

所以根据 (i) 能到 (j) 建出一个转移矩阵 (per)。那么我们的初始矩阵 (ori)(显然起点是 (0)):

[left[ egin{matrix} 1 & 0 &...&0 end{matrix} ight] ]

运算就是 (A=ori imes per^n),然后答案就是 (sum_{i=0}^{tot} A[0][i])

但是由于 (ori) 的特殊性,其实可以直接计算 (sum_{i=0}^{tot} per[0][i])

( ext{Code})

#include <cstdio>

#define rep(i,_l,_r) for(register signed i=(_l),_end=(_r);i<=_end;++i)
#define fep(i,_l,_r) for(register signed i=(_l),_end=(_r);i>=_end;--i)
#define erep(i,u) for(signed i=head[u],v=to[i];i;i=nxt[i],v=to[i])
#define efep(i,u) for(signed i=Head[u],v=to[i];i;i=nxt[i],v=to[i])
#define print(x,y) write(x),putchar(y)

template <class T> inline T read(const T sample) {
    T x=0; int f=1; char s;
    while((s=getchar())>'9'||s<'0') if(s=='-') f=-1;
    while(s>='0'&&s<='9') x=(x<<1)+(x<<3)+(s^48),s=getchar();
    return x*f;
}
template <class T> inline void write(const T x) {
    if(x<0) return (void) (putchar('-'),write(-x));
    if(x>9) write(x/10);
    putchar(x%10^48);
}
template <class T> inline T Max(const T x,const T y) {if(x>y) return x; return y;}
template <class T> inline T Min(const T x,const T y) {if(x<y) return x; return y;}
template <class T> inline T fab(const T x) {return x>0?x:-x;}
template <class T> inline T gcd(const T x,const T y) {return y?gcd(y,x%y):x;}
template <class T> inline T lcm(const T x,const T y) {return x/gcd(x,y)*y;}
template <class T> inline T Swap(T &x,T &y) {x^=y^=x^=y;}

#include <queue>
#include <cstring>
using namespace std;

const int mod=100000,maxn=105;

int m,n,mp[30],tot,t[maxn][4],fa[maxn],ans;
queue <int> q;
bool no[maxn];
char s[16];
struct Matrix {
	int a[maxn][maxn];
	
	Matrix() {memset(a,0,sizeof a);}
	
	Matrix operator * (Matrix t) {
		Matrix r;
		rep(i,0,tot)
			rep(j,0,tot)
				if(a[i][j])
					rep(k,0,tot)
						r.a[i][k]=(r.a[i][k]+1ll*a[i][j]*t.a[j][k]%mod)%mod;
		return r;
	}
} per;

void Insert() {
	int p=0,len=strlen(s+1);
	rep(i,1,len) {
		int d=mp[s[i]-'A'];
		if(!t[p][d]) t[p][d]=++tot;
		p=t[p][d];
	}
	no[p]=1;
}

void GetFail() {
	rep(i,0,3) if(t[0][i]) q.push(t[0][i]),fa[t[0][i]]=0;
	while(!q.empty()) {
		int u=q.front(); q.pop();
		rep(i,0,3) 
			if(t[u][i]) fa[t[u][i]]=t[fa[u]][i],q.push(t[u][i]),no[t[u][i]]|=no[t[fa[u]][i]];
			else t[u][i]=t[fa[u]][i];
	}
}

void init() {
	rep(i,0,tot) {
		if(no[i]) continue;
		rep(j,0,3)
			if(!no[t[i][j]])
				++per.a[i][t[i][j]];
	}
}

Matrix qkpow(int y) {
	Matrix r; 
	rep(i,0,tot) r.a[i][i]=1;
	while(y) {
		if(y&1) r=r*per;
		per=per*per; y>>=1;
	}
	return r;
} 

signed main() {
	mp[0]=0,mp['C'-'A']=1,mp['G'-'A']=2,mp['T'-'A']=3;
	m=read(9),n=read(9);
	while(m--) {
		scanf("%s",s+1);
		Insert();
	}
	GetFail(); init();
	per=qkpow(n);
	rep(i,0,tot)
		ans=(ans+per.a[0][i])%mod;
	print(ans,'
');
	return 0;
}
原文地址:https://www.cnblogs.com/AWhiteWall/p/14123750.html