poj3080 Blue Jeans(暴枚+kmp)
2024-09-03 13:20:04
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
题意:有m个串(m<=10)求所有串的最长连续公共子串,如有多个解输出字典序最小的
题解:按长度倒着暴枚出第一串的所有子串,用kmp查找这个子串是不是在所有其他串中即可
但是
说好的只有"ACGT"四个字母呢?!
说好的只有长度为六十的串呢?!
人与人之间基本的信任呢?! 代码如下:
#include<queue>
#include<string>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define hi puts("hi!");
using namespace std; int m,n,cnt;
string ans,s[],s1[]; struct kmp
{
string ss,sss;
int ans,nxt[]; void getfill()
{
memset(nxt,,sizeof(nxt));
for(int j=;j<sss.size();j++)
{
int k=nxt[j];
while(k&&sss[j]!=sss[k])
{
k=nxt[k];
}
nxt[j+]=(sss[j]==sss[k])?k+:;
}
} void find()
{
ans=;
getfill();
int k=;
for(int j=;j<ss.size();j++)
{
while(k&&ss[j]!=sss[k])
{
k=nxt[k];
}
if(ss[j]==sss[k])
{
k++;
}
if(k==sss.size())
{
ans=;
}
}
} }k; int main()
{
// freopen("1.out","w",stdout);
scanf("%d",&m);
while(m--)
{
int flag=,flag1=;
cnt=;
scanf("%d\n",&n);
for(int i=;i<=n;i++)
{
cin>>s[i];
}
int len=s[].size();
for(int i=len;i>=;i--)
{
if(i<)
{
puts("no significant commonalities");
break;
}
for(int l=;l<=len-i;l++)
{
string sub=s[].substr(l,i);
flag=;
for(int w=;w<=n;w++)
{
k.ss=s[w];
k.sss=sub;
k.find();
flag=min(flag,k.ans);
}
if(flag)
{
s1[++cnt]=sub;
flag1=;
}
}
if(flag1)
{
string ans1=s1[];
for(int ha=;ha<=cnt;ha++)
{
if(ans1.compare(s1[ha])==)
{
ans1=s1[ha];
}
}
cout<<ans1<<endl;
break;
}
}
}
}
最新文章
- C 语言学习 第12次作业总结
- HDU 4927 Series 1(高精度+杨辉三角)
- Jquery 点击按钮将其背景图换成另一张,再次点击恢复默认图片
- 火狐浏览器Firefox上DownThemAll插件
- Maven学习总结(六)——Maven与Eclipse整合
- 第三十课:JSDeferred详解1
- uva-----11292 The Dragon of Loowater
- 转: android编译过程(流程图)
- (八)Struts2 文件上传和下载
- weblogic启动问题
- Codeforces243C-Colorado Potato Beetle(离散化+bfs)
- 解决jenkins slave 中文乱码 encoding=ANSI_X3.4-1968
- 8款非常不错的.Net反编译利器
- ImageUtil
- 如何使用AsyncTask
- 尚学堂java 参考答案 第八章
- Nodejs编译Native Code:使用C++构建工具npm
- 多线程学习-ListenableFuture使用介绍以及示例
- JQuery模仿淘宝天猫魔盒抢购页面倒计时效果
- HttpContext.Current并非无处不在