基因修复

  题目大意:给定一些坏串,再给你一个字符串,要你修复这个字符串(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)的更多相关文章

  1. POJ 3691 &amp; 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: ...

  2. POJ 3691 DNA repair (DP+AC自动机)

    DNA repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4815   Accepted: 2237 Descri ...

  3. poj 3691 DNA repair(AC自己主动机+dp)

    DNA repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5877   Accepted: 2760 Descri ...

  4. HDU 2457/POJ 3691 DNA repair AC自动机+DP

    DNA repair Problem Description   Biologists finally invent techniques of repairing DNA that contains ...

  5. HDU 2457 DNA repair(AC自动机+DP)题解

    题意:给你几个模式串,问你主串最少改几个字符能够使主串不包含模式串 思路:从昨天中午开始研究,研究到现在终于看懂了.既然是多模匹配,我们是要用到AC自动机的.我们把主串放到AC自动机上跑,并保证不出现 ...

  6. hdu2457:DNA repair

    AC自动机+dp.问改变多少个字符能让目标串不含病毒串.即走过多少步不经过病毒串终点.又是同样的问题. #include<cstdio> #include<cstring> # ...

  7. HDU 2425 DNA repair (AC自动机+DP)

    DNA repair Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  8. 【POJ3691】 DNA repair (AC自动机+DP)

    DNA repair Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Description B ...

  9. HDU2457 DNA repair —— AC自动机 + DP

    题目链接:https://vjudge.net/problem/HDU-2457 DNA repair Time Limit: 5000/2000 MS (Java/Others)    Memory ...

随机推荐

  1. python __future__ package的几个特性

    我学习python过程, 和学习其它编程知识一样, 不是先读大部头书系统学习, 而是看博客和直接实践, 慢慢将这些知识点连成线, 再扩展到面. 这个过程缺点和优点都很明显. 缺点是, 有些知识点可能因 ...

  2. [译]Mongoose指南 - Population

    MongoDB没有join, 但是有的时候我们需要引用其它collection的documents, 这个时候就需要populate了. 我们可以populate单个document, 多个docum ...

  3. 淘宝(阿里百川)手机客户端开发日记第十三篇 mysql的连接

    首先,我建立了一个包,里面存放了三个类文件,这三个文件是我从网络中找的,经过自己的整理.(我刚才查找想把这三个文件传上去,可能是自己对cnblogs的博客不太熟悉吧,没有找到,我只好粘贴代码了) 三个 ...

  4. 架设WEBIM

    使用openfire+jwchat构建. Openfire 采用Java开发,开源的实时协作(RTC)服务器基于XMPP(Jabber)协议.Openfire安装和使用都非常简单,并利用Web进行管理 ...

  5. BZOJ2843——极地旅行社

    1.题目大意:动态树问题,点修改,链查询.另外说明双倍经验题=bzoj1180 2.分析:lct模板题,练手的 #include <stack> #include <cstdio&g ...

  6. 搞明白这八个问题,Linux系统就好学多了

    导读 正在犹豫入坑Linux学习的同学或者已经入坑的同学,经常会问到这样八个问题.今天,这些问题我都会一一解答,希望我的看法能帮助各位同学.常言道“好的开始是成功的一半”,如果你明白了以下八个问题,就 ...

  7. C++笔试题(转)

    http://blog.csdn.net/hxz_qlh/article/details/16864567 这里面列举的题考察的东西都非常细,包括strcpy,字符串,大.小端的判断,很容易犯错,值得 ...

  8. BZOJ 1090: [SCOI2003]字符串折叠

    Sol 区间DP. 转移很简单,枚举会形成的断长转移就行,话说上一题我就跟这个是差不多的思路,转移改了改,然后死活过不了... 同样都是SCOI的题...相差4年... Code /********* ...

  9. 1 python学习——python环境配置

    1 python学习--python环境配置 要学习python语言,光看书看教程还是不好,得动手去写.当然,不管学习什么编程语言,最佳的方式还在于实践. 要实践,先得有一个Python解释器来解释执 ...

  10. Centos安装firefox/chrome

    centos安装chrome:去官网下载chrome安装包(xxx.rpm),带软件安装工具的系统双击该xxx.rpm就能自动安装,或者sudo rpm -i xxx.rpm安装. centos卸载自 ...