[sicily online]1035. DNA matching

來源:互聯網
上載者:User
/*用map,注意重複ConstraintsTime Limit: 1 secs, Memory Limit: 32 MBDescriptionDNA (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!InputInput 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.OutputFor 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 Input23ATCGTAGCTAGG2AATTATTASample Output10*/#include<iostream>#include<stdio.h>#include<cmath>#include<iomanip>#include <map>#include <vector>#include <string>#include <algorithm>#include <sstream>#include <stack>using namespace std;string getOp(string x){for(string::size_type i=0;i<x.size();i++){if(x[i]=='A')x[i]='T';else if(x[i]=='G')x[i]='C';else if(x[i]=='C')x[i]='G';else if(x[i]=='T')x[i]='A';}//end forreturn x;}int main(){int n;cin>>n;for(int xx=0;xx<n;xx++){int m;cin>>m;int count=0;map<string,int> data;for(int yy=0;yy<m;yy++){string tmp;cin>>tmp;data[tmp]++;}//end for yymap<string,int>::iterator ite;for(ite=data.begin();ite!=data.end();){string opStr=getOp(ite->first);map<string,int>::iterator iteTmp;iteTmp=data.find(opStr);if(iteTmp!=data.end()){count++;if((--iteTmp->second)==0)data.erase(iteTmp);if((--ite->second)==0)data.erase(ite);}//end ifelse{data.erase(data.begin());}//end elseif(data.begin()==data.end())break;ite=data.begin();}//end for itecout<<count<<endl;}//end for xx}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.