Sicily 1035. DNA matching

Source: Internet
Author: User

Description

DNA (deoxyribonucleic acid) is founded in every living creature as the storage medium for genetic information. it is comprised of subunits called Nucleus otides that are strung together into polymer chains. DNA polymer chains are more commonly called DNA strands.

There are four kinds of nucleus otides in DNA, distinguished by the chemical group, or base attached to it. the four bases are adenine, guanine, cytosine and thymine, abbreviated as A, G, C and T (these letters will be used to refer to nucleus otides containing these bases ). single nucleus otides are linked together end-to-end to form DNA single strands via chemical reactions. for simplicity, we can use a string composed of letters A, T, C and G to denote a single strand, such as attcgac, but we must also note that the sequence of nucleus otides in any strand has a natural orientation, so attcgac and cagctta can not be viewed as identical strands.

DNA does not usually exist in nature as free single strands, though. under appropriate conditions single strands will pair up and twist around each other, forming the famous double helix structure. this pairing occurs because of a mutual attraction, call hydrogen bonding, that exists between AS and Ts, and between GS and CS. hence a/t and G/C are called complementary base pairs.

In the molecular biology experiments dealing with DNA, one important process is to match two complementary single strands, and make a DNA Double Strand. here we give the constraint that two complementary single strands must have equal length, and the nucleus otides in the same position of the two single strands shoshould be complementary pairs. for example, attcgac and taagctg are complementary, but cagctta and taagctg are not, neither are attcgac and gtaagct.

As a biology research assistant, your boss has assigned you a job: givn single strands, find out the maximum number of double strands that cocould be made (of course each strand can be used at most once ). if n is small, of course you can find the answer with the help of pen and paper, however, sometimes n cocould be quite large... Fortunately you are good at programming and there is a computer in front of you, so you can write a program to help yourself. but you must know that you have your other assignments to finish, and you shoshould not waste too much time here, so, hurry up please!

Input

Input may contain multiple test cases. the first line is a positive integer T (t <= 20), indicating the number of test cases followed. in each test case, the first line is a positive integer n (n <= 100), denoting the number of single strands below. and n lines follow, each line is a string comprised of four kinds of capital letters, A, T, C and G. the length of each string is no more than 100.

Output

For each test case, the output is one line containing a single integer, the maximum number of double strands that can be formed using those given single strands.

Sample Input

2
3
Atcg
Tagc
Tagg
2
Aatt
Atta

Sample output

1
0
D

 

In fact, it seems that there is no technical content. You can read the questions and clarify your ideas ......

Question:

DNA single-chain matching into double-chain, a with T, C with g, with a single-chain cannot be reused, the matching single-chain must be equal to the length and each location must match, the direction of a single link cannot be changed (CTA is not equal to ATC ). Each test case is given with n single-chain links and asks how many double links can be matched at most.

In order to facilitate pruning and sort by length, the loop is stopped when a long interval is left while searching backward. If a single bool array record is used, the size of each string is cached in an int array. Because the data volume is small, the pruning + dual loop can pass through, and the complexity is about O (n2l) (L is the string length ).

Follow-up: it seems that the dictionary tree can be reduced to O (NL) If the chain is used to create a dictionary tree)

#include<iostream>#include<cstdio>#include<string>#include<algorithm>using namespace std;bool cmp(string a, string b) {    return a.size() > b.size();}bool match(string a, string b) {    int len = a.size();    for (int i = 0; i < len; ++i) {        if (a[i] == ‘A‘ && b[i] != ‘T‘)            return false;        if (a[i] == ‘T‘ && b[i] != ‘A‘)            return false;        if (a[i] == ‘C‘ && b[i] != ‘G‘)            return false;        if (a[i] == ‘G‘ && b[i] != ‘C‘)            return false;    }    return true; }int main(void) {    int t, n;    string str[101];    int len[101];    bool used[101];    // for each test case: (t<=20)      cin >> t;    while(t--) {        // scan and store n strings(n <= 100)        cin >> n;        for (int i = 0; i < n; ++i) {            cin >> str[i];        }                // sort by length        sort(str, str + n, cmp);        for (int i = 0; i < n; ++i) {            // store length            len[i] = str[i].size();            // init used            used[i] = false;        }                int count = 0;                for (int i = 0; i < n; ++i) {            if (!used[i]) {                for (int j = i + 1; j < n && len[j] == len[i]; ++j) {                    if (match(str[i], str[j]) && !used[j]) {                        count++;                        used[i] = used[j] = true;                        break;                    }                }            }        }                cout << count << ‘\n‘;    }    return 0;} 

Sicily 1035. DNA matching

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.