Hdu 3962 Microgene (AC自己主动机+矩阵)

时间:2022-12-27 23:43:43

标题效果:

构造一个字符串,使得有两个和两个以上的目标串。长短L这一系列有多少串都。

IDEAS:

只有全款减有1一些字符串,没有目标就是答案。

假定数据是非常小的,够用dp解。dp[i][j][k] 表示长度为i,走到自己主动机的j。有k个目标串的数量。

转移便是。

if(j->next[d] ->isword) dp[i+1][j->next][1] += dp[i][j][0].

else dp[i+1][j->next][0]+=dp[i][j][0],dp[i+1][j->next][1] += dp[i][j][1]...

如今长度达到百万。

所以用矩阵优化。

设自己主动机的节点数量为idx,那么就开一个(2*idx。2*idx)的矩阵。

假设i<idx j<idx 表示 開始在i的时候没有目标串,走到j也没有。

假设i<idx j>idx 表示 開始在i的时候没有目标串。走到j有了一个。

后面同理。

。。

那么构造这个矩阵便是依照上面的dp方程类似构造。

if(j->next[d]->isword)matrix [i][j->next->index+idx]++; 開始的时候没有,走过来加一个

else matrix [i][j]++,matrix [i+idx][j+idx] 開始的时候没有,走到j也没有  和  開始的时候有一个,走到j还是一个。

