POJ 3691 DNA repair (DP+字符串)
题意:给出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+字符串)的更多相关文章
- 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 ...
- POJ 3691 DNA repair(AC自动机+DP)
题目链接 能AC还是很开心的...此题没有POJ2778那么难,那个题还需要矩阵乘法,两个题有点相似的. 做题之前,把2778代码重新看了一下,回忆一下当时做题的思路,回忆AC自动机是干嘛的... 状 ...
- 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 ...
- POJ 3691 DNA repair ( Trie图 && DP )
题意 : 给出 n 个病毒串,最后再给出一个主串,问你最少改变主串中的多少个单词才能使得主串中不包含任何一个病毒串 分析 : 做多了AC自动机的题,就会发现这些题有些都是很套路的题目.在构建 Trie ...
- [poj 3691]DNA repair
好久没刷 poj 了,今天练习 AC 自动机时去水了一发喵~ 在 poj 上 A 题的感觉并没有 BZOJ 上那么愉悦,准确的说是痛不欲生 真是应了那句老话,你再慢也有比你慢的,你再快也有比你快的…… ...
- POJ 3691 DNA Sequence (AC自动机 + 矩阵 有bug,待修改)
DNA Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9889 Accepted: 3712 Desc ...
- POJ 1007 DNA sorting (关于字符串和排序的水题)
#include<iostream>//写字符串的题目可以用这种方式:str[i][j] &str[i] using namespace std; int main() {int ...
随机推荐
- while 读取文件内容
exec < filename while read line;do echo $line done 方法1 while read line;do echo $line done<$ 方法 ...
- HDU 找到唯一的冠军
产生冠军 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & ...
- The Great Pan
The Great Pan Time Limit:1000MS Memory Limit:65536KB ...
- 剑指offer之 替换空格
package Problem4; public class ReplaceBank { /* * 题目描述: 请实现一个函数,将字符串的每个空格替换为"%20". * 例如输入& ...
- 算法(Algorithms)第4版 练习 1.3.32
ADT: /** * see if Steque is empty * @return {@code true} Steque is empty * {@code false} Steque isn' ...
- jquery中bind,live,delegate,on的区别
这几种方法都是绑定事件用到的,但是他们之间有些差别 bind(type,[data],fn) 为每个匹配元素的特定事件绑定事件处理函数 例如: <ul> <a href=" ...
- python-socket1
如何理解socket编程 就是两个进程,跨计算机,他俩需要通讯的话,需要通过网络对接起来.这就是 socket 的作用.打个比方吧,两个进程在两个计算机上,需要有一个进程做被动方,叫做服务器.另一个做 ...
- 01_常用的MIME类型
.doc application/msword .docx application/vnd.openxmlformats-officedocument.wordprocessingml.d ...
- Netty组件理解(转载)
转载的文章,写的非常好. 一.先纵览一下Netty,看看Netty都有哪些组件? 为了更好的理解和进一步深入Netty,我们先总体认识一下Netty用到的组件及它们在整个Netty架 ...
- linux命令学习笔记(16):which命令
我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索: which 查看可执行文件的位置. whereis 查看文件的位置. locate 配合数据库查看文件位置. f ...