题目链接

能AC还是很开心的...此题没有POJ2778那么难,那个题还需要矩阵乘法,两个题有点相似的。

做题之前,把2778代码重新看了一下,回忆一下当时做题的思路,回忆AC自动机是干嘛的...

状态表示dp[i][j]长度为i的以j串为结束的最小改变数目。AC自动机预处理一下,然后DP。

卡内存+不知道状态数,MLE+RE+WA了多次。

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cstdlib>
using namespace std;
#define N 2222222
#define INF 10000000
int trie[N][];
int o[N];
int que[N];
int fail[N];
int dp[][];
int t;
void CL()
{
memset(trie,-,sizeof(trie));
memset(o,,sizeof(o));
t = ;
}
int judge(char s)
{
switch(s)
{
case'A':return ;
case'C':return ;
case'G':return ;
case'T':return ;
}
return ;
}
void insert(char *str)
{
int i,len,root;
root = ;
len = strlen(str);
for(i = ;i < len;i ++)
{
if(trie[root][judge(str[i])] == -)
trie[root][judge(str[i])] = t ++;
root = trie[root][judge(str[i])];
}
o[root] = ;
}
void build_ac()
{
int head,tail,front,i;
head = tail = ;
for(i = ;i < ;i ++)
{
if(trie[][i] != -)
{
fail[trie[][i]] = ;
que[tail++] = trie[][i];
}
else
{
trie[][i] = ;
}
}
while(head != tail)
{
front = que[head++];
if(o[fail[front]])
o[front] = ;
for(i = ;i < ;i ++)
{
if(trie[front][i] != -)
{
que[tail++] = trie[front][i];
fail[trie[front][i]] = trie[fail[front]][i];
}
else
{
trie[front][i] = trie[fail[front]][i];
}
}
}
}
int main()
{
int n,i,j,k,len,flag,cas = ;
char str[];
while(scanf("%d",&n)!=EOF)
{
if(n == ) break;
CL();
for(i = ;i <= n;i ++)
{
scanf("%s",str);
insert(str);
}
build_ac();
scanf("%s",str);
len = strlen(str);
for(i = ;i <= len;i ++)
{
for(j = ;j <= t;j ++)
dp[i][j] = INF;
}
dp[][] = ;
for(i = ;i < len;i ++)
{
for(j = ;j < t;j ++)
{
if(o[j]) continue;
for(k = ;k < ;k ++)
{
if(o[trie[j][k]]) continue;
flag = !(k == judge(str[i]));
dp[i+][trie[j][k]] = min(dp[i+][trie[j][k]],dp[i][j] + flag);
}
}
}
int ans = INF;
for(i = ;i < t;i ++)
{
ans = min(ans,dp[len][i]);
}
printf("Case %d: ",cas ++);
if(ans == INF)
printf("-1\n");
else
printf("%d\n",ans);
}
return ;
}

POJ 3691 DNA repair(AC自动机+DP)的更多相关文章

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

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

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

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

  3. [hdu2457]DNA repair(AC自动机+dp)

    题意:给出一些不合法的模式DNA串,给出一个原串,问最少需要修改多少个字符,使得原串中不包含非法串. 解题关键:多模式串匹配->AC自动机,求最优值->dp,注意在AC自动机上dp的套路. ...

  4. poj 2778 DNA Sequence AC自动机DP 矩阵优化

    DNA Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11860   Accepted: 4527 Des ...

  5. POJ 3691 DNA repair ( Trie图 && DP )

    题意 : 给出 n 个病毒串,最后再给出一个主串,问你最少改变主串中的多少个单词才能使得主串中不包含任何一个病毒串 分析 : 做多了AC自动机的题,就会发现这些题有些都是很套路的题目.在构建 Trie ...

  6. POJ3691 DNA repair(AC自动机 DP)

    给定N个长度不超过20的模式串,再给定一个长度为M的目标串S,求在目标串S上最少改变多少字符,可以使得它不包含任何的模式串 建立Trie图,求得每个节点是否是不可被包含的串,然后进行DP dp[i][ ...

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

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

  8. POJ 2778 DNA Sequence (AC自动机+DP+矩阵)

    题意:给定一些串,然后让你构造出一个长度为 m 的串,并且不包含以上串,问你有多少个. 析:很明显,如果 m 小的话 ,直接可以用DP来解决,但是 m 太大了,我们可以认为是在AC自动机图中,根据离散 ...

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

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

随机推荐

  1. raw格式镜像文件压缩并转换为qcow2格式

    raw格式文件,这个比较占用空间,你可以用以下命令将其压缩并转换成qcow2格式. # virt-sparsify --compress --convert qcow2 ubuntu.img ubun ...

  2. mysql sql维护常用命令

    mysql修改表名,列名,列类型,添加表列,删除表列 alter table test rename test1; --修改表名 alter table test add  column name v ...

  3. iOS NSOperation的使用

    先给出NSOpetation的官方指导https://developer.apple.com/library/ios/documentation/Cocoa/Reference/NSOperation ...

  4. Objective-C 和 C++中指针的格式和.方法 和内存分配

    最近在看cocos2d-x,于是打算复习一下C++,在这里简单对比下,留个念想. 先看看oc中指针的用法 @interface ViewController : UIViewController { ...

  5. Java for LeetCode 025 Reverse Nodes in k-Group

    Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...

  6. 【JAVA、C++】LeetCode 016 3Sum Closest

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  7. codeforces A. Jeff and Digits 解题报告

    题目链接:http://codeforces.com/problemset/problem/352/A 题目意思:给定一个只有0或5组成的序列,你要重新编排这个序列(当然你可以不取尽这些数字),使得这 ...

  8. google maps js v3 api教程(2) -- 在地图上添加标记

    原文链接 google maps javascript官方文档:https://developers.google.com/maps/documentation/javascript/ 我们在创建地图 ...

  9. Windows下安装Cygwin及包管理器apt-cyg(转)

    本文为转载文章: http://www.2cto.com/os/201212/176551.html Cygwin可以在Windows下使用unix环境Bash和各种功能强大的工具,对于Linux管理 ...

  10. 深入C++中构造函数、拷贝构造函数、赋值操作符、析构函数的调用过程总结

    转自 http://www.jb51.net/article/37527.htm,感谢作者 #include "stdafx.h"      #include <iostre ...