sicily 1035. DNA matching

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: givn 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
d

 

其实好像没什么技术含量,读懂题目,理清思路就可以写了……

题目大意:

DNA单链匹配成双链,A配T,C配G,配过的单链不能再用,匹配的单链必须等长且每个位置都匹配,单链的方向不能改(CTA不等于ATC)。每个测试用例给定n条单链,问最多可以匹配出多少双链。

为了方便剪枝,按长度排序,向后查找时离开了等长区间就停止循环。开一个bool数组记录单链是否用过,一个int数组缓存各个字符串大小。因为数据量很小,剪枝+二重循环也能过,复杂度约为O(n2l)(l为字符串长度)。

追记:如果用补链建字典树似乎应该可以降到O(nl)

#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
using namespace std;

bool cmp(string a, string b) {
    return a.size() > b.size();
}

bool match(string a, string b) {
    int len = a.size();
    for (int i = 0; i < len; ++i) {
        if (a[i] == 'A' && b[i] != 'T')
            return false;
        if (a[i] == 'T' && b[i] != 'A')
            return false;
        if (a[i] == 'C' && b[i] != 'G')
            return false;
        if (a[i] == 'G' && b[i] != 'C')
            return false;
    }
    return true; 
}


int main(void) {
    int t, n;
    string str[101];
    int len[101];
    bool used[101];

    // for each test case: (t<=20)
      cin >> t;
    while(t--) {
        // scan and store n strings(n <= 100)
        cin >> n;
        for (int i = 0; i < n; ++i) {
            cin >> str[i];
        }
        
        // sort by length
        sort(str, str + n, cmp);

        for (int i = 0; i < n; ++i) {
            // store length
            len[i] = str[i].size();
            // init used
            used[i] = false;
        }
        
        int count = 0;
        
        for (int i = 0; i < n; ++i) {
            if (!used[i]) {
                for (int j = i + 1; j < n && len[j] == len[i]; ++j) {
                    if (match(str[i], str[j]) && !used[j]) {
                        count++;
                        used[i] = used[j] = true;
                        break;
                    }
                }
            }
        }
        
        cout << count << '\n';
    }

    return 0;
} 
posted @ 2014-09-24 23:05  Joyee  阅读(1319)  评论(0编辑  收藏  举报