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学习心得(十)
Ajax Ajax是浏览器中使用JavaScript进行服务器后台请求,读取附加信息或者导致服务器响应的过程. Ajax广泛用于从服务器读取数据,并用所得到的数据更新页面,以及向服务器发送数据 Aja ...
- __set($key,$values) 和__get($varName) 魔术方法设置读取私有属性
__set($key,$val) 对类内私有属性赋值 作用:对私有属性的处理 当在类外对类内的私有属性赋值时会自动调用此函数 __get($varName) 读取类内私有属性 作用:虽然可以外部访问, ...
- 一个c++给程序打log的单例模式类
开发过程中需要给程序打log. 所以照着网上写了个单例模式的log类 #ifndef MISCLOGWRITER_H_ #define MISCLOGWRITER_H_ #include <io ...
- hadoop 异常处理实例(一)hadoop内存配置项
Exception in thread "main" java.io.IOException: Job failed! at org.apache.hadoop.mapred.Jo ...
- iOS9 App Thinning(应用瘦身)功能介绍
iOS9 发布后,产生了一个使 App Thinning 无法正常运行的 bug.在iOS9.0.2 版本中,这个 bug 已经被修复,App Thinning 已经可以正常使用.当你从应用商店(Ap ...
- python处理LINUX的PWD文档
用冒号分隔的哟. 此章后面讲的JSON,配置文件读取,原理应该一样吧,只是要用合适的包去处理吧. CSV文档是用CSV包处理的. 文档: root:x:0:0:root:/root:/bin/bash ...
- Cxf soap协议改成1.2
在和.net做联调的时候,报错: A SOAP 1.2 message is not valid when sent to a SOAP 1.1 only endpoint. 看来是soap协议不匹配 ...
- 【HDOJ】1099 Lottery
题意超难懂,实则一道概率论的题目.求P(n).P(n) = n*(1+1/2+1/3+1/4+...+1/n).结果如果可以除尽则表示为整数,否则表示为假分数. #include <cstdio ...
- Android 逐帧动画isRunning 一直返回true的问题
AnimationDrawabl主要通过xml实现逐帧动画,SDK实例如下: An AnimationDrawable defined in XML consists of a single < ...
- HUD --- 3635
Five hundred years later, the number of dragon balls will increase unexpectedly, so it's too difficu ...