POJ 3080 Blue Jeans (朴素算法)

题目链接:POJ 3080

Describe:
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

题目大意:

给定若干行长度为60的字符串,若存在公共子串,则输出长度最大的子串,若有多个,则输出字典序小的,如果没有则按要求输出一行英文。

解题思路:

暴力。枚举第一行字符串的所有子串,判断该子串是否为其它m-1个字符串的子串,如果是,更新ans答案串,并更新最大长度(如果需要的话),详细注解在注释给出。

AC代码:

 

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 using namespace std;
 5 string a[11],tmp,ans; // a用来存输入的串,tmp为中间变量,ans为答案串
 6 int n,m,flag,len; // n,m为题目输入变量,flag用来检查子串是否为其它字符串的子串,相当于标志
 7 // len用来存答案串最大长度
 8 int main()
 9 {
10     cin >> n;
11     while(n--)
12     {
13         ans = ""; // 每一个样例,先初始化答案串,便于以后判断
14         len = 0; // 初始化
15         cin >> m;
16         for(int i = 0; i < m; i++) cin >> a[i]; // 输入
17         // 双重循环,枚举a[0]所有子串,并保证子串长度不小于3(题目要求)
18         for(int j = 0; j <= 57; j++) // 注意j的范围
19         {
20             for(int i = 3; i + j <= 60; i++) // 注意此处的循环条件
21             {
22                 flag = 0;  // 每个子串判断前,初始化
23                 tmp = a[0].substr(j,i); // 得到子串,切割
24                 for(int i = 1; i < m; i++) // 和其他m-1个串做判断
25                 {
26                     if(strstr(a[i].c_str(),tmp.c_str()) == NULL) // 判断是否为子串
27                     {
28                         flag = 1;
29                         break;
30                     }
31                 }
32                 if(flag == 1) continue; // 若不是子串,直接跳过
33                 if(ans == "") // 如果第一次得到子串,先赋值
34                 {
35                     ans = tmp;
36                     len = tmp.size();
37                 } else { // 否则,先判断长度
38                     if(tmp.size() > len)
39                     {
40                         ans = tmp;
41                         len = tmp.size();
42                     } else if(tmp.size() == len) { // 若长度相同,则判断字典序
43                         if(strcmp(ans.c_str(),tmp.c_str()) > 0) {
44                             ans = tmp;
45                         }
46                     }
47                 }
48             }
49         }
50         // 按要求输出
51         if(ans == "") cout << "no significant commonalities" << endl;
52         else cout << ans << endl;
53     }
54     return 0;
55 }

 

posted @ 2020-08-05 10:53  不敢说的梦  阅读(122)  评论(0)    收藏  举报