dp[i][j] 它表示的长度 i 下游前缀 j 更改节点的最小数量。

很清楚dp[0][0] = 0;

dp[ i ][ j ] = min(dp[ i ][ j ],dp[i-1][k] + (j == k ?

0 : 1)),当且仅当j。k满足下列条件时。

j 不为某条模式串的末节点 且 j 到 root 的由失败指针组成的路径上无末节点。

j 是k的儿子节点 或者 j 的父节点可由 k 沿着失败指针找到。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <cmath>
#include <stack>
#include <map> #pragma comment(linker, "/STACK:1024000000");
#define EPS (1e-8)
#define LL long long
#define ULL unsigned long long
#define _LL __int64
#define INF 0x3f3f3f3f using namespace std; const int MAXN = 4; struct N
{
int next[MAXN],flag,fail;
} st[1010]; int Top; int sel(char c)
{
if(c == 'A')
return 0;
if(c == 'G')
return 1;
if(c == 'C')
return 2;
return 3;
} int creat()
{
memset(st[Top].next,-1,sizeof(st[Top].next));
st[Top].fail = -1,st[Top].flag = 0;
return Top++;
} int dp[1010][1010]; char s[1010]; void Get_Trie(int root,char *s)
{
int site = 1; while(s[site] != '\0')
{
if(st[root].next[sel(s[site])] == -1)
st[root].next[sel(s[site])] = creat();
root = st[root].next[sel(s[site])];
++site;
} st[root].flag = 1;
} int Get_Fail(int site,int tar)
{
while(site != -1 && st[site].next[tar] == -1)
site = st[site].fail;
if(site == -1)
return 0;
return st[site].next[tar];
} queue<int> q; void Get_Fail(int root)
{
q.push(root); st[root].fail = -1; int f; while(q.empty() == false)
{
f = q.front();
q.pop(); for(int i = 0; i < MAXN; ++i)
{
if(st[f].next[i] != -1)
{
st[st[f].next[i]].fail = Get_Fail(st[f].fail,i);
q.push(st[f].next[i]);
}
}
}
} bool Is_Safe(int site)
{
if(site == -1)
return true;
if(st[site].flag || Is_Safe(st[site].fail) == false)
return false;
return true;
} int Is_Safe(int site,int tar)
{ if(site == -1)
return 0;
if(st[site].next[tar] != -1)
{
if(st[st[site].next[tar]].flag != 0 || Is_Safe(st[site].next[tar]) == false)
return -1;
return st[site].next[tar];
}
return Is_Safe(st[site].fail,tar);
} void Match(int root,char *s)
{
memset(dp,INF,sizeof(dp));
dp[0][0] = 0; int site,i,j,tmp; for(site = 1; s[site] != '\0'; ++site)
{
for(i = 0; i < Top; ++i)
{
if(dp[site-1][i] == INF)
continue; for(j = 0; j < 4; ++j)
{
if(st[i].next[j] != -1 && st[st[i].next[j]].flag != 0)
continue;
if(st[i].next[j] != -1 && Is_Safe(st[i].next[j]))
{
dp[site][st[i].next[j]] = min(dp[site][st[i].next[j]],dp[site-1][i] + (j == sel(s[site]) ? 0 : 1));
continue;
} tmp = Is_Safe(i,j); if(tmp == -1)
continue;
dp[site][tmp] = min(dp[site][tmp],dp[site-1][i] + (j == sel(s[site]) ? 0 : 1));
}
}
}
} int main()
{
int i,n; int icase = 1; int root; while(scanf("%d",&n) && n)
{
Top = 0;
root = creat(); for(int i = 1; i <= n; ++i)
{
scanf("%s",s+1);
Get_Trie(root,s);
} Get_Fail(root); scanf("%s",s+1); Match(root,s); int anw = INF,len = strlen(s+1); for(i = 0; i < Top; ++i)
anw = min(anw,dp[len][i]);
printf("Case %d: %d\n",icase++,anw == INF ? -1 : anw);
} return 0;
}

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

