poj 1699 Best Sequence (搜索技巧 剪枝 dfs)
题意:给出几个基因片段,要求你将它们排列成一个最短的序列,序列中使用了所有的基因片段,而且不能翻转基因。
分析:先计算出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)的更多相关文章
- poj 1699 Best Sequence(AC自己主动机+如压力DP)
id=1699" target="_blank" style="">题目链接:poj 1699 Best Sequence 题目大意:给定N个D ...
- POJ 1699 Best Sequence (DFS+预处理)
意甲冠军:看图片是晶莹剔透的,正确的, N连接到第一序列(同样的序列部分).总序列获得最短. 主题链接:http://poj.org/problem?id=1699 ~~~~ 思路就是:将N个序列首尾 ...
- POJ 1699 Best Sequence dfs
题目: http://poj.org/problem?id=1699 无意间A了..超时一次,加了一句 if(len > ans)return; 然后就A了,dfs题,没有太多好说的,代码写的效 ...
- POJ 1699 Best Sequence(DFS)
題目鏈接 題意 : 將幾個片段如圖所示方法縮成一個序列,求出最短這個序列. 思路 : 其實我也不知道怎麼做.....看網上都用了DP.....但是我不會.....這個DP不錯,還有用KMP+状压DP做 ...
- poj 1699 Best Sequence
http://poj.org/problem?id=1699 题意:给你n个长度为L的序列,求包含这几个序列的最短长度. 先预处理每两个序列之间的关系,然后dfs枚举就行. #include < ...
- POJ - 3074 Sudoku (搜索)剪枝+位运算优化
In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgrids. For exa ...
- 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 ...
- hdoj1010 奇偶剪枝+DFS
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- 搜索(剪枝优化):HDU 5113 Black And White
Description In mathematics, the four color theorem, or the four color map theorem, states that, give ...
随机推荐
- Leetcode-Read N Characters Given Read4 II
The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...
- android 讯飞语音识别(离线)注意事项
讯飞语音识别:使用注意事项:mainfest.xml中一定要记得权限写进去21001:我的情况是没有写SpeechApp类,并且需要在application中注册:20005:无匹配结果23300:本 ...
- IP地址格式控制
/// <summary> /// 验证IP格式是否输入正确 /// </summary> /// <param name="ip"></ ...
- html5上传文件并监听进度
出处: http://blog.csdn.net/small_rice_/article/details/21391625
- hdu 1905 小数化分数2
;}
- Oracle 9 - redo和undo
1.redo redo 有在线redo日志和归档redo日志, 从Oracle 10g开始还新增加了flashback技术. 每个Oracle数据库至少有2个在线重做日志组,循环写. 只有INSERT ...
- http://my.oschina.net/u/1185331/blog/502350
http://my.oschina.net/u/1185331/blog/502350
- linux入门教程(九) 文本编辑工具vim
前面多次提到过vim这个东西,它是linux中必不可少的一个工具.没有它很多工作都无法完成.早期的Unix都是使用的vi作为系统默认的编辑器的.你也许会有疑问,vi与vim有什么区别?可以这样简单理解 ...
- lintcode:Subtree 子树
题目: 子树 有两个不同大小的二叉树: T1 有上百万的节点: T2 有好几百的节点.请设计一种算法,判定 T2 是否为 T1的子树. 样例 下面的例子中 T2 是 T1 的子树: 1 3 / \ / ...
- 【nginx网站性能优化篇(3)】反向代理实现负载均衡
注意,本篇文章为负载均衡的理论篇,后续找个机会推出实战篇.理论篇主要讲述如何配置负载均衡,配置负载均衡不难.难的是真正的实战,比如如何做到多服务器之间的数据共享(session,file等),多cac ...