hdu 2457 DNA repair
AC自动机+DP。按着自动机跑,(其实是生成新的满足题目要求的串,然后找改变最少的。)但是不能跑到是单词的地方,如果跑到单词的话那么说明改变后的串含有病毒了,不满足题意。然后就是应该怎么跑的问题了,现在我们从自动机的根节点开始跑,如果跑到下一个节点和当前串的字母不一样的话,那么当前位置生成的串是和原串在该位置是有差异的,dp+1,否者的话dp不变。所以dp[ i ][ j ]表示的是匹配到当前匹配串的位置时,跑到自动机的 j 节点需要改变的最少字母数。
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <climits>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <cctype>
#include <queue>
#include <cmath>
#include <set>
#include <map>
#define CLR(a, b) memset(a, b, sizeof(a))
using namespace std; const int MAX_NODE = 22 * 55 * 2;
const int INF = 0x3f3f3f3f;
const int CHILD_NUM = 4;
const int N = 1010; class ACAutomaton
{
private:
int chd[MAX_NODE][CHILD_NUM];
int dp[N][MAX_NODE];
int fail[MAX_NODE];
bool val[MAX_NODE];
int Q[MAX_NODE];
int ID[128];
int sz;
public:
void Initialize()
{
fail[0] = 0;
ID['A'] = 0;ID['G'] = 1;
ID['C'] = 2;ID['T'] = 3;
}
void Reset()
{
CLR(chd[0] , 0);sz = 1;
}
void Insert(char *a)
{
int p = 0;
for ( ; *a ; a ++)
{
int c = ID[*a];
if (!chd[p][c])
{
CLR(chd[sz] , 0);
val[sz] = false;
chd[p][c] = sz ++;
}
p = chd[p][c];
}
val[p] = true;
}
void Construct()
{
int *s = Q , *e = Q;
for (int i = 0 ; i < CHILD_NUM ; i ++)
{
if (chd[0][i])
{
fail[ chd[0][i] ] = 0;
*e ++ = chd[0][i];
}
}
while (s != e)
{
int u = *s++;
for (int i = 0 ; i < CHILD_NUM ; i ++)
{
int &v = chd[u][i];
if (v)
{
*e ++ = v;
fail[v] = chd[ fail[u] ][i];
val[v] |= val[fail[v]];
}
else
{
v = chd[ fail[u] ][i];
}
}
}
}
int Work(char *ch)
{
int len, S, T, ret;
len = strlen(ch);
CLR(dp, INF);dp[0][0] = 0;
for(int i = 0; i < len; i ++)
for(int j = 0; j < sz; j ++)
{
if(val[j]) continue;
if(dp[i][j] == INF) continue;
for(int k = 0; k < 4; k ++)
{
T = chd[j][k];
if(val[T]) continue;
dp[i + 1][T] = min(dp[i + 1][T], dp[i][j] + (ID[ch[i]] != k));
}
}ret = INF;
for(int i = 0; i < sz; i ++)
{
ret = min(ret, dp[len][i]);
}
return ret == INF ? -1 : ret;
}
} AC; char ch[N]; int main()
{
//freopen("input.txt", "r", stdin);
AC.Initialize();
int n, t, cas = 1;
while (scanf("%d", &n), n)
{
AC.Reset();
for (int i = 0 ; i < n ; i ++)
{
char temp[55];
scanf("%s", temp);
AC.Insert(temp);
}
scanf("%s", ch);
AC.Construct();
printf("Case %d: %d\n", cas ++, AC.Work(ch));
}
return 0;
}
hdu 2457 DNA repair的更多相关文章
- HDU 2457 DNA repair(AC自动机+DP)题解
题意:给你几个模式串,问你主串最少改几个字符能够使主串不包含模式串 思路:从昨天中午开始研究,研究到现在终于看懂了.既然是多模匹配,我们是要用到AC自动机的.我们把主串放到AC自动机上跑,并保证不出现 ...
- POJ 3691 & HDU 2457 DNA repair (AC自己主动机,DP)
http://poj.org/problem?id=3691 http://acm.hdu.edu.cn/showproblem.php?pid=2457 DNA repair Time Limit: ...
- HDU 2457 DNA repair (AC自动机+DP)
题意:给N个串,一个大串,要求在最小的改变代价下,得到一个不含上述n个串的大串. 思路:dp,f[i][j]代表大串中第i位,AC自动机上第j位的最小代价. #include<algorithm ...
- Hdu 2457 DNA repair (ac自己主动机+dp)
题目大意: 改动文本串的上的字符,使之不出现上面出现的串.问最少改动多少个. 思路分析: dp[i][j]表示如今 i 个字符改变成了字典树上的 j 节点. 然后顺着自己主动机一直转移方程. 注意合法 ...
- HDU 2425 DNA repair (AC自动机+DP)
DNA repair Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- AC自动机+DP HDOJ 2457 DNA repair(DNA修复)
题目链接 题意: 给n串有疾病的DNA序列,现有一串DNA序列,问最少修改几个DNA,能使新的DNA序列不含有疾病的DNA序列. 思路: 构建AC自动机,设定end结点,dp[i][j]表示长度i的前 ...
- HDU 2457/POJ 3691 DNA repair AC自动机+DP
DNA repair Problem Description Biologists finally invent techniques of repairing DNA that contains ...
- hdu2457:DNA repair
AC自动机+dp.问改变多少个字符能让目标串不含病毒串.即走过多少步不经过病毒串终点.又是同样的问题. #include<cstdio> #include<cstring> # ...
- 【POJ3691】 DNA repair (AC自动机+DP)
DNA repair Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Description B ...
随机推荐
- javascript取url参数的几种方法
//获取QueryString的数组 function getQueryString() { var result = location.search.match(new RegExp("[ ...
- java 整体字体样式设置
两种方式: 1.UIManager.put("Button.font", new Font("MS UI Gothic", Font.PLAIN, 24)) ...
- php生成图片验证码
验证码主要用来防止暴力破解的方式来进行登录,下面是php中的图片验证码生成方式,废话不多说,直接上代码 /** * 生成验证码 */ function buildRandomString($type= ...
- 【读书笔记】【CLR via C#】【第一章】The CLR’s Execution Model
内容提要 本章的目的是对.Net 框架的设计做一个总体的介绍,包括介绍框架中使用的一些技术.定义一些术语.同时会展示从源代码生成应用程序(或者一些包含了一些自定义类型的可以发布的组件),并且会解释程序 ...
- linux下删除内核
一.概述 笔者的Ubuntu系统刚安装成功后,就不知道怎么会有多个内核,但实际上默认运行的只有一个.在grub启动界面多余的启动项和多余内核占用的存储空间迫使我产生了铲除多余内核的冲动. 最近,自己从 ...
- hadoop 学习笔记 (十) mapreduce2.0
MapReduce的特色---不擅长的方面 >实时计算 像mysql一样,在毫秒级或者秒级内返回结果 >流式计算 Mapreduce的输入数据时静态的,不能动态变化 MapReduce自身 ...
- css学习笔记一
1.在css开头用* {margin:0;padding:0;}可以清除所有样式 2.在css中table,th,td {padding:0;}效果等同于cellpadding="0″. 3 ...
- 从 mian 函数开始一步一步分析 nginx 执行流程(二)
如不做特殊说明,本博客所使用的 nginx 源码版本是 1.0.14,[] 中是代码所在的文件! 上一个博客中我们将 main 函数执行流程分析完,到最后一步调用 ngx_master_process ...
- bzoj 1188 [HNOI2007]分裂游戏(SG函数,博弈)
1188: [HNOI2007]分裂游戏 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 733 Solved: 451[Submit][Status ...
- 解压华为P6 UPDATE.APP
#!/usr/bin/env python # Version: 0.2.201308040830 # Author: linkscue # Function: unpack any hauwei h ...