码迷,mamicode.com
首页 > 其他好文 > 详细

poj 3080 暴力的string用法

时间:2018-07-09 23:14:58      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:substr   dna   each   font   gen   cstring   positive   ios   tca   

Blue Jeans
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 20404   Accepted: 9043

Description

The Genographic Project is a research partnership between IBM and The National Geographic Society that is analyzing DNA from hundreds of thousands of contributors to map how the Earth was populated. 

As an IBM researcher, you have been tasked with writing a program that will find commonalities amongst given snippets of DNA that can be correlated with individual survey information to identify new genetic markers. 

A DNA base sequence is noted by listing the nitrogen bases in the order in which they are found in the molecule. There are four bases: adenine (A), thymine (T), guanine (G), and cytosine (C). A 6-base DNA sequence could be represented as TAGACC. 

Given a set of DNA base sequences, determine the longest series of bases that occurs in all of the sequences.

Input

Input to this problem will begin with a line containing a single integer n indicating the number of datasets. Each dataset consists of the following components:
  • A single positive integer m (2 <= m <= 10) indicating the number of base sequences in this dataset.
  • m lines each containing a single base sequence consisting of 60 bases.

Output

For each dataset in the input, output the longest base subsequence common to all of the given base sequences. If the longest common subsequence is less than three bases in length, display the string "no significant commonalities" instead. If multiple subsequences of the same longest length exist, output only the subsequence that comes first in alphabetical order.

Sample Input

3
2
GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
3
GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
GATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
GATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
3
CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
ACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT

Sample Output

no significant commonalities
AGATAC
CATCATCAT

本题利用C++的字符串暴力求解可以得到答案。
 1 #include<cstring>
 2 #include<iostream>
 3 #include<string>
 4 using namespace std;
 5 string a[12];
 6 
 7 int main()
 8 {
 9     int T;
10     cin >> T;
11     while(T--){
12         int n;
13         cin >> n;
14         for(int i=1;i<=n;i++){
15             a[i].clear();
16             cin >> a[i];
17         }
18         string ans="";
19         for(int i=3;i<=60;i++){
20             for(int j=0;j<=60-i;j++){
21                 string temp=a[1].substr(j,i);
22                 bool flag=true;
23                 for(int k=2;k<=n;k++){
24                     if(a[k].find(temp)==string::npos){
25                         flag=false;
26                         break;
27                     }
28                 }
29                 if(flag&&temp.size()>ans.size()) ans=temp;
30                 else if(flag&&temp.size()==ans.size()&&temp<ans) ans=temp;
31             }
32         }
33         if(ans=="") cout << "no significant commonalities" << endl;
34         else cout << ans << endl;
35     }
36     return 0;
37 }

substr(x,y)表示获取从x位开始长度为y的子串。

于是用获取得到的子串进行查找。即,find()。

poj 3080 暴力的string用法

标签:substr   dna   each   font   gen   cstring   positive   ios   tca   

原文地址:https://www.cnblogs.com/ZQUACM-875180305/p/9286447.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!