文本生成器加强版。用矩阵加速。(一直tle居然是因为
node operator*(const node&o)const{
node tmp;
REP(i,0,pt) REP(j,0,pt) REP(k,0,pt) {
int temp=(ll)a[i][k]*o.a[k][j]%mod;
tmp.a[i][j]=(tmp.a[i][j]+temp)%mod;
}
return tmp;
}
tmp.a[i][j]=(tmp.a[i][j]+((ll)a[i][k]*o.a[k][j])%mod)%mod(TLE)
然而并不明白是为什么QAQ。ccz大爷说是longlong取模慢,而且强制把int转成了longlong。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define REP(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define clr(x,c) memset(x,c,sizeof(x))
#define ll long long
const int nmax=105;
const int mod=100000;
int ch[nmax][4],fail[nmax];
bool F[nmax];
char s[15];
int n,m,pt=0;
int id(char c){
switch(c){
case 'A':
return 0;break;
case 'C':
return 1;break;
case 'G':
return 2;break;
case 'T':
return 3;break;
}
}
void insert(char s[]){
int t=0,len=strlen(s);
for(int i=0;i<len;i++){
if(!ch[t][id(s[i])]) ch[t][id(s[i])]=++pt;
t=ch[t][id(s[i])];
}
F[t]=true;
}
queue<int>q;
void getfail(){
q.push(0);fail[0]=0;
while(!q.empty()){
int t=q.front();q.pop();
REP(i,0,3) {
if(ch[t][i]) q.push(ch[t][i]),fail[ch[t][i]]=t==0?0:ch[fail[t]][i];
else ch[t][i]=t==0?0:ch[fail[t]][i];
}
F[t]|=F[fail[t]];
}
}
struct node{
int a[nmax][nmax];
node(){
clr(a,0);
}
node operator*(const node&o)const{
node tmp;
REP(i,0,pt) REP(j,0,pt) REP(k,0,pt) {
int temp=(ll)a[i][k]*o.a[k][j]%mod;
tmp.a[i][j]=(tmp.a[i][j]+temp)%mod;
}
return tmp;
}
}a,b;
void getmatrix(){
REP(i,0,pt) a.a[i][i]=1;
REP(i,0,pt) REP(j,0,3) if(!F[ch[i][j]]) b.a[i][ch[i][j]]++;
while(m){
if(m&1) a=a*b;
b=b*b;m>>=1;
}
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
clr(fail,0);clr(ch,0);clr(F,0);pt=0;clr(a.a,0);clr(b.a,0);
REP(i,1,n) scanf("%s",s),insert(s);getfail();
getmatrix();
int ans=0;
REP(i,0,pt) ans=(ans+a.a[0][i])%mod;
printf("%d\n",ans);
}
return 0;
}
DNA Sequence
Description
It's well known that DNA Sequence is a sequence only contains A, C, T and G, and it's very useful to analyze a segment of DNA Sequence,For example, if a animal's DNA sequence contains segment ATC then it may mean that the animal may have a genetic disease. Until now scientists have found several those segments, the problem is how many kinds of DNA sequences of a species don't contain those segments.
Suppose that DNA sequences of a species is a sequence that consist of A, C, T and G,and the length of sequences is a given integer n. Input
First line contains two integer m (0 <= m <= 10), n (1 <= n <=2000000000). Here, m is the number of genetic disease segment, and n is the length of sequences.
Next m lines each line contain a DNA genetic disease segment, and length of these segments is not larger than 10. Output
An integer, the number of DNA sequences, mod 100000.
Sample Input 4 3 AT AC AG AA Sample Output 36 Source |