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. uboot启动阶段修改启动参数方法及分析

    作者:围补 本来启动方式这节不是什么复杂的事儿,不过想简单的说清楚明白,还真是不知道怎么组织.毕竟文字跟有声语言表达有别.但愿简单的东西别让我讲的太复杂! Arm板系统文件一般有三个——bootloa ...

  2. 【转】Acm之java速成

    这里指的java速成,只限于java语法,包括输入输出,运算处理,字符串和高精度的处理,进制之间的转换等,能解决OJ上的一些高精度题目. 1. 输入:格式为:Scanner cin = new Sca ...

  3. 安卓SDK更新host文件地址

    之前在安装jdk时,安装进度一直很缓慢,在更新这个(两个任选,都有效)host文件地址后,瞬间就可以了. 203.208.46.146 dl.google.com 203.208.46.146 dl- ...

  4. c++ cin>>详解

    参考地址:http://www.cnblogs.com/A-Song/archive/2012/01/29/2331204.html 程序的输入都建有一个缓冲区,即输入缓冲区.一次输入过程是这样的,当 ...

  5. 如何让HTML在手机上实现直接拨打电话以及发送短信?

    拨打电话的HTML实现方式: <a href="tel:134289210xx″>拨打电话</a> 上面是比较常用的方式,但是有可能在某些场景下是支持不太好,可以试用 ...

  6. Thread Dump 和Java应用诊断(转)

    Thread Dump 和Java应用诊断 Thread Dump是非常有用的诊断Java应用问题的工具,每一个Java虚拟机都有及时生成显示所有线程在某一点状态的thread-dump的能力.虽然各 ...

  7. 全栈project师的毁与誉

    全栈(Full Stack)project师.也能够叫全端project师,不管是前端知识,还是后端架构你都要了解.甚至有些调皮的程序猿这样理解全栈project师:全栈project师 = 屌丝战斗 ...

  8. UNIX网络编程卷1 server程序设计范式7 预先创建线程,以相互排斥锁上锁方式保护accept

    本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie 1.预先创建一个线程池.并让每一个线程各自调用 accept 2.用相互排斥锁代替让每一个线 ...

  9. 状态压缩dp(hdu2167,poj2411)

    hdu2167 http://acm.hdu.edu.cn/showproblem.php?pid=2167 给定一个N*N的板子,里面有N*N个数字,选中一些数字,使得和最大 要求任意两个选中的数字 ...

  10. Red Gate系列之六 SQL Test 1.0.12.3 Edition SQL测试工具 完全破解+使用教程

    原文:Red Gate系列之六 SQL Test 1.0.12.3 Edition SQL测试工具 完全破解+使用教程 Red Gate系列之六 SQL Test 1.0.12.3 Edition S ...