题目链接

题意:给出几个基因片段,要求你将它们排列成一个最短的序列,序列中使用了所有的基因片段,而且不能翻转基因。

分析:先计算出add数组,再dfs枚举。

空间复杂度O(n*n),  最坏时间复杂度 O(n^n),但是剪枝以后很快,因为好多搜不到后面,搜不到第n层。

 #include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <vector>
#include <algorithm>
#define LL long long
using namespace std;
const int maxn = +;
const int INF = <<;
int n, len[maxn], add[maxn][maxn], ans;
bool vis[maxn];
char s[maxn][maxn]; void cal(int a, int b, int lena, int lenb) //add计算串a在串b,前面增加的字符个数。
{
int i, j, k, f, x;
for(i = ; i < lena; i++)
{
f = ;
for(j = , k = i; j < lenb && k < lena; j++, k++)
{
if(s[a][k] == s[b][j]) continue;
else { f = ; break; }
}
if(f == ) break;
}
x = lena - i;
add[a][b] = lenb - x;
if(add[a][b]<) add[a][b] = ;
}
void dfs(int pre, int sum, int lenth) //分别代表之前的串,和串的总数,总串的长度。
{
if(lenth >= ans) return;
if(sum == n)
{
if(lenth < ans) ans = lenth;
return;
}
for(int i = ; i < n; i++)
{
if(!vis[i])
{
vis[i] = true;
if(add[pre][i]==) //一定要注意这是存在包含串,如abcdabc 包含 dab
//串a包含串b,等价于从a到b的边等于0,那么这时,状态在转移时,在原本
//是以串a结尾的状态加入串b,此时目标状态仍然是以串a结尾,这里需要注意。
dfs(pre, sum+, lenth+add[pre][i]);
else
dfs(i, sum+, lenth+add[pre][i]);
vis[i] = false;
}
}
}
int main()
{
int t, i, j;
scanf("%d", &t);
while(t--)
{
ans = INF;
memset(add, , sizeof(add));
memset(vis, false, sizeof(vis));
scanf("%d", &n);
for(i = ; i < n; i++)
{
scanf("%s", s[i]);
len[i] = strlen(s[i]);
}
for(i = ; i < n; i++)
for(j = ; j < n; j++)
cal(i, j, len[i], len[j]);
for(i = ; i < n; i++)
{
vis[i] = true;
dfs(i, , len[i]);
vis[i] = false;
}
printf("%d\n", ans);
}
return ;
}

poj 1699 Best Sequence (搜索技巧 剪枝 dfs)的更多相关文章

  1. poj 1699 Best Sequence(AC自己主动机+如压力DP)

    id=1699" target="_blank" style="">题目链接:poj 1699 Best Sequence 题目大意:给定N个D ...

  2. POJ 1699 Best Sequence (DFS+预处理)

    意甲冠军:看图片是晶莹剔透的,正确的, N连接到第一序列(同样的序列部分).总序列获得最短. 主题链接:http://poj.org/problem?id=1699 ~~~~ 思路就是:将N个序列首尾 ...

  3. POJ 1699 Best Sequence dfs

    题目: http://poj.org/problem?id=1699 无意间A了..超时一次,加了一句 if(len > ans)return; 然后就A了,dfs题,没有太多好说的,代码写的效 ...

  4. POJ 1699 Best Sequence(DFS)

    題目鏈接 題意 : 將幾個片段如圖所示方法縮成一個序列,求出最短這個序列. 思路 : 其實我也不知道怎麼做.....看網上都用了DP.....但是我不會.....這個DP不錯,還有用KMP+状压DP做 ...

  5. poj 1699 Best Sequence

    http://poj.org/problem?id=1699 题意:给你n个长度为L的序列,求包含这几个序列的最短长度. 先预处理每两个序列之间的关系,然后dfs枚举就行. #include < ...

  6. POJ - 3074 Sudoku (搜索)剪枝+位运算优化

    In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgrids. For exa ...

  7. ICPC Asia Nanning 2017 I. Rake It In (DFS+贪心 或 对抗搜索+Alpha-Beta剪枝)

    题目链接:Rake It In 比赛链接:ICPC Asia Nanning 2017 Description The designers have come up with a new simple ...

  8. hdoj1010 奇偶剪枝+DFS

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  9. 搜索(剪枝优化):HDU 5113 Black And White

    Description In mathematics, the four color theorem, or the four color map theorem, states that, give ...

随机推荐

  1. Getting Started with Java

    “学前”说明:<Learn Java for Android>这本书内容很多,都是精华,建议大家看英文版的.在这里我不打算一一总结书中的内容,书中每章节后面的exercises都很好,非常 ...

  2. 01-06-01【Nhibernate (版本3.3.1.4000) 出入江湖】事务

    Nhibernate事务的使用: public void Add(Customer customer) { ISession session = _sessionManager.GetSession( ...

  3. 同一机器 部署 两个 jboss

    当jboss和oracle在同一机器上时,通常oracle占用8080端口,这时只需要去修改\deploy\jbossweb-tomcat50.sar\server.xml中.当在同一台机器上运行两个 ...

  4. 词法分析器flex的使用

    词法分析器flex的功能说起来就是一句话,将正则表达式转化为c代码. flex编译成功后会生成一个flex.exe的可执行文件.此时,我们需要一个定义了正则表达式 动作的input文件.例如test. ...

  5. [转载]实战Linux下VMware虚拟机根目录空间扩充

    [转载]实战Linux下VMware虚拟机根目录空间扩充 (2011-07-31 21:34:34) 转载▼ 标签: 转载   原文地址:实战Linux下VMware虚拟机根目录空间扩充作者:shar ...

  6. tornado解析http body的过程分析

    tornado解析http body的过程分析 在最近写的一个RESTful API Server过程中,发现tornaod对解析POST BODY的内容有限制. 而在以前用web.py则没有这个限制 ...

  7. POJ 2379 ACM Rank Table(排序)

    题很水,数据注意一下四点即可: 1.有些team会在一道题AC了之后还提交,这个时候只需要算第一次ac的时间以及这之前的wa,之后的全部忽略.2.如果一道题没有ac,那么在计算时间时不应该加上它的wa ...

  8. HDU 1882 Strange Billboard(位运算)

    题目链接 题意 : 给你一个矩阵,有黑有白,翻转一个块可以让上下左右都翻转过来,问最少翻转多少次能让矩阵变为全白. 思路 : 我们从第一行开始枚举要翻转的状态,最多可以枚举到2的16次方,因为你只要第 ...

  9. hdu1874 畅通工程续

    http://acm.hdu.edu.cn/showproblem.php?pid=1874 //标准最短路模板 //需要注意的是两点间可能有多组 //需要取最短的 #include<iostr ...

  10. P1082 找朋友

    描述 童年的我们,对各种事物充满了好奇与向往.这天,小朋友们对数字产生了兴趣,并且想和数字交朋友.可是,怎么分配这些数字才能使得每个小朋友都唯一地找到一个数字朋友呢?C小朋友说:咱们按自己名字的字典序 ...