Sicily 1035 DNA matching

来源:互联网 发布:c语言经典程序100道 编辑:程序博客网 时间:2024/05/17 22:00

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

题意解析:就是输出匹配的DNA的对数,已经匹配的DNA则不能重复计算。

我的想法是:用一个链表来存储DNA,再判断这个链表中是否有匹配的DNA,若存在匹配的DNA,则双双从list中删除,若不存在,则继续遍历链表。
如果遍历完整个链表都不存在与第一个元素匹配的,则删掉第一个元素,然后再循环遍历list操作,最后可能会出现在list中只有一个元素的情况,此时的next指针指向end,则结束循环。


代码见下:
// Problem#: 1035// Submission#: 2938955// 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 <map>#include <string>#include <list>using namespace std;bool isEqual(string a, string b){    if (a.length() != b.length())    {        return false;    }    int i;    for (i = 0; i < a.length(); )    {        if (a[i] == 'A' && b[i] == 'T')            i++;        else if (a[i] == 'T' && b[i] == 'A')            i++;        else if (a[i] == 'C' && b[i] == 'G')            i++;        else if (a[i] == 'G' && b[i] == 'C')            i++;        else            break;    }    if (i == a.length())    {        return true;    }    else    {        return false;    }}int main(){    int T;    cin >> T;    while (T--)    {        list<string> tmp;        list<string>::iterator it;        list<string>::iterator next;        int n, count=0;        cin >> n;        for (int i = 0; i < n; i++)        {            string s;            cin >> s;            tmp.push_back(s);        }        it = tmp.begin();        next = tmp.begin();        next++;        while (!tmp.empty())        {            if (next == tmp.end()) // 当list只有一个元素的情况            {                break;            }            if (isEqual(*it, *next)) // 如果找到匹配的,则count自增,且list中删掉两个匹配的元素,且重置it为新list的首元素            {                count++;                tmp.erase(it);                tmp.erase(next);                                if (tmp.empty()) // 若空则跳出循环,结束                {                    break;                }                it = tmp.begin();                next = tmp.begin();                next++;             }            else // 如果没找到匹配的,则继续往后移动指针,继续找            {                next++;                if (next == tmp.end()) // 如果遍历完了都没找到匹配的,则删掉list首元素,重新设置首元素                {                    tmp.erase(it);                    it = tmp.begin();                    next = tmp.begin();                    next++;                }            }        }        cout << count << endl;    }}                                 

 
0 0
原创粉丝点击