题意:给出nn(1≤n≤50,1≤n≤50) 个病毒DNA序列,长度均不超过20。现在给出一个长度不超过1000的字符串,求至少要更换多少个字符,

才能使这个字符串不包含这些DNA序列。

析:利用前缀来做好状态转移。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e16;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1000 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} string str;
vector<string> v, prx;
int dp[maxn][maxn], nxt[maxn][4];
bool nt[maxn]; const char *ACGT = "ACGT"; int main(){
int kase = 0;
while(scanf("%d", &n) == 1 && n){
string s;
v.clear(); prx.clear();
for(int i = 0; i < n; ++i){
cin >> s;
v.push_back(s);
}
cin >> str;
for(int i = 0; i < n; ++i)
for(int j = 0; j <= v[i].size(); ++j)
prx.push_back(v[i].substr(0, j));
sort(prx.begin(), prx.end());
prx.erase(unique(prx.begin(), prx.end()), prx.end()); for(int i = 0; i < prx.size(); ++i){
nt[i] = false;
for(int j = 0; j < n && !nt[i]; ++j)
nt[i] |= v[j].size() <= prx[i].size() && prx[i].substr(prx[i].size()-v[j].size()) == v[j];
if(nt[i]) continue;
for(int j = 0; j < 4; ++j){
s = prx[i] + ACGT[j];
int k;
while(1){
k = lower_bound(prx.begin(), prx.end(), s) - prx.begin();
if(k < prx.size() && prx[k] == s) break;
s = s.substr(1);
}
nxt[i][j] = k;
}
} memset(dp, INF, sizeof dp);
dp[0][0] = 1;
for(int i = 1; i < prx.size(); ++i) dp[0][i] = 0;
for(int t = 0; t < str.size(); ++t){
for(int i = 0; i < prx.size(); ++i){
if(nt[i]) continue;
for(int j = 0; j < 4; ++j){
int k = nxt[i][j];
dp[t+1][k] = min(dp[t+1][k], dp[t][i] + (str[t] != ACGT[j]));
}
}
} int ans = INF;
for(int i = 0; i < prx.size(); ++i) if(!nt[i])
ans = min(ans, dp[str.size()][i]);
printf("Case %d: %d\n", ++kase, ans == INF ? -1 : ans);
}
return 0;
}

  

POJ 3691 DNA repair (DP+字符串)的更多相关文章

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

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

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

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

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

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

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

    题目链接 能AC还是很开心的...此题没有POJ2778那么难,那个题还需要矩阵乘法,两个题有点相似的. 做题之前,把2778代码重新看了一下,回忆一下当时做题的思路,回忆AC自动机是干嘛的... 状 ...

  5. POJ 3691 DNA repair 基于AC自己主动机DP

    dp[i][j] 它表示的长度 i 下游前缀 j 更改节点的最小数量. 很清楚dp[0][0] = 0; dp[ i ][ j ] = min(dp[ i ][ j ],dp[i-1][k] + (j ...

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

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

  7. [poj 3691]DNA repair

    好久没刷 poj 了,今天练习 AC 自动机时去水了一发喵~ 在 poj 上 A 题的感觉并没有 BZOJ 上那么愉悦,准确的说是痛不欲生 真是应了那句老话,你再慢也有比你慢的,你再快也有比你快的…… ...

  8. POJ 3691 DNA Sequence (AC自动机 + 矩阵 有bug,待修改)

    DNA Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9889   Accepted: 3712 Desc ...

  9. POJ 1007 DNA sorting (关于字符串和排序的水题)

    #include<iostream>//写字符串的题目可以用这种方式:str[i][j] &str[i] using namespace std; int main() {int ...

随机推荐

  1. hd acm1466

    http://www.cnblogs.com/alihenaixiao/p/4107907.html#undefined.这个博客有详解,我这个只是写一些·自己的总结. 问题:平面上有n条直线,且无三 ...

  2. 优化chkconfig

    只保留系统服务: crond /network /sshd /rsyslog /sysstat 其他服务全部关闭 首先将所有 3:on的服务名过滤出来,然后 grep -vE 排除需要的服务 #!/b ...

  3. mysql数据库更新

    在使用mysql数据库的时候,A方使用一个版本,B方在使用一个版本数据库进行开发使用,B方在开发的时候,有新的需求,需要添加表字段和所需要的表.但是A方已经在使用之前的版本数据库并且数据库里面有真实的 ...

  4. Django 模版当中使用中文 UnicodeDecodeError at / 问题

    Django 再次是当中字符编码问题 今天使用了bootstrap 当中的一些CSS 对自己的博客项目当中的一些东西进行美化 但是很奇怪的是 当 诸如按钮类的加入 中文字符后 就会提示 Unicode ...

  5. Python习题-输出一个字符串中最长的子字符串及其长度

    描述:有个字符串$sd1#111$svda123!!!221&eSSDSDG,包含特殊字符.数字和字母,输出最长的子字符串和他的长度#例如上面的字符串包含数字字母的字符串是svda123,长度 ...

  6. hdu4699 Editor(双向链表或双栈对弹)

    本题就是两个要点: 1.数据结构的设计.显然可以使用双向链表来做,但是写双向链表的代码复杂度高.其实更好的方法是使用两个对弹的栈来做,而且没必要用STL的栈,就自己开两个数组简单搞一下就好了. 2.最 ...

  7. Java自定义分页标签的实现

    主要字段含义: 页号 pagaNo页面大小 pageSize总记录条数 recordCount计算本次一共分多少页 myPageSize页号显示开始 start 页号显示结束 end PageTag需 ...

  8. FFMPEG实现H264的解码(从源代码角度)

    农历2014年底了,将前段时间工作中研究的FFMPEG解码H264流程在此做一下整理,也算作年终技术总结了! H264解码原理: H264的原理参考另一篇博文 http://blog.csdn.net ...

  9. ACM学习历程—ZOJ 3777 Problem Arrangement(递推 && 状压)

    Description The 11th Zhejiang Provincial Collegiate Programming Contest is coming! As a problem sett ...

  10. How to manage concurrency in Django models

    How to manage concurrency in Django models The days of desktop systems serving single users are long ...