Blue Jeans
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 21740 | Accepted: 9650 |
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
Source
问题链接:POJ3080 Blue Jeans
问题描述:求多个字符串的最长公共子串(长度要至少为3,如果有多个长度相同的输出字典序最小的)
解题思路:以第一个字符串为基准,枚举它的各个子串(长度从大到小枚举),如果这个子串也在其他字符串中出现,那么就是一个答案,进行更新即可。C语言可使用strstr函数进行查找,C++可使用find函数进行查找
AC的C++程序:
#include<iostream>
#include<cstring>
using namespace std;
const int N=100;
char p[11][N],s[N],ans[N];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int m;
scanf("%d",&m);
for(int i=0;i<m;i++)
scanf("%s",p[i]);
//枚举第一个串的各个子串
int len=strlen(p[0]);
bool flag=false;//标记是否找到答案
for(int l=len;l>=3;l--)//子串长度 ,长度至少要为3
{
for(int i=0;i+l<=len;i++)//子串起始位置
{
strncpy(s,p[0]+i,l);//s表示p[0]的子串[i,i+1,i+2,...,i+l];
s[i+l]='\0';//注意strncpy函数不会在结束出添加结束符,因此要自己添加
//查看s在是否出现在其他字符串中
bool flag2=true;//标记s是否出现在其他字符串中
for(int j=1;j<m;j++)
if(!strstr(p[j],s))//查看字符串p[j]中是否包含s
{
flag2=false;
break;
}
if(flag2)//s是各个字符串的子串
{
//如果没有找到答案,或者ans的长度和s相同,但是s的字典序小,就更新
if(!flag||strcmp(ans,s)>0)
{
strcpy(ans,s);
flag=true;
}
}
}
if(flag)//如果找到答案,就输出
{
printf("%s\n",ans);
break;
}
}
if(!flag)//没找到符合条件的公共子串
printf("no significant commonalities\n");
}
return 0;
}
本文详细解析了POJ3080 BlueJeans问题,介绍了如何寻找多个字符串的最长公共子串,特别是长度至少为3且字典序最小的子串。通过以第一个字符串为基准,枚举所有可能的子串,并使用strstr或find函数检查子串在其他字符串中的存在性,从而高效地解决问题。
832

被折叠的 条评论
为什么被折叠?



