SOJ 1035

来源:互联网 发布:淘宝代充平台 编辑:程序博客网 时间:2024/06/05 06:36

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 is comprised of subunits called nucleotides that are strung together into polymer chains. DNA polymer chains are more commonly called DNA strands.

    There are four kinds of nucleotides 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 nucleotides containing these bases). Single nucleotides 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 nucleotides 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 nucleotides in the same position of the two single strands should 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: given n single strands, find out the maximum number of double strands that could 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 could 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 many other assignments to finish, and you should 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

23ATCGTAGCTAGG2AATTATTA

Sample Output

10

Problem Source

ZSUACM Team Member


  比较简单的题目,即给你n个字符串,寻找其中最大的成对数。之所以简单是因为如果s1字符串与多个字符串匹配,可以选择其中任意一个进行匹配并且不会对接下来的操作造成影响。

  可以用一个容器来存储字符串,将字符串存入时先行判断容器中是否有与将存入字符串成对的存在,若有则将其从容器中删除(因为每个字符串只能用一次),然后成对数加一,若没有则直接存入。

// Problem#: 1035// Submission#: 5054941// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University#include<iostream>#include<vector>#include<string>#include<iterator>#include<algorithm>using namespace std;string temp;//用来保存与某个DNA相匹配的字符串 int T,n;//T为case数,n为DNA字符串的数量 vector<string> DNA;//用来保存DNA字符串的容器 string &makePair(string &A)//这一函数用来计算与A字符串相匹配的字符串,并保存在temp中 {    temp="";    for(int i=0;i<A.size();++i)    {        if(A[i]=='A') temp+='T';        else if(A[i]=='G') temp+='C';        else if(A[i]=='T') temp+='A';        else temp+='G';    }    return temp;}int answer(){    int count=0;//成对的字符串数量被初始化为0     DNA.clear();    string input;    for(int i=0;i<n;++i)    {        cin>>input;        vector<string>::iterator iter=find(DNA.begin(),DNA.end(),makePair(input));//输入字符串为input,在容器中查找是否有其配对字符串存在         if(iter==DNA.end()) DNA.push_back(input);//若不存在,则将input存入         else//若存在,则将成对数量增一,由于每个字符串只能用一次,所以input不再存入,并将其成对字符串从容器中删除         {            DNA.erase(iter);            count++;        }    }    return count;}int main(){    cin>>T;    for(int i=0;i<T;++i)    {        cin>>n;        cout<<answer()<<endl;;    }    return 0;}                                 

0 0
原创粉丝点击