Phylogenetic Trees Inherited

Time Limit: 3000MS

Memory Limit: 65536K

Total Submissions: 480

Accepted: 297

Special Judge

Description

Among other things,Computational Molecular Biology deals with processing genetic sequences.Considering the evolutionary relationship of two sequences, we can say thatthey are closely related if they do not differ very much. We might representthe
relationship by a tree, putting sequences from ancestors above sequencesfrom their descendants. Such trees are called phylogenetic trees.


Whereas one task of phylogenetics is to infer a tree from given sequences,we'll simplify things a bit and provide a tree structure - this will be acomplete binary tree. You'll be given the n leaves of the tree. Sure you know,n is always a power of 2. Each leaf
is a sequence of amino acids (designated bythe one-character-codes you can see in the figure). All sequences will be ofequal length l. Your task is to derive the sequence of a common ancestor withminimal costs.

Amino Acid

Alanine

Ala

A

Arginine

Arg

R

Asparagine

Asn

N

Aspartic Acid

Asp

D

Cysteine

Cys

C

Glutamine

Gln

Q

Glutamic Acid

Glu

E

Glycine

Gly

G

Histidine

His

H

Isoleucine

Ile

I

Amino Acid

Leucine

Leu

L

Lysine

Lys

K

Methionine

Met

M

Phenylalanine

Phe

F

Proline

Pro

P

Serine

Ser

S

Threonine

Thr

T

Tryptophan

Trp

W

Tyrosine

Tyr

Y

Valine

Val

V

The costs are determined asfollows: every inner node of the tree is marked with a sequence of length l,the cost of an edge of the tree is the number of positions at which the twosequences at the ends of the edge differ, the total cost is the
sum of the costsat all edges. The sequence of a common ancestor of all sequences is then foundat the root of the tree. An optimal common ancestor is a common ancestor withminimal total costs.

Input

The input file containsseveral test cases. Each test case starts with two integers n and l, denotingthe number of sequences at the leaves and their length, respectively. Input isterminated by n=l=0. Otherwise, 1<=n<=1024 and 1<=l<=1000. Thenfollow
n words of length l over the amino acid alphabet. They represent the leavesof a complete binary tree, from left to right.

Output

For each test case, output aline containing some optimal common ancestor and the minimal total costs.

Sample Input

4 3

AAG

AAA

GGA

AGA

4 3

AAG

AGA

AAA

GGA

4 3

AAG

GGA

AAA

AGA

4 1

A

R

A

R

2 1

W

W

2 1

W

Y

1 1

Q

0 0

Sample Output

AGA 3
AGA 4
AGA 4
R 2
W 0
Y 1
Q 0

Source

field=source&key=Ulm+Local+2000">UlmLocal 2000

【题目大意】给出全然二叉树的全部叶子节点,每一个叶子节点上的字符串的长度是固定长度的,分别比較两个孩子的字符串,选取合适的字符串作为这个孩子的父节点。假如两个叶子节点各自是AAG,GAG。那么我们的父节点能够选择AAG或者GAG,由于他们的权值为1(权值是由父节点依据字符串字母的顺序依次与两个孩子比較的区别之和),由叶子节点依次往根节点求值,求得最小值,并打印出字符串,假设存在多个字符串具有同样的值。仅仅需打印出当中一个就可以;

【分析:】由于每一个字符串的长度都是一样的,那么我们能够每次取一个字母进行求值,然后遍历字符串的长度。依次获得字符串每一位的和;针对一个字母。每次求父节点的最大值为1,就是两个都不同样,并且仅仅要两个都同样。我们必须取同样的字母就可以获得最小的值。

看到全然二叉树。显然是要用数组来实现,与之前的一层一层稍有不同的是,每一层分别要算出全部父节点的状态值;

由于不须要遍历状态值,所以全部的元素仅仅须要依照其自然顺序就可以。即Z的下标为Z-‘A’;

Java代码例如以下:

import java.util.Arrays;
import java.util.Scanner; public class Main { // 节点的最大个数是2*1024-1,
private int dp[][]= new int[1000][2*1024]; private int arrayLen;
private int stringLen; public void initial(int arrayLen, int stringLen) {
this.arrayLen = arrayLen;
this.stringLen = stringLen; for (int i = 0; i < stringLen; i++) {
Arrays.fill(dp[i], 0,arrayLen,0);
}
// Arrays.fill(value, 0,arrayLen,0);
} public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner cin = new Scanner(System.in); int m, n;// 节点数和字符串长度
m = cin.nextInt();
n = cin.nextInt(); Main ma = new Main();
String str = null; while (!(m == 0 && n == 0)) {
ma.initial(2 * m, n);
for (int i = 0; i < m; i++) {
str = cin.next();
for (int j = 0; j < n; j++) {
ma.dp[j][m + i] = 1 << ((int) (str.charAt(j) - 'A'));
}
} if (m == 1) {
System.out.println(str + " 0");
} else {
ma.getMinValueAndPrint();
}
m = cin.nextInt();
n = cin.nextInt();
}
} private void getMinValueAndPrint() { // 从第0个字母開始
int temp;
int countSum=0;
for (int i = 0; i < stringLen; i++) {
temp = arrayLen / 4;
while (temp >= 1) {
for (int j = temp; j < temp * 2; j++) {
if ((dp[i][2 * j] & dp[i][2 * j + 1]) == 0) {
dp[i][j] = dp[i][2 * j] | dp[i][2 * j + 1];
countSum++; } else {
dp[i][j] = dp[i][2 * j] & dp[i][2 * j + 1]; }
}
temp = temp/2;
}
} temp = 0;
StringBuilder sb = new StringBuilder(); for (int i = 0; i < stringLen; i++) {
// 26表示26个字母;
temp = dp[i][1];
for (int count = 0; count < 26; count++) {
if ((temp & 1) != 0) {
sb.append((char)(count + 'A'));
break;
}
temp = temp>>1;
}
}
System.out.println(sb.toString() + " " + countSum); }
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

