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!
2 3 ATCG TAGC TAGG 2 AATT ATTA
1 0
解决:先找到 size 相同的,然后判断是否符合。注意每个序列只能组合一次,也就是遇到和它可以组合的以后就不能再和其他的进行组合了。
1 #include <iostream> 2 #include <map> 3 #include <algorithm> 4 #include <string> 5 6 using namespace std; 7 8 int main(int argc, char* argv[]) 9 { 10 map<char, char> Bind; 11 Bind['A'] = 'T'; 12 Bind['T'] = 'A'; 13 Bind['G'] = 'C'; 14 Bind['C'] = 'G'; 15 16 int T, strandsNum; 17 int num; 18 string nucleotide, str, str1; 19 cin >> T; 20 multimap<size_t, string>::iterator iterT; 21 while(T--) { 22 strandsNum = 0; 23 multimap<size_t, string> nucleotides; 24 cin >> num; 25 while (num--) { 26 cin >> nucleotide; 27 nucleotides.insert(pair<size_t, string>(nucleotide.size(), nucleotide)); 28 } 29 for (multimap<size_t, string>::iterator iter = nucleotides.begin(); iter != nucleotides.end();) { 30 if (iter->second != "") { 31 iterT = iter; 32 str = iter->second; 33 ++iter; 34 while (iter != nucleotides.end()) { 35 if (iter->first == iterT->first) { 36 if (iter->second != "") { 37 str1 = iter->second; 38 int i = 0; 39 for (; i != iter->first; ++i) { 40 if (str[i] != Bind[str1[i]]) { 41 break; 42 } 43 } 44 if (i == iter->first) { 45 strandsNum++; 46 iter->second = ""; 47 iterT->second = ""; 48 break; 49 } 50 } 51 } else { 52 break; 53 } 54 ++iter; 55 } 56 iter = iterT; 57 } 58 ++iter; 59 } 60 cout << strandsNum << endl; 61 } 62 63 return 0; 64 }