您的位置:首页 > 产品设计 > UI/UE

poj 1699 Best Sequence

2014-07-27 14:30 274 查看
Language:Default

Best Sequence

Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 4806Accepted: 1900
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


水dp,dp[i][j]表示j为起点,i为状态的最优值
int dis[10][10];
int dp[1<<10][10];
char s[10][22];
int t,n;

inline bool cmp(char *a,int i,char *b,int j,int l){
	rep(k,0,l){
		if(a[i+k]!=b[j+k]) return 0;
	}
	return 1;
}

inline int gao(int a,int b){
	if(a==b) return 0;
	int l1=strlen(s[a]),l2=strlen(s[b]);
	int l=min(l1,l2);
	red(i,l+1,0){
		if(cmp(s[a],l1-i,s[b],0,i)) return l1-i;
	}
	return l1;
}

int main(){
	t=input();
	while(t--){
		n=input();
		rep(i,0,n) scanf("%s",s[i]);
		rep(i,0,n) rep(j,0,n){
			dis[i][j]=gao(i,j);
		}
		
		clr(dp,-1);
		rep(i,0,n) dp[1<<i][i]=strlen(s[i]);
		int all=(1<<n);
		rep(i,0,all) rep(j,0,n){
			if(dp[i][j]!=-1 && (i&(1<<j))){
				rep(k,0,n){
					if(!(i&(1<<k))){
						int nxt=(i|(1<<k));
						int val=dp[i][j]+dis[k][j];
						if(dp[nxt][k]==-1 || dp[nxt][k]>val) dp[nxt][k]=val;
					}
				}
			}
		}
		
		int ans=200000;
		rep(i,0,n){
			if(dp[all-1][i]!=-1 && dp[all-1][i]<ans) ans=dp[all-1][i];
		}
		output(ans);
	}
	return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: