20-2-11-String-POJ3080

POJ3080

Blue Jeans

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 24558Accepted: 10824

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 ©. 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

South Central USA 2006

  • 题意: n组数据, 每组数据包含m个长度为60字符串, 求所有m个字符串的最长公共子序列, 如果这个最长公共子序列的长度小于3, 输出: no significant commonalities. 如果有多个长度相等的最长公共子序列, 输出字典序最小的那个.
  • 使用暴力+kmp算法解决此问题:
  • 对输入的第一个字符串暴力枚举其所有的子序列:
    • 枚举的方式为: 长度为1的所有子序列, 长度为2的所有子序列…长度为60的所有子序列.
    • 将该子序列作为kmp算法的的匹配字符串, 在所有的m个字符串中进行匹配, 匹配成功的同时按照题目要求的字典序最小更新ans
  • 有关kmp算法Click me
  • 时间复杂度分析
    • 暴力枚举第一个字符串的所有子串为O(length*length)
    • 一次kmp为O(length+length)的复杂度
    • 总的时间复杂度为一共进行了O(m*length*length)
#pragma warning(disable:4996)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
const int maxm = 15;
const int maxlength = 65;
char str[maxm][maxlength];

//返回的s, 子串的长度, 第一个字符串的位置
void getNextSubStr(char* s, int len, int pos) {
	for (int i = pos; i < pos + len; i++) {
		s[i - pos] = str[0][i];
	}
	s[pos + len] = '\0';
}

//求next数组
void getNext(char* s, int len, int* next) {
	int i = 0, j;
	j = next[0] = -1;
	while (i < len) {
		if (j == -1 || s[i] == s[j]) {
			next[++i] = ++j;
		}
		else {
			j = next[j];
		}
	}
}

int kmp(char* s, int lens, char* p, int lenp, int *next) {
	int i = 0, j = 0;
	while (i < lens && j < lenp) {
		if (j == -1 || s[i] == p[j]) {
			i++;
			j++;
		}
		else {
			j = next[j];
		}
	}
	if (j == lenp) {
		return i - j;
	}
	else {
		return -1;
	}
}

int main() {
	int n;
	cin >> n;
	while (n--) {
		int m;
		scanf("%d", &m);
		for (int i = 0; i < m; i++) {
			scanf("%s", str[i]);
		}
		int ans = 0;
		char ans_str[maxlength];
		ans_str[0] = '\0';
		//暴力枚举第一个字符串所有的子串, T(n+ n-1 + n-2 + n-3 ...+ 1 == n^2)
		//KMP匹配的模式字符串和其next数组
		char s[maxlength];
		int next[maxlength];
		for (int len = 1; len <= 60; len++) {
			//判断是否所有的该长度的字符串都匹配失败, 如果是, 那么找不到更长的可以匹配的字符串了, 结束判断
			bool f = true;
			for (int pos = 0; pos < 60 - len + 1; pos++) {
				getNextSubStr(s, len, pos);
				getNext(s, len, next);
				//这个子串是否在所有的字符串中匹配成功
				bool flag = true;
				for (int i = 1; i < m; i++) {
					if (kmp(str[i], 60, s, len, next) == -1) {
						flag = false;//有一个字符串没有匹配成功.
						break;
					}
				}
				if (flag == true) {
					f = false;
				}
				//如果这个字符串匹配成功了, 更新ans
				if (flag == true) {
					//ans必然小于等于len, 所以只有if和else
					if (ans == len) {
						//选择字典序较小的
						if (strcmp(ans_str, s) > 0) {
							strcpy(ans_str, s);
						}
					}
					//ans小于len
					else {
						ans = len;
						strcpy(ans_str, s);
					}
				}
			}
			if (f == true) {
				break;
			}
		}

		if (ans < 3) {
			cout << "no significant commonalities\n";
		}
		else {
			printf("%s\n", ans_str);
		}
	}
	return 0;
}
/*
3
2
GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
3
GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
GATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
GATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
3
CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
ACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值