POJ-2414 phylogenetic trees inherited State compression, bit operation processing set operation

Source: Internet
Author: User

The problem solving process is very elegant. The bitwise operation is used to maintain an optional character set combination, and the Union and intersection are completed.

For details, see the code:

# Include <cstring> # include <cstdio> # include <cstdlib> # include <algorithm> # include <iostream> using namespace STD; /* it took more than half an hour to understand this question: given n strings, n is the power of 2, and each string has a length of L, the N strings are the leaf nodes of a Complete Binary Tree. Now, if the tree is to be filled with strings of the same length as l, what is the minimum cost, what is the root node. the cost is calculated as follows: the number of different strings at the two ends of an edge at the same position. if it is known that a leaf node is Aga, if we consider that the parent node is Aga, the cost is 1, of course, we also need to consider the difference between the parent and the other child and the difference solution between the two parents: Due to space reasons, we 'd better deal with each digit separately, instead of using multi-dimensional arrays to represent the status. so for each character above, we need to count the status of two leaf nodes pushing to the parent node. Obviously, we cannot get an optimal parent node status at a time, instead, you can only generate the status of each parent. that is, when the parent and child are different, which characters can be selected to fill this space? For example, if the left child: AAG, the right child: ata, then there is only one choice for the first parent node, that is,, this can be proved by greed. the second digit has two options (A, T ), we need to save these two options to prove that we can come together with our future brothers. Similarly, the third place also has two options. We also need to keep them (G,) since there are only uppercase letters, we can completely compress the state for retained processing. In short, the solution to this question is quite subtle */int n, L, t [2050]; char code [2050] [1050]; int main () {int ret, Lim; while (scanf ("% d", & N, & L ), N | L) {ret = 0; // used to record the cost of Lim = n <1; for (INT I = N; I <Lim; I ++) {scanf ("% s", code [I]) ;}for (INT p = 0; P <L; ++ p) {for (INT I = N; I <Lim; ++ I) {T [I] = 1 <(code [I] [p]-'A '); // here this bit of each character is a binary compression} For (INT I = N-1; I> 0; -- I) {int LCH = I <1, RCH = I <1 | 1; t [I] = T [LCH] & T [RCH]; // first perform an intersection of the two character sets if (T [I] = 0) {// It indicates that there are no identical optional sets, exchange a larger set of T [I] = T [LCH] | T [RCH]; ++ RET ;}} for (INT I = 0; I <26; ++ I) {If (T [1] & (1 <I) {putchar ('A' + I); break ;}}} printf ("% d \ n", RET);} return 0 ;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.