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. SE 2014年4月4日

    如图OSPF自治系统中有4个区域,要求如图配置使得中所有网络均能够相互访问为了网络安全及优化网络性能: 使用ospf实现全网互通: [RT1]ospf 1 router-id 1.1.1.1 [RT1 ...

  2. hdu4679(树形dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4679 题意:给一棵树,每条边上都有一个权值,去掉树上任意一条边之后,分成两个子树,两个子树的最长路与这 ...

  3. Android 监听SMS短信

    当设备接收到一条新的SMS消息时,就会广播一个包括了android.provider.Telephony.SMS_RECEIVED动作的Intent. 注意,这个动作是一个字符串值,SDK 1.0不再 ...

  4. Android开展:ADT+Eclipse使用错误:Text editor does not have a document provider

    Eclipse参加Android sdk源代码 正在使用Eclipse进行Android开发时间,我们经常需要导入sdk源代码来Eclipse中,方便api阅读和查询,详细操作为:ctrl+鼠标左键. ...

  5. JAVA 计算地球上任意两点(经纬度)距离

    /** * 计算地球上任意两点(经纬度)距离 * * @param long1 * 第一点经度 * @param lat1 * 第一点纬度 * @param long2 * 第二点经度 * @para ...

  6. 关于SVN配置文件的一个小例子

    1   背景假设 厦门央瞬公司是一家电子元器件设备供应商,其中有个ARM部门,专门负责ARM芯片的方案设计.销售,并在北京.上海各设立了一个办事处.对于工作日志,原先采用邮件方式发给经理,但是这种方式 ...

  7. Lichee(两) 在sun4i_crane该平台下编译

    让我们先来回顾一下编译命令 $ cd workdir/lichee $ ./build.sh -p sun4i_crane -k 3.0  lichee文件夹下的build.sh #!/bin/bas ...

  8. c++ 按行读取txt文本

    CStdioFile 类的声明保存在 afx.h 头文件中. CStdioFile 类继承自 CFile 类, CStdioFile 对象表示一个用运行时的函数 fopen 打开的 c 运行时的流式文 ...

  9. 【Android Training - UserInfo】记住登入用户的信息[Lesson 1 - 使用AccountManager来记住用户]

    Remembering Your User[记住你的用户] 每一个人都非常喜欢自己的名字能被人记住.当中最简单,最有效的使得你的app让人喜欢的方法是记住你的用户是谁,特别是当用户升级到一台新的设备或 ...

  10. 一类斜率优化的dp(特有性质:只能连续,不能交叉)

    hdu3480 给定一个有n个数的集合,将这个集合分成m个子集,要求子集的并等于全集求花费最小. 花费为该子集的(最大数-最小数)的平方. 我们将n个数排序, a < b < c < ...