Sicily 1035 DNA Matching

Source: Internet
Author: User

1035. DNA Matching Constraints

Time limit:1 secs, Memory limit:32 MB

Description

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

    there is four kinds of nucleotides in DNA, distinguished by the chemical group, or base Attached to it. The four bases is adenine, guanine, cytosine and thymine, abbreviated as A, G, C and T (these letters would be used to Refe R to nucleotides containing these bases). Single nucleotides is 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 m UST also note that the sequence of nucleotides in any strand have a natural orientation, so attcgac and Cagctta can is Viewed as identical strands.

DNA does not usually exist in nature as free single strands, though. Under appropriate conditions single strands would pair up and twist around all other, forming the famous double helix Stru Cture. This pairing occurs because of a mutual attraction, call hydrogen bonding, that exists between as and Ts, and between Gs a nd Cs. Hence A/T and g/c are called complementary base pairs.

In the Molecular biology experiments dealing with DNA, one important process was to match both complementary single strands, And make a DNA double strand. Here we give the constraint, complementary a strands must has equal length, and the nucleotides in the same Position of the strands should be complementary pairs. For example, ATTCGAC and TAAGCTG is complementary, but Cagctta and TAAGCTG is not, neither is ATTCGAC and GTAAGCT.

As a biology assistant, your boss have assigned you a Job:given n single strands, find out the maximum number of Double strands that could is made (of course each strand can is used at the most once). If n is small, of course you can find the answer with the help of pen and paper, however, sometimes n could be quite large ... Fortunately you be good at programming and there be a computer in front of your, so can write a program to help Yourse Lf. But you must know the many other assignments to finish, and we should not waste too much time here, so, hurry U P 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. The first line is a positive an integer n (n<=100), denoting the number of single strands below. and n lines follow, each of the line are a string comprised of four kinds of capital letters, A, T, C and G. The length of each string was no more than 100.

Output

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

Sample Input
23atcgtagctagg2aattatta
Sample Output
10
Problem Source

ZSUACM Team Member

This problem because its order of magnitude is relatively small, so the idea is relatively simple, the use of double loop traversal tag can be.

The map correlation function m is defined first, and the corresponding relationship between each nucleic acid is defined, i.e. m[' A ']= ' T '; m[' T ']= ' A '; m[' C ']= ' G '; m[' G ']= ' C ';

Define the corresponding relationship after the design to determine whether the two string matches the function match, when the length of the two strings are not the same, it must not match, return false; When traversing a string, a pair of characters does not necessarily match, returns false, and returns true in other cases;

Then the double loop iterates through all the input strings, the inner loop starts at the string that the outer loop reaches, and when the two strings are not matched and the match succeeds, the inner loop to the string is assigned the value "matched", the counter counter plus one, and then ends the inner loop, Because the outer loop string is no longer traversed, the string does not have to be marked.

The final output of the resulting counter value can be.

The code is as follows:

#include <iostream> #include <map> #include <string>using namespace Std;map<char,char>m;bool    Match (string a,string b) {if (A.size ()!=b.size ()) {return false;        } else {for (int i=0;i<a.size (); i++) {if (M[a[i]]!=b[i]) return false;    } return true;    }}int Main () {int t;    cin>>t;    m[' A ']= ' T ';    m[' T ']= ' A ';    m[' C ']= ' G ';    m[' G ']= ' C ';        while (t--) {int n;        cin>>n;        String str[100];        for (int k=0;k<n;k++) {cin>>str[k];        } int counter=0;                for (int i=0;i<n;i++) {if (str[i]!= "matched") {for (int j=i+1;j<n;j++)                        {if (str[j]!= "matched" && match (Str[j],str[i])) {                        counter++;                        str[j]= "Matched";                    Break  }                }          }} cout<<counter<<endl;                                  } 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.