poj 1699 Best Sequence(dfs)
来源:互联网 发布:如何查询域名备案信息 编辑:程序博客网 时间:2024/04/29 03:14
http://poj.org/problem?id=1699
题意:给出n个只含A,C,G,T的字符串,要求能把这n个字符串组合起来的最短长度。
思路:预处理一下,a[i][j]表示将第j个字符串连接到第i个字符串后面增加的长度,那么我们需要找出这样一个序列1,2....n满足a[1][2] + a[2][3] + ...+a[n-1][n]的最小值。DFS就OK了,任选一个字符串作为起点进行dfs,求出所有的情况后得到最小的长度。
#include <stdio.h>#include <iostream>#include <algorithm>#include <set>#include <map>#include <vector>#include <math.h>#include <string.h>#include <queue>#include <string>#define LL long long#define _LL __int64#define eps 1e-8using namespace std;const int INF = 0x3f3f3f3f;char s[12][22];int n;int a[12][12];int vis[12];int ans; //维护最短长度int cal(char s1[], char s2[]){ int len1 = strlen(s1); int len2 = strlen(s2); int i,j,t; int flag; if(strcmp(s1,s2) == 0) return 0; for(int k = 1; k <= len2; k++) { t = len2 - k; i = len1 - t; j = 0; flag = 1; while(i < len1 && j < t) { if(s1[i] == s2[j]) { i++; j++; } else { flag = 0; break; } } if(flag == 1) return k; }}void solve(){ for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { if(i == j) continue; a[i][j] = cal(s[i],s[j]); } }}void dfs(int x, int step, int sum){ if(step > ans) //剪枝,当step > ans时没不要继续递归了、 return; if(sum == n) { ans = min(ans,step); return; } for(int i = 0; i < n; i++) { if(!vis[i]) { vis[i] = 1; dfs(i,step + a[x][i], sum+1); vis[i] = 0; } }}int main(){ int test; scanf("%d",&test); while(test--) { scanf("%d",&n); ans = 0; for(int i = 0; i < n; i++) { scanf("%s",s[i]); ans += strlen(s[i]); } solve(); //预处理求出a[][] memset(vis,0,sizeof(vis)); for(int i = 0; i < n; i++) { vis[i] = 1; dfs(i,strlen(s[i]),1); vis[i] = 0; } printf("%d\n",ans); } return 0;}
0 0
- poj 1699 Best Sequence(dfs)
- poj-1699-Best Sequence-dfs+子状态
- POJ 1699 Best Sequence (DFS+预处理)
- POJ--1699--Best Sequence【扩展KMP+DFS】
- POJ--1699 Best Sequence(DP+dfs)
- [poj 1699] Best Sequence dfs+剪支
- poj 1699 Best Sequence(dfs)
- poj 1699 Best Sequence(dfs+A(n,n))
- poj 1699—Best Sequence(dfs剪枝)
- poj 1699 Best Sequence
- poj 1699 Best Sequence
- POJ 1699 Best Sequence
- poj 1699 Best Sequence
- POJ 1699 Best Sequence
- poj 1699 Best Sequence
- POJ 1699 Best Sequence
- POJ 1699 Best Sequence
- POJ 1699 Best Sequence
- response对象
- linux内存磁盘初始化技术
- 学习OpenCV 第二版<1-2>:Who Uses OpenCV?
- javascript介绍及其简单例子(6)
- 《Unix-Linux编程实践教程》读书笔记(十一)
- poj 1699 Best Sequence(dfs)
- C++ 模板详解
- 通过文件描述符恢复误删文件
- hdu3068(最长回文manacher)
- python esay_install的下载,安装,配置
- 循环-02. 计算圆周率(15)
- freemarker自定义标签报错(四)
- javascript实现倒计时-------Day28
- java.lang.ClassNotFoundException: net.sf.ezmorph.Morpher