poj_3080Blue Jeans && poj_3450Corporate Identity(KMP)

Blue Jeans
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9476   Accepted: 3983

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
枚举子串,然后KMP。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#pragma warning(disable : 4996)
int n, m;
int Next[1000];
char str[15][65];
char ans[65];

void get_next(char *s, int m)
{
	char pat[65] = {0};
	strcpy(pat + 1, s);
	Next[1] = 0;
	int i, j = 0;
	for(i = 2; i <= m; i++)
	{
		while(j > 0 && pat[j+1] != pat[i])
		{
			j = Next[j];
		}
		if(pat[j+1] == pat[i])
		{
			j += 1;
		}
		Next[i] = j;
	}
}
bool kmp(char *A, char *B, int n, int m)
{
	char pat[65] = {0};
	char text[65] = {0};
	strcpy(text + 1, A);
	strcpy(pat + 1, B);
	int i, j = 0;
	for(i = 1; i <= n; i++)
	{
		while(j > 0 && pat[j+1] != text[i])
		{
			j = Next[j];
		}
		if(pat[j+1] == text[i])
		{
			j += 1;
		}
		if(j == m)
		{
			return true;
		}
	}
	return false;
}

bool check(char *s, int tot) //匹配串tmp
{
	int n, m;
	m = strlen(s);
	for (int i = 2; i <= tot; i++)
	{
		n = strlen(str[i] + 1);
		if(!kmp(str[i] + 1, s, n, m))
		{
			return false;
		}
	}
	return true;
}

int main()
{
	freopen("in.txt", "r", stdin);
	int t, n, length, len;
	char tmp[65] = {0};
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d", &n);
		for(int i = 1; i <= n; i++)
		{
			scanf("%s", str[i] + 1);
		}
		len = strlen(str[1] + 1);
		length = 0;
		for(int i = 1; i <= len; i++)
		{
			for(int j = 1; j <= len - i + 1; j++)
			{
				strncpy(tmp, str[1] + j, i);
				memset(Next, 0, sizeof(Next));
				get_next(tmp, i);
				if(check(tmp, n))
				{
					if(i >= length)
					{
						if(strcmp(tmp, ans) < 0 && i == length)
						{
							strcpy(ans, tmp);
							length = strlen(ans);
						}
						else
						{
							strcpy(ans, tmp);
							length = strlen(ans);
						}
					}
				}
				memset(tmp, 0, sizeof(tmp));
			}
		}
		if(length >= 3)
		{
			printf("%s\n", ans);
		}
		else
		{
			printf("no significant commonalities\n");
		}
	}
	return 0;
}

Corporate Identity
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 3998   Accepted: 1514

Description

Beside other services, ACM helps companies to clearly state their “corporate identity”, which includes company logo but also other signs, like trademarks. One of such companies is Internet Building Masters (IBM), which has recently asked ACM for a help with their new identity. IBM do not want to change their existing logos and trademarks completely, because their customers are used to the old ones. Therefore, ACM will only change existing trademarks instead of creating new ones.

After several other proposals, it was decided to take all existing trademarks and find the longest common sequence of letters that is contained in all of them. This sequence will be graphically emphasized to form a new logo. Then, the old trademarks may still be used while showing the new identity.

Your task is to find such a sequence.

Input

The input contains several tasks. Each task begins with a line containing a positive integer N, the number of trademarks (2 ≤ N ≤ 4000). The number is followed by N lines, each containing one trademark. Trademarks will be composed only from lowercase letters, the length of each trademark will be at least 1 and at most 200 characters.

After the last trademark, the next task begins. The last task is followed by a line containing zero.

Output

For each task, output a single line containing the longest string contained as a substring in all trademarks. If there are several strings of the same length, print the one that is lexicographically smallest. If there is no such non-empty string, output the words “IDENTITY LOST” instead.

Sample Input

3
aabbaabb
abbababb
bbbbbabb
2
xyz
abc
0

Sample Output

abb
IDENTITY LOST


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#pragma warning(disable : 4996)
char str[4005][205];
char tmp[205], ans[205];
int Next[4005];
void get_next(int m, char *B)
{
	char pat[205];
	strcpy(pat + 1, B);
	Next[1] = 0;
	int i, j = 0;
	for(i = 2; i <= m; i++)
	{
		while(j > 0 && pat[j+1] != pat[i])
		{
			j = Next[j];
		}
		if(pat[j+1] == pat[i])
		{
			j += 1;
		}
		Next[i] = j;
	}
}
bool kmp(char *A, char *B, int n, int m) //主串 模式串 主串长度 模式串长度
{
	char text[205], pat[205];
	strcpy(text + 1, A);
	strcpy(pat + 1, B);
	int i, j = 0;
	for(i = 1; i <= n; i++)
	{
		while(j > 0 && pat[j+1] != text[i])
		{
			j = Next[j];
		}
		if(pat[j+1] == text[i])
		{
			j += 1;
		}
		if(j == m)
		{
			return true;
		}
	}
	return false;
}

bool check(char *s, int t)
{
	for(int i = 2; i <= t; i++)
	{
		int n = strlen(str[i] + 1);
		int m = strlen(s);
		if(!kmp(str[i] + 1, s, n, m))
		{
			return false;
		}
	}
	return true;
}

int main()
{
	freopen("in.txt", "r", stdin);
	int t, length;
	while (scanf("%d", &t) != EOF)
	{
		if(t == 0)
		{
			break;
		}
		length = 0;
		for(int i = 1; i <= t; i++)
		{
			scanf("%s", str[i] + 1);
		}
		int len = strlen(str[1] + 1);
		for(int i = 1; i <= len; i++) // i 代表匹配串长度
		{
			for(int j = 1; j <= len - i + 1; j++)
			{
				strncpy(tmp, str[1] + j, i);
				memset(Next, 0, sizeof(Next));
				get_next(i, tmp);
				if(check(tmp, t))
				{
					if(i >= length)
					{
						if(strcmp(tmp, ans) < 0 && i == length)
						{
							strcpy(ans, tmp);
						}
						if(i > length)
						{
							strcpy(ans, tmp);
						}
						length = strlen(ans);
					}
				}
				memset(tmp, 0, sizeof(tmp));
			}
		}
		if(strlen(ans) >= 1)
		{
			printf("%s\n", ans);
			memset(ans, 0, sizeof(ans));
		}
		else
		{
			printf("IDENTITY LOST\n");
		}
	}

	return 0;
}




posted @ 2013-05-09 21:29  N3verL4nd  阅读(160)  评论(0编辑  收藏  举报