Chapter06-Phylogenetic Trees Inherited(POJ 2414)(减少国家DP)的更多相关文章

  1. poj - 1170 - Shopping Offers(减少国家dp)

    意甲冠军:b(0 <= b <= 5)商品的种类,每个人都有一个标签c(1 <= c <= 999),有需要购买若干k(1 <= k <=5),有一个单价p(1 & ...

  2. poj 1185 火炮 (减少国家DP)

    火炮 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19690   Accepted: 7602 Description 司 ...

  3. [ACM] hdu 5045 Contest (减少国家Dp)

    Contest Problem Description In the ACM International Collegiate Programming Contest, each team consi ...

  4. Light OJ 1406 Assassin`s Creed 减少国家DP+支撑点甚至通缩+最小路径覆盖

    标题来源:problem=1406">Light OJ 1406 Assassin`s Creed 意甲冠军:向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路: ...

  5. HDU 4433 locker 2012 Asia Tianjin Regional Contest 减少国家DP

    意甲冠军:给定的长度可达1000数的顺序,图像password像锁.可以上下滑动,同时会0-9周期. 每个操作.最多三个数字连续操作.现在给出的起始序列和靶序列,获得操作的最小数量,从起始序列与靶序列 ...

  6. POJ.3624 Charm Bracelet(DP 01背包)

    POJ.3624 Charm Bracelet(DP 01背包) 题意分析 裸01背包 代码总览 #include <iostream> #include <cstdio> # ...

  7. POJ 2995 Brackets 区间DP

    POJ 2995 Brackets 区间DP 题意 大意:给你一个字符串,询问这个字符串满足要求的有多少,()和[]都是一个匹配.需要注意的是这里的匹配规则. 解题思路 区间DP,开始自己没想到是区间 ...

  8. POJ 2411 Mondriaan&#39;s Dream (dp + 减少国家)

    链接:http://poj.org/problem?id=2411 题意:题目描写叙述:用1*2 的矩形通过组合拼成大矩形.求拼成指定的大矩形有几种拼法. 參考博客:http://blog.csdn. ...

  9. hdu 4778 Rabbit Kingdom(减少国家)

    题目链接:hdu 4778 Rabbit Kingdom 题目大意:Alice和Bob玩游戏,有一个炉子.能够将S个同样颜色的宝石换成一个魔法石.如今有B个包,每一个包里有若干个宝石,给出宝石的颜色. ...

随机推荐

  1. c++多态的案例分析

    近期在研究c++中多态的应用 ,当中遇到些许的疑问与问题,可是终于的结果是不容置疑的,以下记录下我的学习过程,以纪念本个知识点. 首先,是从一个案例開始的,题目大意是这种: 设定一个多边形的公共类,然 ...

  2. 新 Netflix 开源门户

    Netflix 开源改革计划:新 Netflix 开源门户 http://www.oschina.net/news/67555/evolution-of-open-source-at-netflix ...

  3. TP 控制器扩展_initialize方法实现原理

    参考网址:http://gongwen.sinaapp.com/article-59.html 控制器扩展接口 系统Action类提供了一个初始化方法_initialize接口,可以用于扩展需要,_i ...

  4. iOS很重要的 block回调

    刚刚进入ios开发行业,发现开发中要用到大量的block回调,由此可见它的重要性.学习它之前我也是网上找的资料,推荐这篇文章http://blog.csdn.net/mobanchengshuang/ ...

  5. hdu1532 (最大流入门,EK算法)

    看着这个博客 然后敲了hdu1532这个入门题,算是对最大流有点理解了 #include <stdio.h> #include <string.h> #include < ...

  6. 从零开始学Xamarin.Forms(四) Android 准备步骤(添加第三方Xamarin.Forms.Labs库)

    原文:从零开始学Xamarin.Forms(四) Android 准备步骤(添加第三方Xamarin.Forms.Labs库)  1.安装对应dll     Update-Package Xama ...

  7. 自定义ComboBox,简简单单实现

    private void Button_Click(object sender, RoutedEventArgs e) { Popup1.PlacementTarget = TesTextBox; P ...

  8. J2SE基础:1.类和对象基础

    什么是对象 在Java语言,全部的人,事物或者模块都是一个对象. 同样的对象具有一些同样的特性. 狗,猫,蛇3个对象(动物的对象) 苹果,梨,桔子3个对象(水果的对象) 什么是类 能够将现实生活中的对 ...

  9. CMD经常使用的命令

    Win7Excuting订单 win+R.运行该快捷方式.下面3一个人必须知道: ping 它是用来检查网络是否通畅或者网络连接速度的命令. 作为一个生活在网络上的管理员或者黑客来说,ping命令是第 ...

  10. HDU 1828 Picture(长方形的周长和)

    HDU 1828 Picture 题目链接 题意:给定n个矩形,输出矩形周长并 思路:利用线段树去维护,分别从4个方向扫一次,每次多一段的时候,就查询该段未被覆盖的区间长度,然后周长就加上这个长度,4 ...