Sicily 1035. DNA matching题解

来源:互联网 发布:腾讯手游模拟器mac版 编辑:程序博客网 时间:2024/06/01 20:08
题目:

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

2
3
ATCG
TAGC
TAGG
2
AATT
ATTA

Sample Output

1
0
题解:字符串匹配类的问题:DNA组成双螺旋的规则A-T、G-C,找出给出单螺旋能够组成双螺旋的最多数目。本题关键需要注意正向与反向匹配,即ATGC与TACG、GCAT均能够匹配,我使用map容器来完成规则A-T、G-C、T-A、C-G的映射关系,方便快捷。至于匹配算法就是两个循环。具体代码如下:
/*
 *@891785916@qq.com
 *@vanxin
*/
#include <iostream>#include <string>#include <map>using namespace std;int main(){//利用map容器建立映射规则map<char,char> rules;rules.insert(make_pair('A','T'));rules.insert(make_pair('T','A'));rules.insert(make_pair('G','C'));rules.insert(make_pair('C','G'));int count_number = 0;int case_number;cin>>case_number;while(case_number>0){int dna_number;cin>>dna_number;//动态设置数组大小string* dna_string = new string[dna_number];int* insist_array = new int[dna_number];for(int i = 0; i < dna_number; i++){cin>>dna_string[i];insist_array[i] = 0;}for(int i = 0; i < dna_number; i++){int length = dna_string[i].length();if(insist_array[i] == 0){for(int j = i+1; j < dna_number; j++){if(insist_array[j] == 0){//对比长度if(length == dna_string[j].length()){//逐个字符对比int k = 0;//正向匹配while(rules[dna_string[i][k]] == dna_string[j][k] && k < length)k++;if(k == length){count_number++;insist_array[i] = 1;insist_array[j] = 1;break;}//反向匹配k = 0;int k2 = length-1;while(k2 >= 0  && rules[dna_string[i][k]] == dna_string[j][k2]){k++;k2--;}if(k == length){count_number++;insist_array[i] = 1;insist_array[j] = 1;break;}}}}}}case_number--;cout<<count_number<<endl;count_number = 0;}return 0;}

原创粉丝点击