矩阵的构造是难= =

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define N 75
using namespace std; const int mod = 10007;
const char tab = 'a';
const int max_next = 4;
int rev[256];
struct trie
{
struct trie *fail;
struct trie *next[max_next];
int isword;
int index;
};
struct AC
{
trie *que[100005],*root,ac[100005];
int head,tail;
int idx;
trie *New()
{
trie *temp=&ac[idx];
for(int i=0;i<max_next;i++)temp->next[i]=NULL;
temp->fail=NULL;
temp->isword=0;
temp->index=idx++;
return temp;
}
void init()
{
idx=0;
root=New();
}
void Insert(trie *root,char *word,int len){
trie *t=root;
for(int i=0;i<len;i++){
if(t->next[rev[word[i]]]==NULL)
t->next[rev[word[i]]]=New();
t=t->next[rev[word[i]]];
}
t->isword++;
}
void acbuild(trie *root){
int head=0,tail=0;
que[tail++]=root;
root->fail=NULL;
while(head<tail){
trie *temp=que[head++],*p;
for(int i=0;i<max_next;i++){
if(temp->next[i]){
if(temp==root)temp->next[i]->fail=root;
else {
p=temp->fail;
while(p!=NULL){
if(p->next[i]){
temp->next[i]->fail=p->next[i];
break;
}
p=p->fail;
}
if(p==NULL)temp->next[i]->fail=root;
}
if(temp->next[i]->fail->isword)temp->next[i]->isword++;
que[tail++]=temp->next[i];
}
else if(temp==root)temp->next[i]=root;
else temp->next[i]=temp->fail->next[i];
}
}
}
void tra()
{
for(int i=0;i<idx;i++)
{
if(ac[i].fail!=NULL)printf("fail = %d ",ac[i].fail->index);
for(int k=0;k<max_next;k++)
printf("%d ",ac[i].next[k]->index);
puts("");
}
}
}sa; struct matrix
{
int r,c;
int data[N][N];
matrix(){}
matrix(int _r,int _c):r(_r),c(_c){memset(data,0,sizeof data);}
friend matrix operator * (const matrix A,const matrix B)
{
matrix res;
res.r=A.r;res.c=B.c;
memset(res.data,0,sizeof res.data);
for(int i=0;i<A.r;i++)
{
for(int j=0;j<B.c;j++)
{
for(int k=0;k<A.c;k++)
{
if(A.data[i][k] && B.data[k][j]){
res.data[i][j]+=A.data[i][k]*B.data[k][j];
res.data[i][j]%=mod;
}
}
}
}
return res;
}
friend matrix operator + (const matrix A,const matrix B)
{
matrix res;
res.r=A.r;res.c=A.c;
memset(res.data,0,sizeof res.data);
for(int i=0;i<A.r;i++)
{
for(int j=0;j<A.c;j++)
{
res.data[i][j]=A.data[i][j]+B.data[i][j];
res.data[i][j]%=mod;
}
}
return res;
}
friend matrix operator - (const matrix A,const matrix B)
{
matrix res;
res.r=A.r;res.c=A.c;
memset(res.data,0,sizeof res.data);
for(int i=0;i<A.r;i++)
{
for(int j=0;j<A.c;j++)
{
res.data[i][j]=A.data[i][j]-B.data[i][j];
res.data[i][j]=(res.data[i][j]%mod+mod)%mod;
}
}
return res;
}
friend matrix operator ^ (matrix A,int n)
{
matrix res;
res.r=A.r;res.c=A.c;
memset(res.data,0,sizeof res.data);
for(int i=0;i<A.r;i++)res.data[i][i]=1; while(n)
{
if(n&1)res=res*A;
A=A*A;
n>>=1;
}
return res;
}
void print()
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
printf("%d ",data[i][j]);
puts("");
}
}
}E,zero; char word[30]; int main()
{
rev['A']=0;
rev['C']=1;
rev['G']=2;
rev['T']=3;
int n,L;
while(scanf("%d%d",&n,&L)!=EOF)
{
sa.init();
for(int i=1;i<=n;i++)
{
scanf("%s",word);
sa.Insert(sa.root,word,strlen(word));
}
sa.acbuild(sa.root);
E=matrix(sa.idx*2,sa.idx*2); for(int i=0;i<sa.idx*2;i++)E.data[i][i]=1; matrix origin=matrix(sa.idx*2,sa.idx*2); for(int i=0;i<sa.idx;i++)
{
for(int j=0;j<4;j++)
{
int temp=sa.ac[i].next[j]->index;
if(sa.ac[i].next[j]->isword)
origin.data[i][temp+sa.idx]++;
else
origin.data[i][temp]++,origin.data[i+sa.idx][temp+sa.idx]++;
}
}
origin.print(); origin=origin^L; int ans=1;
int x=4;
int t=L;
while(t)
{
if(t&1)ans=(ans*x)%mod;
x=(x*x)%mod;
t>>=1;
}
for(int i=0;i<2*sa.idx;i++)
{
ans-=origin.data[0][i];
ans=(ans+mod)%mod;
} printf("%d\n",ans);
}
return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

Hdu 3962 Microgene (AC自己主动机+矩阵)的更多相关文章

  1. poj 2778 AC自己主动机 &plus; 矩阵高速幂

    // poj 2778 AC自己主动机 + 矩阵高速幂 // // 题目链接: // // http://poj.org/problem?id=2778 // // 解题思路: // // 建立AC自 ...

  2. Hdu 2243 考研路茫茫——单词情结 (AC自己主动机&plus;矩阵)

    哎哟喂.中文题. . .不说题意了. 首先做过POJ 2778能够知道AC自己主动机是能够求出长度为L的串中不含病毒串的数量的. POJ 2778的大概思路就是先用全部给的病毒串建一个AC自己主动机. ...

  3. hdu 2243 考研绝望——复杂的文字&lpar;AC自己主动机&plus;矩阵高速功率&rpar;

    pid=2243" target="_blank" style="">题目链接:hdu 2243 考研路茫茫--单词情结 题目大意:略. 解题思 ...

  4. POJ 2778 AC自己主动机&plus;矩阵幂 不错的题

    http://poj.org/problem?id=2778 有空再又一次做下,对状态图的理解非常重要 题解: http://blog.csdn.net/morgan_xww/article/deta ...

  5. POJ 3691 &amp&semi;amp&semi; HDU 2457 DNA repair &lpar;AC自己主动机,DP&rpar;

    http://poj.org/problem?id=3691 http://acm.hdu.edu.cn/showproblem.php?pid=2457 DNA repair Time Limit: ...

  6. hdu 4057 AC自己主动机&plus;状态压缩dp

    http://acm.hdu.edu.cn/showproblem.php?pid=4057 Problem Description Dr. X is a biologist, who likes r ...

  7. HDU 2825 Wireless Password &lpar;AC自己主动机,DP&rpar;

    pid=2825">http://acm.hdu.edu.cn/showproblem.php? pid=2825 Wireless Password Time Limit: 2000 ...

  8. HDU 2896 病毒侵袭 &lpar;AC自己主动机&rpar;

    pid=2896">http://acm.hdu.edu.cn/showproblem.php?pid=2896 病毒侵袭 Time Limit: 2000/1000 MS (Java ...

  9. &lbrack;AC自己主动机&plus;可能性dp&rsqb; hdu 3689 Infinite monkey theorem

    意甲冠军: 给n快报,和m频率. 然后进入n字母出现的概率 然后给目标字符串str 然后问m概率倍的目标字符串是敲数量. 思维: AC自己主动机+可能性dp简单的问题. 首先建立trie图,然后就是状 ...

随机推荐

  1. 一款bootstrap树形js

    http://www.htmleaf.com/Demo/201502141380.html

  2. 3&period;C&num;中泛型类的进一步探讨

    阅读目录 一:多重泛型  class不仅可以有T,还可以有K,实例化的时候传多个数据类型的类型,C#集合类型中的Dictionary就是多重泛型 using System; using System. ...

  3. sqlite 下载的 ZIP 包的区别

    https://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki sqlite-netFx20-binary-bundle- ...

  4. windows环境下svn同步web文件&lbrack;转&rsqb;

    windows环境下svn同步web文件 SVN在团队开发中使用非常普遍,是一个很方便的版本控制系统. 如果要是能将SVN服务器上的数据自动发布到Web服务器,那将是整个项目开发.测试更加便捷.利用S ...

  5. spring计划任务,springMvc计划任务,Spring&commat;Scheduled,spring定时任务

    spring计划任务,springMvc计划任务,Spring@Scheduled,spring定时任务 >>>>>>>>>>>&gt ...

  6. Java 的性能优化

    jvm 中的方法区: 永久区---存的类的信息.方法.常量 .静态变量 1.Java的性能优化 1.减少gc的压力,优先级比较低的线程,他是一个守护线程 回收我们的堆内存. 2.尽量的避免我们的new ...

  7. Android P新功能特性抢先看

    2018年3月8日,Google推出了Android P Preview版本,并提供官方镜像下载. 为了让广大开发者能够及时了解Android P的新功能特性,提前为您的app进行良好适配,WeTes ...

  8. vi十六进制编辑

    指定行:n 光标行之前或之后的n个字符nl 之后 2l 光标位置两个字符后nh 之前 2h 光标位置两个字符前 光标行之上或之下的n个字符nk 之上 1k 光标位置1个字符之上nj 之下 1j 光标位 ...

  9. 100-days&colon; sixteen

    Title: The world's most expensive cities 生活成本最高的城市 For the first time in its 30-year history, the Wo ...

  10. 学习笔记 python 面向对象学习

    封装: 封装是面向对象的特征之一,是对象和类概念的主要特性. 封装,也就是把客观事物封装成抽象的类,并且类可以把自己的数据和方法只让可信的类或者对象操作,对不可信的进行信息隐藏. 继承: 继承是指这样 ...