HDU 1560 DNA sequence (IDA *)

Source: Internet
Author: User
DNA sequence

Time Limit: 15000/5000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)

Total submission (s): 721 accepted submission (s): 349 problem descriptionthe twenty-first century is a biology-technology developing century. we know that a gene is made of DNA. the nucleus otide bases from which DNA is built are a (adenine), C (cytosine), g (guanine), and T (thymine ). finding the longest common
Subsequence between DNA/protein sequences is one of the basic problems in modern computational molecular biology. but this problem is a little different. given several DNA sequences, you are asked to make a shortest sequence from them so that each of the given
Sequence is the subsequence of it.

For example, given "acgt", "atgc", "cgtt" and "cagt", you can make a sequence in the following way. it is the shortest but may not the only one.

 

Inputthe first line is the test case number t. then T test cases follow. in each case, the first line is an integer N (1 <= n <= 8) represents number of the DNA sequences. the following K lines contain the K sequences, one per line. assuming
That the length of any sequence is between 1 and 5.

Outputfor each test case, print a line containing the length of the shortest sequence that can be made from these sequences.

Sample Input

14ACGTATGCCGTTCAGT
 

Sample output

8. Meaning: it is easy to understand the concept: DFS enumerates all cases. Because the depth is unknown, you can use iterations to deepen the search. Feeling: I started to use the simple DFS algorithm to time out, so I thought of IDA * because an array is defined as a global variable, I 've been coding for a long time:
# Include <iostream> # include <cstdio> # include <cstring> # define maxn 10 using namespace STD; int N, ANS, depth; int Len [maxn], P [maxn]; char s [maxn] [maxn]; char DX [] = {'A', 'C', 'G','t '}; int geth () // A * function requires a minimum of several characters to meet the conditions {int I, j, t =-1, TT; for (I = 1; I <= N; I ++) {TT = Len [I]-P [I]; If (T <TT) t = tt;} return t ;} bool DFS (int pos) {int I, j, H, flag; int TMP [maxn]; // start to define this as a global variable. Debug: I have been debugging for a long time. Then, when response H = geth (); // it takes a few characters to get the minimum value. If (Pos + H> depth) return false; // if the minimum required character cannot meet the condition, the request is trimmed if (H = 0) {ans = Pos; return true;} for (I = 1; I <= N; I ++) TMP [I] = P [I]; for (I = 0; I <4; I ++) {flag = 0; // used for pruning (j = 1; j <= N; j ++) {If (s [J] [p [J] = DX [I]) {flag = 1; p [J] ++ ;}} if (! Flag) continue; // If P [J] does not change, the added letter will not use pruning if (DFS (Pos + 1) return true; For (j = 1; j <= N; j ++) P [J] = TMP [J];} return false;} int main () {int I, j, T; scanf ("% d", & T); While (t --) {scanf ("% d", & N); depth =-1; for (I = 1; I <= N; I ++) {scanf ("% s", s [I]); Len [I] = strlen (s [I]); P [I] = 0; If (depth <Len [I]) depth = Len [I];} while (! DFS (0) Depth ++; // ida * specifies the depth of printf ("% d \ n", ANS) each time;} 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.