Match:DNA repair(POJ 3691)

题目大意:给定一些坏串,再给你一个字符串,要你修复这个字符串(AGTC随便换),使之不含任何坏串,求修复所需要的最小步数。
这一题也是和之前的那个1625的思想是一样的,通过特殊的trie树找到所有的状态然后一个一个枚,具体状态转移的思想可以在1625那里看
当然了这一题不是像1625那样求总的组合数,这一题也是DP,求的是最小值,那么我们也是一样,统计从合法状态中转移到任何一个状态最小值即可。
状态转移方程dp[i+1][转移状态]=min(dp[i+1][转移状态],dp[i][当前状态]+s)(当转移状态对应的就是trie的节点,当节点对应的字符等于字符串当前位置的字符,则s为0,否则s为1。)
#include <iostream>
#include <algorithm>
#include <functional>
#include <string.h>
#define MAX 1010 using namespace std; struct node
{
int Num, If_End;
struct node *Fail, *Next[];
}*root, Mem_Pool[MAX], *Queue[ * MAX]; static int sum_node, Hash_Table[], dp[MAX][MAX];
static char str[MAX], DNA[] = { 'A', 'G', 'C', 'T' }; struct node *create_new_node();
int find_min(const int, const int);
void put_DNA_into_hash(void);
void insert(struct node *);
void build_ac_automation(struct node *); int main(void)
{
int Disease_Segement_Sum, str_length, case_sum = ; put_DNA_into_hash();
while (~scanf("%d", &Disease_Segement_Sum))
{
if (Disease_Segement_Sum == )
break;
sum_node = ;
node *root = create_new_node();
getchar(); for (int i = ; i < Disease_Segement_Sum; i++)
insert(root);
build_ac_automation(root); gets(str);
str_length = strlen(str); for (int i = ; i < str_length; i++)
{
fill(dp[i + ], dp[i + ] + sum_node, MAX + );
for (int j = ; j < sum_node; j++)
{
if (Mem_Pool[j].If_End)
continue;
for (int k = ; k < ; k++)
{
int id = Mem_Pool[j].Next[k]->Num;
if (Mem_Pool[j].Next[k]->If_End)
continue;
else if (Hash_Table[str[i]] == k)
dp[i + ][id] = find_min(dp[i + ][id], dp[i][j]);
else
dp[i + ][id] = find_min(dp[i + ][id], dp[i][j] + );
}
}
}
int ans = MAX + ;
for (int i = ; i < sum_node; i++)
ans = find_min(ans, dp[str_length][i]);
if (ans != MAX + )
printf("Case %d: %d\n", case_sum++, ans);
else
printf("Case %d: -1\n",case_sum++);
}
return EXIT_SUCCESS;
} int find_min(const int x, const int y)
{
return x < y ? x : y;
} struct node *create_new_node(void)
{
node *tmp = &Mem_Pool[sum_node];
tmp->Fail = NULL;
tmp->If_End = ;
memset(tmp->Next, , sizeof(struct node *) * );
tmp->Num = sum_node++;
return tmp;
} void put_DNA_into_hash(void)
{
for (int i = ; i<; i++)
Hash_Table[DNA[i]] = i;
} void insert(struct node *root)
{
node *ptr = root;
gets(str); for (int i = ; str[i] != '\0'; i++)
{
int id = Hash_Table[str[i]];
if (ptr->Next[id] == NULL)
ptr->Next[id] = create_new_node();
ptr = ptr->Next[id];
}
ptr->If_End = ;
} void build_ac_automation(struct node *root)
{
int head = , tail = ;
root->Fail = NULL;
Queue[tail++] = root; while (head != tail)
{
node *out = Queue[head++];
for (int i = ; i < ; i++)
{
if (out->Next[i] != NULL)
{
if (out == root)
out->Next[i]->Fail = root;
else
{
out->Next[i]->Fail = out->Fail->Next[i];
if (out->Fail->Next[i]->If_End)
out->Next[i]->If_End = ;
}
Queue[tail++] = out->Next[i];
}
else if (out == root)
out->Next[i] = root;
else
out->Next[i] = out->Fail->Next[i];
}
}
}

