poj 1795 DNA Laboratory
| Time Limit: 5000MS | Memory Limit: 30000K | |
| Total Submissions: 2892 | Accepted: 516 |
Description
Having started to build his own DNA lab just recently, the evil doctor Frankenstein is not quite up to date yet. He wants to extract his DNA, enhance it somewhat and clone himself. He has already figured out how to extract DNA from some of his blood cells, but unfortunately reading off the DNA sequence means breaking the DNA into a number of short pieces and analyzing those first. Frankenstein has not quite understood how to put the pieces together to recover the original sequence.
His pragmatic approach to the problem is to sneak into university and to kidnap a number of smart looking students. Not surprisingly, you are one of them, so you would better come up with a solution pretty fast.
Problem
You are given a list of strings over the alphabet A (for adenine), C (cytosine), G (guanine), and T (thymine),and your task is to find the shortest string (which is typically not listed) that contains all given strings as substrings.
If there are several such strings of shortest length, find the smallest in alphabetical/lexicographical order.
Input
For each scenario, the first line contains the number n of strings with 1 <= n <= 15. Then these strings with 1 <= length <= 100 follow, one on each line, and they consist of the letters "A", "C", "G", and "T" only.
Output
Sample Input
1
2
TGCACA
CAT
Sample Output
Scenario #1:
TGCACAT
Source
#define _CRT_SECURE_NO_DEPRECATE
#include <iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<bitset>
#include<set>
#include<map>
#include<cmath>
using namespace std;
#define N_MAX 16
#define MOD 100000000
#define INF 0x3f3f3f3f
typedef long long ll;
string s[N_MAX];
int dp[<<N_MAX][N_MAX];//状态是i,当前字符串的头部是字符串j时总字符串最小长度
int dist[N_MAX][N_MAX];//dist[i][j]:在j的前面加上字符串i,整体字符串所需要增加的长度
vector<string>vec;
int t,n; void init() {
memset(dp, INF, sizeof(dp));
memset(dist, , sizeof(dist));
for (int i = ; i < n; i++) {
for (int j = ; j < n;j++) {
if (i == j)continue;
int sz = min(vec[i].size(), vec[j].size());
for (int k = sz; k >= ;k--) {
if (vec[i].substr(vec[i].size() - k) == vec[j].substr(, k)) {//首尾重复的部分不算
dist[i][j] = vec[i].size() - k;
break;
}
}
}
}
}
string res = "";
void dfs(int head,int state) {//state状态表示当前还有哪些字符串没有被使用
if (state == )return;
string min_s = "Z";int min_head;
for (int i = ; i < n;i++) {
if ((state >> i & )&&dp[state|<<head][head]==dp[state][i]+dist[head][i]) {
int Len = vec[head].size() - dist[head][i];
string s = vec[i].substr(Len);
if (min_s > s) { min_s = s; min_head = i; }
}
}
res += min_s;
dfs(min_head, state ^ ( << min_head));
} int main() {
int t; scanf("%d",&t);
for (int cs = ; cs <= t;cs++) {
scanf("%d",&n);
printf("Scenario #%d:\n",cs);
for (int i = ; i < n; i++) cin >> s[i];
vec.clear();
for (int i=; i < n;i++) {//检查是否有重复的字符串
bool flag = ;
for (int j = ; j < n;j++) {
if (i == j || s[i].size() > s[j].size())continue;
if (s[j].find(s[i]) != string::npos) {//找到重复
flag = ; break;
}
}
if (flag)vec.push_back(s[i]);
}
if (vec.size() == ) { cout << s[] << endl << endl; continue; }
sort(vec.begin(), vec.end());
n = vec.size();
init();
int allstates = << n;
for (int i = ; i < n;i++) {
dp[ << i][i] = vec[i].size();
} for (int state = ; state < allstates; state++) {
for (int i = ; i < n;i++) {
if (dp[state][i] == INF)continue;
for (int j = ; j < n; j++) {
if (!(state >> j & )) {
dp[state | << j][j] = min(dp[state | << j][j], dp[state][i] + dist[j][i]);
}
}
}
}
int head=;
for(int i=;i<n;i++)
if (dp[allstates - ][i] < dp[allstates-][head]) {
head = i;
}
res = vec[head];
dfs(head, (allstates -)^ ( << head));//!!!!
cout << res << endl<<endl;
}
return ;
}
poj 1795 DNA Laboratory的更多相关文章
- POJ 1795 DNA Laboratory(状压DP)
[题目链接] http://poj.org/problem?id=1795 [题目大意] 给出n个字符串,求一个最小长度的串,该串包含给出的所有字符串. 要求长度最小且字典序最小. [题解] dp[i ...
- POJ 1795 DNA Laboratory (贪心+状压DP)
题意:给定 n 个 字符串,让你构造出一个最短,字典序最小的字符串,包括这 n 个字符串. 析:首先使用状压DP,是很容易看出来的,dp[s][i] 表示已经满足 s 集合的字符串以 第 i 个字符串 ...
- POJ 1795
DNA Laboratory Time Limit: 5000MS Memory Limit: 30000K Total Submissions: 1425 Accepted: 280 Des ...
- poj 1007 DNA Sorting 解题报告
题目链接:http://poj.org/problem?id=1007 本题属于字符串排序问题.思路很简单,把每行的字符串和该行字符串统计出的字母逆序的总和看成一个结构体.最后把全部行按照这个总和从小 ...
- POJ 2778 DNA Sequence(AC自动机+矩阵加速)
DNA Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9899 Accepted: 3717 Desc ...
- poj 2778 DNA Sequence ac自动机+矩阵快速幂
链接:http://poj.org/problem?id=2778 题意:给定不超过10串,每串长度不超过10的灾难基因:问在之后给定的长度不超过2e9的基因长度中不包含灾难基因的基因有多少中? DN ...
- POJ 2778 DNA Sequence(AC自动机+矩阵快速幂)
题目链接:http://poj.org/problem?id=2778 题意:有m种DNA序列是有疾病的,问有多少种长度为n的DNA序列不包含任何一种有疾病的DNA序列.(仅含A,T,C,G四个字符) ...
- POJ 3691 DNA Sequence (AC自动机 + 矩阵 有bug,待修改)
DNA Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9889 Accepted: 3712 Desc ...
- [POJ 1007] DNA Sorting C++解题
DNA Sorting Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 77786 Accepted: 31201 ...
随机推荐
- Java实现随机出题,10道10以内加减法计算
package com.swift; import java.awt.Toolkit; import java.util.Scanner; public class PlusQuiz { public ...
- PAT 乙级 1059
题目 题目地址:PAT 乙级 1059 题解 开始我是从暴力循环的角度考虑这道题,大概计算了一下时间复杂度应该不会超,但是很不幸没有通过,时间超限:之后考虑搜索算法可能优化不太好,因此就把输入的序列先 ...
- Python中的tuple
tuple_lst = [ ('元祖容器可哈希',), ('元祖中的元素不可直接修改',), ('元祖可迭代',), ('查',), ('练习',), ] 元祖容器可哈希 >>>ha ...
- (转)CocoaPods
本文转自http://nshipster.cn/cocoapods/ 文明是建立在道路,桥梁,运河,下水道,管线,电线和光纤这些基础设施之上的.只要设计和施工得当,它们可以帮助社会成倍的发展. 唯一的 ...
- C++多态实例
#include <iostream> #include <string> using namespace std; //class 实现 class Employee { s ...
- linux lvm扩容
1.分区, 查看磁盘使用:fdisk -l 对磁盘分区:fdisk /dev/sdb 2.创建pv pvcreate /dev/sdb1 查看pv: pvdisplay 3.查看vg vgdisp ...
- CCPC_1003
这个题可以暴力的哟,直接暴力的哟 不用做什么订立的哟 不需要特别判断的哟 去死吧!!!愚蠢的我! #include<bits/stdc++.h> using namespace std; ...
- 图学java基础篇之并发
概述 并发处理本身就是编程开发重点之一,同时内容也很繁杂,从底层指令处理到上层应用开发都要涉及,也是最容易出问题的地方.这块知识也是评价一个开发人员水平的重要指标,本人自认为现在也只是学其皮毛,因此本 ...
- 第2章c++简单程序设计
第2章c++简单程序设计 知识梳理 以下是我遗忘以及认为重要的知识整理: 1.标识符的构成规则: 以大写字母.小写字母或下划线 _ 开始 由大写字母.小写字母.下划线 _ 或数字(0~9)组成 大写字 ...
- UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 19: ordinal not in range(128)
解决方案: 1: 在网上找到的解决方案是: 在调用import matplotlib.pyplot as plt前 import sys sys.setdefaultencoding(“gbk”) 让 ...