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. Linux下OpenCV的环境搭建(转)

    OpenCV is the most popular and advanced code library for Computer Vision related applications today, ...

  2. Service组件 总结 + 绑定理Service三种实现方式 Messager + Binder + AIDL

    在Android中进程按优先级可以分为五类,优先级从高到低排列: - 前台进程 该进程包含正在与用户进行交互的界面组件,比如一个Activity - 可视进程 该进程中的组件虽然没有和用户交互,但是仍 ...

  3. Android之Http通信——3.Android HTTP请求方式:HttpURLConnection

    3.Android HTTP请求方式之HttpURLConnection 引言: 好了,前两节我们已经对HTTP协议进行了学习.相信看完前两节的朋友对HTTP协议相比之前 应该更加熟悉吧.好吧.学了要 ...

  4. hdu5618 (三维偏序,cdq分治)

    给定空间中的n个点,问每个点有多少个点小于等于自己. 先来分析简单的二维的情况,那么只要将x坐标排序,那么这样的问题就可以划分为两个子问题,,这样的分治有一个特点,即前一个子问题的解决是独立的,而后一 ...

  5. MySQL 改动用户password及重置rootpassword

    为数据库用户改动password是DBA比較常见的工作之中的一个.对于MySQL用户账户的password改动,有几种不同的方式.推荐的方式使用加密函数来改动password. 本文主要描写叙述了通过 ...

  6. C语言cgi(1)

    1Columbia Universitycs3157 – Advanced ProgrammingSummer 2014, Lab #2, 60ish pointsJune 9, 2014Follow ...

  7. 元素z-index继承问题

    两同层元素.当中一个的子元素定位与还有一个重叠时,该子元素被覆盖(此时子元素的z-index继承的是其父元素的z-index,不管其z-index多少均被覆盖) <html> <he ...

  8. 佛祖保佑 永无BUG 永不改动

    //////////////////////////////////////////////////////////////////// // _ooOoo_ // // o8888888o // / ...

  9. unity3d 数学的数学基础和辅助类

    转载注明smartdot:http://my.oschina.net/u/243648/blog/67193 1.  数学(点乘/叉乘)/unity3d的数学辅助类 2.  坐标系统(本地/世界/屏幕 ...

  10. Javadoc的Html文件传输chm

     Javadoc的Html文件转chm 工具下载地址:http://msdn.microsoft.com/en-us/library/ms669985.aspx 两篇相关文章: MyEclipse ...