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: 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
 Copy sample input to clipboard
2
3
ATCG
TAGC
TAGG
2
AATT
ATTA

Sample Output
10

Problem Source: ZSUACM Team Member

我的解法:
#include <iostream>
#include <string>
#include <vector>
using namespace std;


int myComp(const string &s1,const string &s2) //自定义匹配函数
{
for(int x=0;x!=s1.length();x++)
{
if((s1[x]=='A'&&s2[x]=='T')||(s1[x]=='T'&&s2[x]=='A')||(s1[x]=='C'&&s2[x]=='G')||(s1[x]=='G'&&s2[x]=='C'))
continue;
else
return 0;
}
return 1;
}


int main()
{
int iCount;
cin>>iCount;        //获取测试数据的组数
for(int i=0;i<iCount;i++)
{
int num;
string tmp;      
cin>>num;         //每组数据的多少
vector<string> vStr;  //string类型的向量,存储DNA单链
for(int y=0;y<num;y++)
{
cin>>tmp;
vStr.push_back(tmp);
}
int totalNum=0;    //记录符合条件的DNA对数量
vector<string>::iterator it,itt;
for(it=vStr.begin();it!=vStr.end();it++)   //遍历向量,逐一匹配
{
for(itt=it+1;itt!=vStr.end();itt++)
{
if((*it).length()==(*itt).length()) //长度符合再进行匹配
if(myComp(*it,*itt))
{
totalNum++;
*itt="00";  //符合条件的两个单链都至00,不再参与其他匹配
*it="00";
}
}
}

cout<<totalNum<<endl;
}
return 0;
}

















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值