poj---3080 Blue Jeans

Blue Jeans
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13551   Accepted: 6011

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

South Central USA 2006

题意是找出最长,字典序最小的子串,且长度大于等于3,否则输出

no significant commonalities
我的思路是暴力枚举出第一个串的长度大于2的所有子串,以下为代码。
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
char s[30][70];
char temp[3600][70];
int main()
{
	int n,t;
	scanf("%d",&t);
	while(t--)
	{
		memset(s,0,sizeof(s));
		memset(temp,0,sizeof(temp));
		scanf("%d",&n);
		for(int i=0;i<n;i++)
			scanf("%s",s[i]);
		int q=0;
		for(int l=3;l<=60;l++)//枚举出所有子串
		{
			for(int i=0;i<60;i++)
			{
				strncpy(temp[q++],s[0]+i,l);//函数为从第i个位置复制长度为l的串给temp
				if(i+l>=60)
				{
					break;
				}
				temp[q][l]='\0';
			}
		}
		char ans[70];
		memset(ans,0,sizeof(ans));
		int okok=0;
		for(int i=0;i<q;i++)//暴力枚举
		{
			int sum=0;
			for(int j=1;j<n;j++)
			{
				for(int w=0;s[j][w]!='\0';w++)
				{
					int ok=1;
					for(int k=0;temp[i][k]!='\0';k++)
					{
						if(s[j][w+k]!=temp[i][k])
						{
							ok=0;
							break;
						}
					}
					if(ok)
					{
						sum++;
						break;
					}
				}
			}
			if(sum==n-1)//子串和所有串都匹配
			{
				if(strlen(temp[i])>strlen(ans))//长度判断
				{
					okok=1;
					memset(ans,0,sizeof(ans));
					strcpy(ans,temp[i]);
				}
				else if (strlen(temp[i])==strlen(ans))
				{
					if(strcmp(temp[i],ans)==-1)//字典序判断
					{
					okok=1;
					memset(ans,0,sizeof(ans));
					strcpy(ans,temp[i]);
					}
				}
			}
		}
		if(okok)
			printf("%s\n",ans);
		else
			printf("no significant commonalities\n");
	}
	return 0;
}

你可能感兴趣的:(poj---3080 Blue Jeans)