POJ 3080 Blue Jeans (求最长公共字符串)

POJ 3080 Blue Jeans (求最长公共字符串)

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

题意:给定长度为60的m个字符串,找它的最长公共子串,如果长度相同,输出字典序小的,如果找到的公共子串小于3 ,就输出 一串不认识的字母,否则就输出找到的那一串同样不认识的字母。

注意到,这个题给的数据范围很小,m不大于10,长度不大于60,吐过暴力枚举的话,复杂度大概是60*60*10*(60+60),不会爆,所以果断枚举,

暴力找就行,枚举相同序列的长度以第一个DNA为模板向其他串中找。其中有个技巧性的地方就是strstr()函数的使用,strstr(a,b)函数为在a中找b,如果可以找到b那么会返回最初始找到b时的位置的地址,若找不到b则返回NULL。

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <algorithm>
 6 
 7 using namespace std;
 8 
 9 int main()
10 {
11     //freopen("sample.txt","r",stdin);
12     int n;
13     scanf("%d",&n);
14     while(n--)
15     {
16         char str[10][65];
17         char ans[65];
18         ans[0]=0;
19         int m;
20         scanf("%d",&m);
21         getchar();
22         for(int i=0;i<m;i++)
23         {
24             gets(str[i]);
25         }
26         for(int i=1;i<=strlen(str[0]);i++)
27         {
28             int af=0;
29             for(int j=0;j<=strlen(str[0])-i;j++)
30             {
31                 int flag=1;
32                 char ttm[65];
33                 strncpy(ttm,str[0]+j,i);
34                 ttm[i]=0;
35                 for(int g=1;g<m;g++)
36                 {
37                     if(!strstr(str[g],ttm))
38                     {
39                         flag=0;
40                         break;
41                     }
42                 }
43                 if(flag)
44                 {
45                     af=1;
46                     if(strlen(ttm)>strlen(ans))
47                         strcpy(ans,ttm);
48                     else if(strlen(ttm)==strlen(ans)&&strcmp(ttm,ans)<0)
49                         strcpy(ans,ttm);
50                 }
51             }
52             if(!af)
53                 break;
54         }
55         if(strlen(ans)<3)
56             printf("no significant commonalities
");
57         else
58             puts(ans);
59     }
60     return 0;
61 }
View Code
原文地址:https://www.cnblogs.com/jiamian/p/11191778.html