【POJ1699】-Best Sequence 搜索剪枝
来源:互联网 发布:node v6.3.0 x64.msi 编辑:程序博客网 时间:2024/05/23 21:25
Time Limit: 1000MS Memory Limit: 10000K
Description
The twenty-first century is a biology-technology developing century. One of the most attractive and challenging tasks is on the gene project, especially on gene sorting program. Recently we know that a gene is made of DNA. The nucleotide bases from which DNA is built are A(adenine), C(cytosine), G(guanine), and T(thymine). Given several segments of a gene, you are asked to make a shortest sequence from them. The sequence should use all the segments, and you cannot flip any of the segments.
For example, given ‘TCGG’, ‘GCAG’, ‘CCGC’, ‘GATC’ and ‘ATCG’, you can slide the segments in the following way and get a sequence of length 11. It is the shortest sequence (but may be not the only one).
Input
The first line is an integer T (1 <= T <= 20), which shows the number of the cases. Then T test cases follow. The first line of every test case contains an integer N (1 <= N <= 10), which represents the number of segments. The following N lines express N segments, respectively. Assuming that the length of any segment is between 1 and 20.
Output
For each test case, print a line containing the length of the shortest sequence that can be made from these segments.
Sample Input
1
5
TCGG
GCAG
CCGC
GATC
ATCG
Sample Output
11
Source
POJ Monthly–2004.07.18
题意:给你一些字符串,判断可以组成的最短的字符串。
思路:mp[i][j]表示j字符串接在i字符串的后面会增长多少,那么我们枚举起点的字符串,计算最短的字符串的长度。
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;char str[11][22];int mp[11][11];bool vis[11];int n,ans;int add(int s,int t){ int len1 = strlen(str[s]); int len2 = strlen(str[t]); for(int i = 0,j;i<len1;i++) { for(j = 0;j<len2 &&i+j<len1;j++) { if(str[s][i+j] !=str[t][j]) break; } if(i+j == len1) return len2-j; } return len2;}void DFS(int u,int num,int len){ if(len > ans) return ;//剪枝 if(num == n) { ans = min(ans,len); return ; } for(int i = 0;i<n;i++) { if(!vis[i]) { vis[i] = true; DFS(i,num+1,len+mp[u][i]); vis[i] = false; } }}int main(){ int T; scanf("%d",&T); while(T--) { scanf("%d",&n); for(int i = 0;i<n;i++) scanf("%s",str[i]); for(int i = 0;i<n;i++)//计算j接在i的后面的时候会增加多少。 for(int j =0;j<n;j++) mp[i][j] = add(i,j); memset(vis,false,sizeof(vis)); ans = 210; for(int i = 0;i<n;i++) { vis[i] = true; DFS(i,1,strlen(str[i])); vis[i]=false; } printf("%d\n",ans); } return 0;}
- 【POJ1699】-Best Sequence 搜索剪枝
- poj1699--Best Sequence(dfs+剪枝)
- POJ1699:Best Sequence(DP)
- POJ1699 Best Sequence DP DFS
- Best Sequence poj1699(dfs)
- POJ1699--Best Sequence(预处理+暴力)
- Best Sequence----dfs+剪枝
- POJ1699 Best Sequence 暨2013年USC校赛D题
- pku1699 Best Sequence (搜索)
- hdu 2610 Sequence one【搜索 dfs+剪枝】
- poj 1699—Best Sequence(dfs剪枝)
- 搜索剪枝
- 搜索剪枝
- 搜索 剪枝
- 搜索剪枝
- 搜索剪枝
- 剪枝搜索
- hdu1455 Sticks(搜索+剪枝+剪枝+.....+剪枝)
- Android 实现“调用手机已安装市场,进行软件评分的功能”
- android:layout_gravity和android:gravity的区别
- 166. Fraction to Recurring Decimal
- 从我自己的角度总结一下移动互联网的技术
- IOS 开发 Pods目录 中头文件编程链接的问题解决
- 【POJ1699】-Best Sequence 搜索剪枝
- Android系统架构
- Android动画效果translate、scale、alpha、rotate详解
- Openfire 的安装和配置
- Android5.0报错:java.lang.IllegalArgumentException: Service Intent must be explicit
- Android app1打开app2的实现
- Android:一个TextView分段改变样式、添加监听事件的实现方式(类似朋友圈评论)
- centos lnmp环境搭配tomcat和jdk
- lucene学习四:索引库的优化