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 ...
随机推荐
- 剑指offer之 O(1)时间删除链表结点
问题描述:给定单向链表的头指针和一个结点指针,定义一个函数在O(1)时间删除该结点. package Problem13; /* * 问题描述:给定单向链表的头指针和一个结点指针,定义一个函数在O(1 ...
- EntityFramework 学习 一 DbSet
DBSet类表示一个实体的集合,用来创建.更新.删除.查询操作,DBSet<TEntity>是DBSet的泛型版本 你可以使用DbContext获取DBSet的引用,例如dbContext ...
- 算法(Algorithms)第4版 练习 2.2.11(3)
关键代码实现: public static void sort(Comparable[] input) { int N = input.length; aux = input.clone();//mu ...
- Codeforces Round #250 (Div. 2) D. The Child and Zoo 并查集
D. The Child and Zoo time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Codeforces 163A Substring and Subsequence:dp【子串与子序列匹配】
题目链接:http://codeforces.com/problemset/problem/163/A 题意: 给你两个字符串a,b,问你有多少对"(a的子串,b的子序列)"可以匹 ...
- Java中数学计算的相关方法
1:Math类 2.BigInteger类 3.BigDecimal类 BigInteger bi = new BigInteger("12433241123"); BigDec ...
- [原]NYOJ-216-A problem is easy
大学生程序代写 /*A problem is easy 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 When Teddy was a child , he was a ...
- Linux上用nginx搭建RTMP服务器
参考文章:https://obsproject.com/forum/resources/how-to-set-up-your-own-private-rtmp-server-using-nginx.5 ...
- xcopy语法
xcopy语法 2007-02-09 13:29:45| 分类: 服务器 | 标签:xcopy语法 |字号 订阅复制文件和目录,包括子目录. 语法 xcopySource [Destination] ...
- LOJ2303 「NOI2017」蚯蚓排队
「NOI2017」蚯蚓排队 题目描述 蚯蚓幼儿园有$n$只蚯蚓.幼儿园园长神刀手为了管理方便,时常让这些蚯蚓们列队表演. 所有蚯蚓用从$1$到$n$的连续正整数编号.每只蚯蚓的长度可以用一个正整数表示 ...