Match:DNA repair(POJ 3691)的更多相关文章
- 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: ...
- POJ 3691 DNA repair (DP+AC自动机)
DNA repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4815 Accepted: 2237 Descri ...
- poj 3691 DNA repair(AC自己主动机+dp)
DNA repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5877 Accepted: 2760 Descri ...
- HDU 2457/POJ 3691 DNA repair AC自动机+DP
DNA repair Problem Description Biologists finally invent techniques of repairing DNA that contains ...
- HDU 2457 DNA repair(AC自动机+DP)题解
题意:给你几个模式串,问你主串最少改几个字符能够使主串不包含模式串 思路:从昨天中午开始研究,研究到现在终于看懂了.既然是多模匹配,我们是要用到AC自动机的.我们把主串放到AC自动机上跑,并保证不出现 ...
- hdu2457:DNA repair
AC自动机+dp.问改变多少个字符能让目标串不含病毒串.即走过多少步不经过病毒串终点.又是同样的问题. #include<cstdio> #include<cstring> # ...
- HDU 2425 DNA repair (AC自动机+DP)
DNA repair Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 【POJ3691】 DNA repair (AC自动机+DP)
DNA repair Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Description B ...
- HDU2457 DNA repair —— AC自动机 + DP
题目链接:https://vjudge.net/problem/HDU-2457 DNA repair Time Limit: 5000/2000 MS (Java/Others) Memory ...
随机推荐
- jquery点击label触发2次的问题
今天写问卷的时候遇到个label点击的时候,监听的click事件被执行两次:产生这个的原因么...事件冒泡 <div class="questionBox checkBox" ...
- 将本地的新建的web Form页面放到服务器提示错误;
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs&q ...
- spark
http://www.cnblogs.com/shishanyuan/p/4723604.html?utm_source=tuicool spark presto2.0计算引擎 http://blog ...
- XML理解
XML:页面之间传递数据,跨平台传递,核心标签 HTML:超文本标记语言,核心标签 <xml version='1.0'>版本1.0<Nation> <one> & ...
- MSP430G2333下位机乘法运算需要注意的一个问题
背景: 最近负责为主板管理电源的电源管理模块编写软体,使用的MCU为MSP430G2333.功能上很简单,即通过板子上的硬件拨码设定,或者通过IIC与主板通信,由主板的BIOS决定开机及关机的延时供电 ...
- MongoDB—— 写操作 Core MongoDB Operations (CRUD)
MongoDB使用BSON文件存储在collection中,本文主要介绍MongoDB中的写操作和优化策略. 主要有三种写操作: Create Update ...
- PowerDesigner的使用(一)
一. PowerDesigner 功能 1. 需求管理:记录需求,分析设计模型 2. 生成文档:生成HTML格式文档,方便沟通. 3. 影响度分析:模型之间连接起来,同步修改功能. 4. 数据映射:提 ...
- [Effective JavaScript 笔记]全书总结
这本书中就像它前言中说的那样,这本书不是给初学者的.需要一定的基础,而且有一定的编码实践,才能很好的理解书里讲到的内容.学习一门编程语言,需要熟悉它的语法.形式和结构,这样才会编写合法的.符合语义的. ...
- Unity手游之路<十二>手游资源热更新策略探讨
http://blog.csdn.net/janeky/article/details/17666409 上一次我们学习了如何将资源进行打包.这次就可以用上场了,我们来探讨一下手游资源的增量更新策略. ...
- ducument.ready不生效的问题 ruby on rails
rails web app页面之间的跳转的时候使用ducument.ready只有在再次加载的时候才生效, 因为rails用了turbolinks, https://github.com/turbol ...