Sicily 1035 DNA matching

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Sicily 1035 DNA matching相关的知识,希望对你有一定的参考价值。

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

Problem Source

ZSUACM Team Member

 

这道题因为其数量级比较小,因而思路比较简单,采用双重循环遍历标记即可。

首先定义map关联函数m,定义各个核酸之间的对应关系,即m[‘A‘]=‘T‘; m[‘T‘]=‘A‘; m[‘C‘]=‘G‘; m[‘G‘]=‘C‘;

定义好对应关系后设计判断两个字符串匹配与否的函数match,当两字符串长度都不相同时一定不匹配,返回false;当遍历字符串过程中某一对字符不对应时一定不匹配,返回false;其他情况下返回true;

然后双重循环遍历输入的所有字符串,内层循环从外层循环所到达的字符串开始即可,当两个字符串均未被匹配过且相互匹配成功时,将内层循环到的字符串都赋值为“Matched”,计数器counter加一,然后结束此次内层循环即可,因为外层循环的字符串不会再被遍历到了所以该字符串不必标记。

最后输出得到的counter的值即可。

代码如下:

#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的主要内容,如果未能解决你的问题,请参考以下文章

Sicily 1176 Two Ends

Sicily 1046 Plane Spotting

Sicily 1020 Big Integer

Sicily 1027 MJ,Nowhere to Hide

Sicily 1198 Substring

Sicily 1020. Big Integer