题目链接

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

分析:先计算出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. 【BZOJ 3504】[Cqoi2014]危桥

    Description Alice和Bob居住在一个由N座岛屿组成的国家,岛屿被编号为0到N-1.某些岛屿之间有桥相连,桥上的道路是双 向的,但一次只能供一人通行.其中一些桥由于年久失修成为危桥,最多 ...

  2. cocos中BatchNode精灵集合的使用

    1.CCSpriteBatchNode是为了提高渲染效率而实现的,它继承自CCNode 2.fps:帧率,是游戏中衡量流畅度的一个很重要的概念,cocos中默认的帧率是60,即一秒刷新60帧 3.精灵 ...

  3. .NET开源项目介绍及资源推荐:数据持久层

    在.NET平台下,关于数据持久层框架非常多,本文主要对如下几种做简要的介绍并推荐一些学习的资源: 1.NHibernate 2.NBear 3.Castle ActiveRecord 4.iBATIS ...

  4. C# Winform 拖放操作

    http://www.cnblogs.com/imlions/p/3189773.html 在开发程序的时候,为了提高用户的使用体验,或满足相关用户的功能,总是离不开拖放功能.而本文是总结winfor ...

  5. Sublime key bindings使用

    开启vi mode后,可以使用很多的VI快捷方式,所以我的sublime已经不是单纯的st了,st的VI模式不完全支持所有的快捷键.我们来看一段官网的key bindings示例: { "k ...

  6. Codeforces Round #343 (Div. 2) C. Famil Door and Brackets

    题目链接: http://codeforces.com/contest/629/problem/C 题意: 长度为n的括号,已经知道的部分的长度为m,现在其前面和后面补充‘(',或')',使得其长度为 ...

  7. Introduction to Deep Neural Networks

    Introduction to Deep Neural Networks Neural networks are a set of algorithms, modeled loosely after ...

  8. CKFinder 1.4.3 任意文件上传漏洞

    CKFinder 是国外一款非常流行的所见即所得文字编辑器,其1.4.3 asp.net版本存在任意文件上传漏洞,攻击者可以利用该漏洞上传任意文件. CKFinder在上传文件的时候,强制将文件名(不 ...

  9. java代码判断图片文件格式, 不是根据文件后缀来判断。

    public static final String TYPE_JPG = "jpg"; public static final String TYPE_GIF = "g ...

  10. java 追加写入代码一例

    最近最项目参数化的时候用到,场景是这样的,需要测试A和B两个接口,其中B接口传入的参数必须是传递给A接口过的,所以整理一个思路就是: 1. 正常调用A接口,但是将传递给A接口的参数保存到文本里,此处要 ...