题目链接

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

分析:先计算出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. LR(0)语法分析

    # include <stdio.h> # include <string.h> //存储LR(0)分析表 struct node { char ch; int num; }; ...

  2. WPF 多线程处理(3)

    WPF 多线程处理(1) WPF 多线程处理(2) WPF 多线程处理(3) WPF 多线程处理(4) WPF 多线程处理(5) WPF 多线程处理(6) 首先我们需要几个属性来保存取得的数据,因为在 ...

  3. 从一个新手容易混淆的例子简单分析C语言中函数调用过程

    某天,王尼玛写了段C程序: #include <stdio.h> void input() { int i; ]; ; i < ; i++) { array[i] = i; } } ...

  4. Linux "ls -l"文件列表权限详解

    ls Linux "ls -l"文件列表权限详解 1.使用 ls -l 命令 执行结果如下(/var/log) : drwxr-x--- root adm -- : apache2 ...

  5. 【转】使用JIRA搭建企业问题跟踪系统【个人推荐】

    免责声明:     本文转自网络文章,转载此文章仅为个人收藏,分享知识,如有侵权,请联系博主进行删除.     原文作者:Judy Shen的专栏     原文地址:使用JIRA搭建企业问题跟踪系统 ...

  6. 使用node的http模块实现爬虫功能,并把爬到的数据存入mongondb

    刚开始使用http中间件做爬虫其实蛮多坑的,最主要的坑就是编码问题,有很多中文网站的采用的gb2313的编码方式,这个在爬到的报文解析就很蛋碎, 因为http中间件对utf-8支持的比较好,所以针对这 ...

  7. 【POJ】【3537】Crosses and Crosses

    博弈论 相当于放了x的位置,左右4格都不能再放x了,谁无处可放就输. n<=2000 直接枚举后继状态,暴力求SG函数即可. 例: 0000000->x..0000 / .x..000 / ...

  8. ffmpeg参数解释

    基本选项: -formats 输出所有可用格式 -f fmt 指定格式(音频或视频格式) -i filename 指定输入文件名,在linux下当然也能指定: 0.0(屏幕录制)或摄像头 -y 覆盖已 ...

  9. Nginx正确记录post日志的方法

    Nginx正确记录post日志的方法 事实上可以很简单,这取决于把 access_log 放在哪个 location 里面. 一,放到包含fastcgi_pass或proxy_pass的Locatio ...

  10. lintcode:最大子数组差

    题目 最大子数组差 给定一个整数数组,找出两个不重叠的子数组A和B,使两个子数组和的差的绝对值|SUM(A) - SUM(B)|最大. 返回这个最大的差值. 样例 给出数组[1, 2, -3, 1], ...