POJ 1699 Best Sequence

Source: Internet
Author: User

Best SequenceTime Limit: 1000 MS Memory Limit: 10000 K Total Submissions: 4198 Accepted: 1662 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 nucleus otide 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 shocould 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 l Ength of the shortest sequence that can be made from these segments. sample Input 15 TCGGGCAGCCGCGATCATCGSample Output 11 Source POJ Monthly -- 2004.07.18 [Submit] [Go Back] [Status] [Discuss] Home Page Go Back To top ideas: remove another string that is completely contained in one string. The position of each string is preprocessed. Finally, the dfs is fully arranged to find the minimum answer.
# Include <iostream> # include <cstring> # include <cstdio> # include <cstdlib> # define INF 0x7ffffffusing namespace std; char s1 [20] [30], s2 [20] [30]; int len [20], pos [20] [20], pt [20]; bool status [20]; int n, Min; int main () {// freopen ("data1.in", "r", stdin); bool check (char str1 [30], char str2 [30]); void dfs (int k); int t; scanf ("% d", & t); while (t --) {int Top = 0; scanf ("% d ", & n); for (int I = 0; I <= n-1; I ++) {scanf ("% s", s 2 [I]) ;}for (int I = 0; I <= n-1; I ++) {int j; for (j = I + 1; j <= n-1; j ++) {int l1, l2; l1 = strlen (s2 [I]); l2 = strlen (s2 [j]); if (l1 = l2 & strcmp (s2 [I], s2 [j]) = 0) {break ;} else if (l1 <l2 & check (s2 [I], s2 [j]) {break ;}} if (j = n) {strcpy (s1 [Top ++], s2 [I]) ;}} n = Top; for (int I = 0; I <= n-1; I ++) {len [I] = strlen (s1 [I]) ;}for (int I = 0; I <= n-1; I ++) {for (int j = 0; j <= n-1; j ++) {if (I = j) {continue;} int x; for (x = 0; x <= Len [I]-1; x ++) {int y; for (y = x; y <= len [I]-1; y ++) {if (s1 [I] [y]! = S1 [j] [y-x]) {break ;}} if (y = len [I]) {break ;}} pos [I] [j] = x ;}} memset (status, false, sizeof (status); Min = INF; dfs (0 ); printf ("% d \ n", Min) ;}return 0 ;}// whether str1 is in str2 bool check (char str1 [30], char str2 [30]) {int l1 = strlen (str1); int l2 = strlen (str2); int I, j; for (I = 0; I <= l2-1; I ++) {for (j = I; j-I <= l1-1; j ++) {if (str2 [j]! = Str1 [j-I]) {break;} if (j-I = l1) {return true ;}} return false ;} void dfs (int k) {for (int I = 0; I <= n-1; I ++) {if (! Status [I]) {pt [k] = I; status [I] = true; if (k = N-1) {int s = len [pt [0]; for (int j = 1; j <= n-1; j ++) {int sum1 = len [pt [j]-(len [pt [J-1]-pos [pt [J-1] [pt [j]); s + = sum1 ;}min = Min (min, s) ;}else {dfs (k + 1) ;}status [I] = false ;}}}

 


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.