POJ 3080 Blue jeans
2015-11-27 12:31
357 查看
[align=center]Blue Jeans[/align]
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
Sample Output
题意就是求最多三个字符串的最长的公共子序列的问题。
考虑用后缀数组的思维求解,从第一个字符开始依次向后移动,取后缀数组, 然后拿到后两个字符串中进行比较,取两者匹配长度的最小值,最后取所有后缀数组中匹配长度的最大值作为结果。
一共就只有最多三个字符串,且每个字符串都只有60个字符,所以这么做是完全可以的。
代码如下:
/*************************************************************************
> File Name: Blue_Jeans.cpp
> Author: Zhanghaoran
> Mail: chilumanxi@xiyoulinux.org
> Created Time: Thu 26 Nov 2015 06:21:10 PM CST
************************************************************************/
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;
char str[10][150];
int nexti[150];
char cmp[150];
int len;
int T;
int n;
int ans;
void preKMP(){
int i, j;
i = 0;
j = nexti[0] = -1;
while(i < len){
if(j != -1 && cmp[i] != cmp[j]){
j = nexti[j];
}
else if(cmp[++ i] == cmp[++ j])
nexti[i] = nexti[j];
else
nexti[i] = j;
}
//for(int i = 0; i <= m; i ++){
// cout << kmpnext[i] << " ";
//}
//cout << endl;
}
void Kmp_Count(){
preKMP();
int i, j, res;
ans = 110;
for(int temp = 1; temp < n; temp ++){
i = 0;
j = 0;
res = 0;
while(i < 60 && j < len){
if(j == -1 || str[temp][i] == cmp[j]){
i ++;
j ++;
}
else
j = nexti[j];
if(j > res)
res = j;
}
if(res < ans)
ans = res;
}
}
int main(void){
scanf("%d", &T);
char ss[150];
while(T --){
scanf("%d", &n);
for(int i = 0; i < n; i ++)
scanf("%s", str[i]);
int res = 0;
for(int i = 0; i <= 57; i ++){
strcpy(cmp, str[0] + i);
len = 60 - i;
Kmp_Count();
if(res < ans){
res = ans;
strncpy(ss, str[0] + i, res);
ss[res] = '\0';
}
else if(ans == res){
char tt[150];
strncpy(tt, str[0] + i, res);
tt[res] = '\0';
if(strcmp(tt, ss) < 0)
strcpy(ss, tt);
}
}
if(res >= 3)
cout << ss << endl;
else
cout << "no significant commonalities" << endl;
}
return 0;
}
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 15090 | Accepted: 6696 |
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个字符,所以这么做是完全可以的。
代码如下:
/*************************************************************************
> File Name: Blue_Jeans.cpp
> Author: Zhanghaoran
> Mail: chilumanxi@xiyoulinux.org
> Created Time: Thu 26 Nov 2015 06:21:10 PM CST
************************************************************************/
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;
char str[10][150];
int nexti[150];
char cmp[150];
int len;
int T;
int n;
int ans;
void preKMP(){
int i, j;
i = 0;
j = nexti[0] = -1;
while(i < len){
if(j != -1 && cmp[i] != cmp[j]){
j = nexti[j];
}
else if(cmp[++ i] == cmp[++ j])
nexti[i] = nexti[j];
else
nexti[i] = j;
}
//for(int i = 0; i <= m; i ++){
// cout << kmpnext[i] << " ";
//}
//cout << endl;
}
void Kmp_Count(){
preKMP();
int i, j, res;
ans = 110;
for(int temp = 1; temp < n; temp ++){
i = 0;
j = 0;
res = 0;
while(i < 60 && j < len){
if(j == -1 || str[temp][i] == cmp[j]){
i ++;
j ++;
}
else
j = nexti[j];
if(j > res)
res = j;
}
if(res < ans)
ans = res;
}
}
int main(void){
scanf("%d", &T);
char ss[150];
while(T --){
scanf("%d", &n);
for(int i = 0; i < n; i ++)
scanf("%s", str[i]);
int res = 0;
for(int i = 0; i <= 57; i ++){
strcpy(cmp, str[0] + i);
len = 60 - i;
Kmp_Count();
if(res < ans){
res = ans;
strncpy(ss, str[0] + i, res);
ss[res] = '\0';
}
else if(ans == res){
char tt[150];
strncpy(tt, str[0] + i, res);
tt[res] = '\0';
if(strcmp(tt, ss) < 0)
strcpy(ss, tt);
}
}
if(res >= 3)
cout << ss << endl;
else
cout << "no significant commonalities" << endl;
}
return 0;
}
相关文章推荐
- 动易2006序列号破解算法公布
- Ruby实现的矩阵连乘算法
- C#插入法排序算法实例分析
- 超大数据量存储常用数据库分表分库算法总结
- C#数据结构与算法揭秘二
- C#冒泡法排序算法实例分析
- 算法练习之从String.indexOf的模拟实现开始
- C#算法之关于大牛生小牛的问题
- C#实现的算24点游戏算法实例分析
- c语言实现的带通配符匹配算法
- 浅析STL中的常用算法
- 算法之排列算法与组合算法详解
- C++实现一维向量旋转算法
- Ruby实现的合并排序算法
- C#折半插入排序算法实现方法
- 基于C++实现的各种内部排序算法汇总
- C++线性时间的排序算法分析
- C++实现汉诺塔算法经典实例
- PHP实现克鲁斯卡尔算法实例解析
- C#获取关键字附近文字算法实例