码迷,mamicode.com
首页 > 其他好文 > 详细

POJ - 3080 Blue Jeans

时间:2014-09-15 16:02:04      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:des   blog   io   os   ar   for   2014   div   art   

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
题意:求n个长为60的串的最长公共子串
思路:因为是n个串的公共字串,所以一定是第一串的公共字串,枚举第一个串的公共字串,再用KMP匹配
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 70;

int m, next[maxn];
char DNA[11][maxn];

void getNext(char *str) {
	int len = strlen(str);
	next[0] = next[1] = 0;
	for (int i = 1; i < len; i++) {
		int j = next[i];
		while (j && str[i] != str[j])
			j = next[j];
		next[i+1] = str[i] == str[j] ? j+1 : 0;
	}
}

int kmp(char *str, char *tmp) {
	getNext(str);
	int len = strlen(str);
	int j = 0;
	for (int i = 0; i < 60; i++) {
		while (j && str[j] != tmp[i])
			j = next[j];
		if (str[j] == tmp[i])
			j++;
		if (j == len) 
			return 1;
	}
	return 0;
}

int main() {
	int t;
	char ans[maxn], tmp[maxn];
	scanf("%d", &t);
	while (t--) {
		scanf("%d", &m);
		for (int i = 0; i < m; i++)
			scanf("%s", DNA[i]);
		int maxLen = 0;
		for (int i = 0; i < 60; i++) {
			memset(tmp, '\0', sizeof(tmp));
			int cnt = 0;
			for (int j = i; j < 60; j++) {
				tmp[cnt++] = DNA[0][j];
				int vis = 1;
				for (int k = 1; k < m; k++)
					if (!kmp(tmp, DNA[k])) {
						vis = 0;
						break;
					}
				if (vis) {
					if (maxLen < cnt) {
						maxLen = cnt;
						memcpy(ans, tmp, sizeof(tmp));
					}
					else if(maxLen == cnt && strcmp(ans, tmp) > 0)
						memcpy(ans, tmp, sizeof(tmp));
				}
			}
		}

		if (maxLen < 3)
			printf("no significant commonalities\n");
		else printf("%s\n", ans);
	}
	return 0;
}


 

POJ - 3080 Blue Jeans

标签:des   blog   io   os   ar   for   2014   div   art   

原文地址:http://blog.csdn.net/u011345136/article/details/39292489

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!