【第21期】观点:人工智能到底用 GPU?还是用 FPGA?

【Sicily】1035. DNA matching

原创 2016年06月01日 10:17:37

一、题目

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

Problem Source

ZSUACM Team Member



二、题目的大意

DNA配对:输入若干条DNA链,找出其中配对的对数,其中A与T配对,C与G配对,并且是不重复配对。要求相配对的DNA链长度必须相同,并且对应位置上的字母俩俩配对



三、解题思路

主要用到string的知识点:对每条输入的DNA链给予两个属性,一个是length属性,一个是bool类型的used属性,length属性存放的是每条DNA链的长度,当DNA链匹配过了,其used属性为true,否则为true,每次匹配之前,先要判断两条链的长度是否相同,并且判断两条链是否已经使用过,如果两条链的长度相同,并且都没有使用过,则进一步判断两条链对应的位置上的字母是否俩俩相匹配,如果全部匹配,则说明这两条链是匹配的DNA链,则count的值+1,并且将匹配过的DNA链的used值设为true。

四、代码:

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

bool cmp(string a,string b){
 return a.size()>b.size();
}
bool match(string a,string b){ 
  for(int i=0;i<a.size();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(){
 int n;
 cin>>n;
 int count;
 int n1=0;
 while(n--){
  int num; 
  cin>>num;
  string str[num];
  bool use[num];
  int len[num];
  //输入DNA链
  for(int i=0;i<num;i++)
  {
   cin>>str[i];
  }
  //对输入的DNA链进行排序,有利于减枝
  sort(str,str+num,cmp);
  //对每条string保存它的长度以及它的访问情况
  for(int i=0;i<num;i++){
   len[i]=str[i].length();
   use[i]=false;
  }
  //获取DNA配对数
  count=0;
  for(int i=0;i<num;i++)
  {   
   if(!use[i])
   {
    for(int j=i+1;j<num;j++){
     if(!use[j])
     {
      if(match(str[i],str[j])&&len[i]==len[j]){
       count++;
       use[i]=true;
       use[j]=true;
       break;
      }        
     }  
    }
   }
   
  }
  cout<<count<<'\n';
   
 }
 return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

Sicily 1035. DNA matching题解

题目: 1035. DNA matching Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description DNA ...

sicily 1035

#include &lt;iostream&gt; #include &lt;string&gt; #include &lt;set&gt; using namespace std; string data[101]; int n; int main() { int T; cin &gt;&gt; T; while(T--) { cin &gt;&gt; n; set&lt;string&gt; s; set&lt;string&gt;::

sicily 1035.DNA Matching

#include #include #include #include using namespace std; int compare(const string &s1,const st...

poj 1035 spell checker

/* * poj 1035 spell checker * antsmall * 10-09-30 * http://poj.org/problem?id=1035 */ #include &lt;iostream&gt; #include &lt;algorithm&gt; #include &lt;vector&gt; using namespace std; struct Word{ char*data; int seq; Word(char*d, int s) { dat

db2删除数据库SQL1035N 数据库当前正在使用。 SQLSTATE=57019

  [db2inst1@db01 ~]$ db2 drop db cmsSQL1035N  数据库当前正在使用。  SQLSTATE=57019   首先,你需要查看链接该数据库有多少应用db2 list applications--然后在杀掉这些应用db2--db2 force applications all最后停止数据库db2 deactivate dat
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)