POJ 3691 DNA repair 基于AC自己主动机DP的更多相关文章

  1. poj 3691 DNA repair(AC自己主动机+dp)

    DNA repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5877   Accepted: 2760 Descri ...

  2. POJ 2778 DNA Sequence (AC自己主动机 + dp)

    DNA Sequence 题意:DNA的序列由ACTG四个字母组成,如今给定m个不可行的序列.问随机构成的长度为n的序列中.有多少种序列是可行的(仅仅要包括一个不可行序列便不可行).个数非常大.对10 ...

  3. Hdu 3341 Lost&#39;s revenge (ac+自己主动机dp+hash)

    标题效果: 举个很多种DNA弦,每个字符串值值至1.最后,一个长字符串.要安排你最后一次另一个字符串,使其没事子值和最大. IDEAS: 首先easy我们的想法是想搜索的!管她3721..直接一个字符 ...

  4. HDU - 2825 Wireless Password(AC自己主动机+DP)

    Description Liyuan lives in a old apartment. One day, he suddenly found that there was a wireless ne ...

  5. hdu4758 Walk Through Squares (AC自己主动机+DP)

    Walk Through Squares Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others ...

  6. hdu4057 Rescue the Rabbit(AC自己主动机+DP)

    Rescue the Rabbit Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

  7. HDU - 4758 Walk Through Squares (AC自己主动机+DP)

    Description   On the beaming day of 60th anniversary of NJUST, as a military college which was Secon ...

  8. POJ 3691 DNA repair (DP+AC自动机)

    DNA repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4815   Accepted: 2237 Descri ...

  9. HDU 2457/POJ 3691 DNA repair AC自动机+DP

    DNA repair Problem Description   Biologists finally invent techniques of repairing DNA that contains ...

随机推荐

  1. 项目中那些事|ListView中嵌套ListView问题

    要在一个ListView中放入另一个ListView,也即在一个ListView的每个 item 中放入另外一个ListView.但刚开始的时候,会发现放入的子ListView会显示不完全(我这里只显 ...

  2. 数学之路-python计算实战(19)-机器视觉-卷积滤波

    filter2D Convolves an image with the kernel. C++: void filter2D(InputArray src, OutputArray dst, int ...

  3. c#怎样获取excel单元格的RGB颜色

    这段时间一直在做office的工作.前2天获取单元格的颜色的问题一直没搞明确. 開始我想用的就是Npoi.主要前一部分的工作都是用Npoi完毕的 row.GetCell(j).CellStyle.Fi ...

  4. 使用NFS安装oracle软件

    昨天.使用openfiler创建nas存储系统,安装oracle软件,在所有正面最好,要创建一个数据库时报ora错,原来使用nfs安装oracle数据库,mount选项有特殊要求,如以下.以备查: R ...

  5. 采用Bash脚本性能监控过程

    为一个Linux过程监控,采用Bash脚本. 采用ps命令的过程监控,使用周期加上连续监测的睡眠时间. 使用方法: psmonitor.sh -p [pid] -d [interval] -n [st ...

  6. shell加法

    echo 1597+1469+1468+2591+1260+1068+1019+993|bc http://bbs.chinaunix.net/thread-161085-1-1.html http: ...

  7. ACM/ICPM2014鞍山现场赛D Galaxy (HDU 5073)

    题目链接:pid=5073">http://acm.hdu.edu.cn/showproblem.php?pid=5073 题意:给定一条线上的点,然后能够去掉当中的m个,使剩下的到重 ...

  8. zoj 3822 Domination (可能性DP)

    Domination Time Limit: 8 Seconds      Memory Limit: 131072 KB      Special Judge Edward is the headm ...

  9. 为应用程序池“XX”提供服务的进程在与 Windows Process Activation Service 通信时出现严重错误

    场景 WCF应用程序部署在IIS7中,使用net.tcp协议对外给几百台客户端提供服务,应用程序池不断崩溃重启. 分析过程 在事件查看器中看到的错误信息类似于 为应用程序池“XX”提供服务的进程在与 ...

  10. How to import the www.googleapis.com SSL CA certification to the jks store file?

    Assumed that you have installed JDK and configured JAVA_HOME for your current operation system. (1)  ...