题目大意:

改动文本串的上的字符,使之不出现上面出现的串。问最少改动多少个。

思路分析:

dp[i][j]表示如今 i 个字符改变成了字典树上的 j 节点。

然后顺着自己主动机一直转移方程。

注意合法与不合法。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#define inf 0x3f3f3f3f
using namespace std; const char tab = 0;
const int max_next = 4;
int idx;
struct trie
{
struct trie *fail;
struct trie *next[max_next];
int isword;
int index;
trie()
{
isword=0;
memset(next,NULL,sizeof next);
fail=NULL;
}
};
int rev[256];
trie *que[100005],ac[100005];
int head,tail; trie *New()
{
trie *temp=&ac[idx];
for(int i=0;i<4;i++)temp->next[i]=NULL;
temp->fail=NULL;
temp->isword=0;
temp->index=idx++;
return temp;
}
void Insert(trie *root,char *word,int len){
trie *t=root;
for(int i=0;i<len;i++){
if(t->next[rev[word[i]]]==NULL)
t->next[rev[word[i]]]=New();
t=t->next[rev[word[i]]];
}
t->isword++;
} void acbuild(trie *root){
int head=0,tail=0;
que[tail++]=root;
root->fail=NULL;
while(head<tail){
trie *temp=que[head++],*p;
for(int i=0;i<max_next;i++){
if(temp->next[i]){
if(temp==root)temp->next[i]->fail=root;
else {
p=temp->fail;
while(p!=NULL){
if(p->next[i]){
temp->next[i]->fail=p->next[i];
break;
}
p=p->fail;
}
if(p==NULL)temp->next[i]->fail=root;
}
if(temp->next[i]->fail->isword)temp->next[i]->isword++;
que[tail++]=temp->next[i];
}
else if(temp==root)temp->next[i]=root;
else temp->next[i]=temp->fail->next[i];
}
}
} void del(trie *root)
{
for(int i=0;i<max_next;i++)
if(root->next[i])del(root->next[i]);
free(root);
} int dp[2005][2005];
int solve(char *word,int len)
{
memset(dp,0x3f,sizeof dp);
dp[0][0]=0;
for(int i=1;i<=len;i++)
{
for(int j=0;j<idx;j++)
{
if(ac[j].isword)continue;
if(dp[i-1][j]==inf)continue;
for(int k=0;k<4;k++)
{
int p=ac[j].next[k]->index;
if(ac[p].isword)continue;
dp[i][p]=min(dp[i][p],dp[i-1][j]+(k!=rev[word[i-1]]));
}
}
}
int ans=inf;
for(int i=0;i<idx;i++)
ans=min(ans,dp[len][i]);
return ans==inf? -1:ans;
}
char word[10005];
int main()
{
rev['A']=0;
rev['G']=1;
rev['C']=2;
rev['T']=3; int n,cas=1;
while(scanf("%d",&n)!=EOF && n)
{
idx=0;
trie *root=New();
for(int i=1;i<=n;i++)
{
scanf("%s",word);
Insert(root,word,strlen(word));
}
acbuild(root);
scanf("%s",word);
printf("Case %d: %d\n",cas++,solve(word,strlen(word)));
}
return 0;
}

Hdu 2457 DNA repair (ac自己主动机+dp)的更多相关文章

  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 2778 DNA Sequence (AC自己主动机 + dp)

    DNA Sequence 题意:DNA的序列由ACTG四个字母组成,如今给定m个不可行的序列.问随机构成的长度为n的序列中.有多少种序列是可行的(仅仅要包括一个不可行序列便不可行).个数非常大.对10 ...

  3. HDU - 2825 Wireless Password(AC自己主动机+DP)

    Description Liyuan lives in a old apartment. One day, he suddenly found that there was a wireless ne ...

  4. HDU 2457 DNA repair (AC自动机+DP)

    题意:给N个串,一个大串,要求在最小的改变代价下,得到一个不含上述n个串的大串. 思路:dp,f[i][j]代表大串中第i位,AC自动机上第j位的最小代价. #include<algorithm ...

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

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

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

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

  7. Hdu 3341 Lost&#39;s revenge (ac+自己主动机dp+hash)

    标题效果: 举个很多种DNA弦,每个字符串值值至1.最后,一个长字符串.要安排你最后一次另一个字符串,使其没事子值和最大. IDEAS: 首先easy我们的想法是想搜索的!管她3721..直接一个字符 ...

  8. HDU 2825 Wireless Password (AC自己主动机,DP)

    pid=2825">http://acm.hdu.edu.cn/showproblem.php? pid=2825 Wireless Password Time Limit: 2000 ...

  9. HDU 2896 病毒侵袭 (AC自己主动机)

    pid=2896">http://acm.hdu.edu.cn/showproblem.php?pid=2896 病毒侵袭 Time Limit: 2000/1000 MS (Java ...

随机推荐

  1. Centos 挂载NTFS格式的USB硬盘

    公司的一台服务器本身硬盘容量较小,要加一块2T的硬盘,用来做samba服务器,以下是挂载硬盘的主要记录: 这是服务器不认NTFS格式的情况:mount: unknown filesystem type ...

  2. linux 串口 特殊字符

    近日在写一个linux的串口程序,发现大多数情况下数据接收没问题,但是有时却有问题.主要是接收的字符串中包含有0x03这个字符,会造成与它相邻的字符同时也接收不到,搞了好久才发现这个错误.查找资料后发 ...

  3. Linux下,PHP扩展安装(使用yum安装)

    直接操作linux,在命令模式下用yum 来安装PHP的扩展: 扩展:mysqli 命令: yum install php-mysqli 扩展:pdo 命令: yum install php-pdo

  4. C++ 匿名对象的生命周期

    //匿名对象的生命周期 #define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace std; class Poin ...

  5. 002Maven_第一个Maven演示

    第一步. 首先建立Hello项目同时建立Maven约定的目录结构 Hello     --src     -----main     ----------java     ----------reso ...

  6. php -- 用文本来存储内容,file_put_contents,serialize,unserialize

    根据存储的内容来划分 字符串: file_put_contents :将一个字符串写入文件 语法:int file_put_contents ( string $filename , mixed $d ...

  7. 纪念伟大的sb错-noip滚粗

    人弱就是弱,被sb错虐翻. 手一抖一生就毁了 开此博文纪念这个伟大的sb错! noip2014 d2t2逆bfs后删点手残没考虑后效性,完美爆80 愿省选rp++,求进noi,orz

  8. 【NOIP模拟题】Incr(dp)

    太水的dp没啥好说的.. #include <cstdio> #include <cstring> #include <cmath> #include <st ...

  9. php和js实现文件拖拽上传

    Dropzone.js实现文件拖拽上传 http://www.sucaihuo.com/php/1399.html demo http://www.sucaihuo.com/jquery/13/139 ...

  10. 关于ie中实现弹性盒模型-我的css

    css3中的弹性盒模型大家都不陌生,但是能否在ie6中实现呢?第三方库中涉及到的页少之又少,也有一部分css框架中支持各种布局,下面给出我用的盒模型样式(为了以后copy方便而已): /******* ...