poj 1699(2013@USC D题)
来源:互联网 发布:网络运行情况通报 编辑:程序博客网 时间:2024/05/22 06:15
Best Sequence
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 4119 Accepted: 1634
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).
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
15TCGGGCAGCCGCGATCATCG
Sample Output
11
DFS暴搜
网上的很多解题报告都说明这道题是按位DP。。
不过因为最开始做这道题的时候,觉得搜索直接可以过,按照这样的想法码下来就过了~
重点在于在搜索之前,先预处理一下:求出任意两组字符串头尾匹配以及尾头匹配的最大长度。
这样处理之后,就可以直接暴搜了。。。
求字符串按照怎样的序列排序后,可以使每个重合长度达到最大。
这也就是枚举每一中不同序列的重合长度,求出重合最多的。
代码:
#include<iostream>#include<stdio.h>#include<string.h>#include<fstream>using namespace std;struct node{ char cha[25]; int len;};node a[20];int minn;int n;int flag[100];int dp[25][25];int compar(int a1,int a2){ int g,k; for(int j=0;j<a[a1].len;j++){ for(k=j,g=0;k<a[a1].len&&g<a[a2].len;k++,g++){ if(a[a1].cha[k]!=a[a2].cha[g]){ break; } } if(k==a[a1].len){ return g; } } return 0;}int DFS(int start,int num,int minlen){ if(num==n){ if(minn>minlen){ minn=minlen; // cout<<minlen<<endl; } return 0; } for(int j=0;j<n;j++){ if(flag[j]==0){ flag[j]=1; int templen=0; if(num==0) templen=a[j].len; else{ templen=minlen+a[j].len-dp[start][j]; // cout<<num<<' '<<templen<<endl; // cout<<templen<<endl; } DFS(j,num+1,templen); flag[j]=0; } } return 0;}int main(){ // freopen("poj 1699.in","r",stdin); // freopen("poj 1699.out","w",stdout); int m; cin>>m; while(m--){ // int n; cin>>n; minn=100000; memset(a,0,sizeof(a)); memset(flag,0,sizeof(flag)); for(int i=0;i<n;i++){ scanf("%s",a[i].cha); int lenth=strlen(a[i].cha); a[i].len=lenth; } for(int i=0;i<n;i++){ for(int h=0;h<n;h++){ dp[i][h]=compar(i,h); // cout<<i<<" "<<h<<" "<<dp[i][h]<<endl; } } //flag[0]=1; DFS(0,0,0); cout<<minn<<endl; } return 0;}
- poj 1699(2013@USC D题)
- POJ1699 Best Sequence 暨2013年USC校赛D题
- OutLook 2013 添加 USC gmail 邮箱 解决国内USC邮箱打不开
- USC 20121007 组队赛 H题 矩阵乘法
- 题目: poj – 3669(D题)
- USC Computer Vision
- USC newweek2 G
- Training@USC Greedy Tino
- training@USC 12 2
- training@USC permutation
- 讲课@USC 杂谈
- USC 1143: 显示器
- usc@1002:项目投标
- usc@1113:奖学金
- 出国体检 西安 USC
- USC oj 1404: Search
- poj 2372 D++ Again
- D -- POJ 1363 Rails
- fedora下安装thrift
- POJ 1503 大整数相加的问题
- HTML5有意思的实现
- Android UI 使用更快更高效
- 二分查找法
- poj 1699(2013@USC D题)
- “Windows XP下vs2010中配置OpenCV2.4.3 ”的补充
- tiny6410开机自启动QT4程序,并设置程序为全屏显示
- jsp原理笔记(四)
- jsp标签自动提示,提高编程效率,自动提示
- 数据库内容介绍
- 概率统计方面的底层需要实现的相关函数
- VC单文档分割窗口4--锁定分割条
- HDU1151有向图的最小路径覆盖