题意:
计算不包含病毒串的串的个数,长度n<20亿。
题解:
没法用dp啊。矩阵加速,这个应用了矩阵的性质。矩阵n次幂,对应的点ij表示i经过n个节点到j路径的个数。
被各种卡时间啊,结果发现是自己的矩阵乘法写残了。
#include<iostream>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
#include<queue>
#include<map>
#include<set>
using namespace std;
#define B(x) (1<<(x))
typedef __int64 ll;
const int oo=0x3f3f3f3f;
const ll OO=1LL<<61;
const int MOD=100000;
const int maxn=1005;
const int SIZE=105;
const int type=4;
char str[15];
map<char,int>mat;
int n;
struct Matrix
{
int maze[110][110],n;
Matrix(){}
Matrix(int _n)
{
n = _n;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
maze[i][j]=0;
}
Matrix operator *(const Matrix &b)const
{
Matrix ret=Matrix(n);
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
for(int k=0;k<n;k++)
{
int tmp=(long long)maze[i][k]*b.maze[k][j]%MOD;
ret.maze[i][j]=(ret.maze[i][j]+tmp)%MOD;
}
return ret;
}
Matrix operator^(int k)
{
Matrix ans(this->n);
Matrix a=*this;
for(int i=0;i<n;i++) ans.maze[i][i]=1;
while(k)
{
if(k&1)
ans=ans*a;
a=a*a;
k>>=1;
}
return ans;
}
};
struct ACautomaton
{
int next[SIZE][type],fail[SIZE],word[SIZE];
int cnt,root;
int newNode()
{
for(int i=0;i<type;i++)
next[cnt][i]=-1;
word[cnt++]=0;
return cnt-1;
}
void Init()
{
cnt=0;
root=newNode();
}
void Insert(char buff[])
{
int now=root;
int len=strlen(buff);
for(int i=0,k;i<len;i++)
{
k=mat[buff[i]];
if(next[now][k]==-1)
next[now][k]=newNode();
now=next[now][k];
}
word[now]=1;
}
void build()
{
fail[root]=root;
int now=root;
queue<int>Q;
for(int i=0;i<type;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();
if(word[fail[now]]==1) word[now]=1;
for(int i=0;i<type;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]);
}
}
}
}
ll quick_pow(int t)
{
//n=cnt;
Matrix a(cnt);
for(int i=0;i<cnt;i++)
for(int j=0;j<type;j++)
{
int k=next[i][j];
if(!word[k])
a.maze[i][k]++;
}
a=a^t;
ll ans=0;
for(int i=0;i<cnt;i++)
ans=(ans+a.maze[0][i]+MOD)%MOD;
return ans;
}
}ac;
int main()
{
int M,N;
mat['A']=0;mat['G']=1;mat['C']=2;mat['T']=3;
while(scanf("%d %d",&M,&N)!=EOF)
{
ac.Init();
for(int i=1;i<=M;i++)
{
scanf("%s",str);
ac.Insert(str);
}
ac.build();
printf("%I64d\n",ac.quick_pow(N));
}
return 0;
}
/*
4 3
AT
AC
AG
AA
*/