UVA11107 Life Froms 后缀数组

You may have wondered why most extraterrestrial life forms resemble humans, differing by superficial traits such as height, colour, wrinkles, ears, eyebrows and the like. A few bear no human resemblance; these typically have geometric or amorphous shapes like cubes, oil slicks or clouds of dust.

The answer is given in the 146th episode of Star Trek - The Next Generation, titled The Chase. It turns out that in the vast majority of the quadrant's life forms ended up with a large fragment of common DNA.

Given the DNA sequences of several life forms represented as strings of letters, you are to find the longest substring that is shared by more than half of them.

Standard input contains several test cases. Each test case begins with 1 ≤ n ≤ 100, the number of life forms. n lines follow; each contains a string of lower case letters representing the DNA sequence of a life form. Each DNA sequence contains at least one and not more than 1000 letters. A line containing 0 follows the last test case.

For each test case, output the longest string or strings shared by more than half of the life forms. If there are many, output all of them in alphabetical order. If there is no solution with at least one letter, output "?". Leave an empty line between test cases.

Sample Input

3
abcdefg
bcdefgh
cdefghi
3
xxx
yyy
zzz
0

Output for Sample Input

bcdefg
cdefgh

?

转载请注明出处:http://blog.csdn.net/scut_pein/article/details/19786707
题意:求出一个长度最大的字符串,在超过一半的DNA序列中连续出现,如果有多解,按照字典序从小到大依次输出

思路:将所给字符串中间用一个没出现过的字符隔开,最后再添加一个最小的字符。然后建立后缀数组。

            用一个belong数组来表示该后缀来源于哪个字符串。via数组标记该字符串是否已经出现

            先二分长度,然后再输出。

#include 
#include 
#include 
#include 
#include 
using namespace std;
#define maxn 140080
#define inf 0x3f3f3f3f
int key[maxn],belong[140080];
int str[maxn];
bool vis[120];
int sa[maxn],t[maxn],t2[maxn],c[maxn];
int height[maxn],Rank[maxn];

/*
用SA模板注意在最后添加一个比所有字符都小的字符。
key[n] = 0;
build_sa(key,n+1,m);
getHeight(key,n+1);
显然sa[0] 就是最后那个位置。。。
height[i] 表示 sa[i] 和 sa[i-1] 的最长公共前缀。。
*/

void build_sa(int * s,int n,int m)
{
	int i,*x = t,*y = t2;
	for(i = 0;i < m;i++)	c[i] = 0;
	for(i = 0;i < n;i++)	c[ x[i] = s[i] ]++;
	for(i = 1;i < m;i++)	c[i] += c[i-1];
	for(i = n-1;i >= 0;i--)	sa[--c[x[i]]] = i;
	for(int k = 1;k <= n;k <<= 1)
	{
		int p = 0;
		for(i = n - k;i < n;i++)	y[p++] = i;
		for(i = 0;i < n;i++)	if(sa[i] >= k)	y[p++] = sa[i] - k;
		for(i = 0;i < m;i++)	c[i] = 0;
		for(i = 0;i < n;i++)	c[ x[y[i]] ]++;
		for(i = 0;i < m;i++)	c[i] += c[i-1];
		for(i = n-1;i >= 0;i--)	sa[--c[x[y[i]]]] = y[i];
		//根据sa和y数组计算新的数y组
		swap(x,y);
		p = 1;	x[sa[0]] = 0;
		for(i = 1;i < n;i++)
			x[sa[i]] = y[sa[i-1]] == y[sa[i]] && y[sa[i-1] + k] == y[sa[i] + k] ? p-1:p++;
		if(p >= n)	break;
		m = p;
	}
}

void getHeight(int * s,int n)
{
	int i,j,k = 0;
	for(i = 0;i < n;i++)	Rank[sa[i]] = i;
	for(i = 0;i < n;i++)	
	{
		if(k) k--;
		int j = sa[Rank[i]-1];
		while(s[i+k] == s[j+k])		k++;
		height[Rank[i]] = k;
	}
}

bool Judge(int n,int len,int num)
{
	int cnt = 0;
	memset(vis,0,sizeof(vis));
	vis[0] = 1;
	if(!vis[belong[sa[0]]])
	{
		cnt++;
		vis[belong[sa[0]]] = 1;
	}
	for(int i = 1;i < n;i++)
	{
		if(height[i] < len)
		{
			cnt = 0;
			memset(vis,0,sizeof(vis));
			vis[0] =1;
			if(!vis[belong[sa[i]]])
			{
				vis[belong[sa[i]]] = 1;
				cnt++;
			}
		}
		else 
		{
			if(!vis[belong[sa[i]]])
			{
				cnt++;
				vis[belong[sa[i]]] = 1;
			}
		}
		if(cnt >= num)	return 1;
	}
	return 0;
}

void Print(int n,int len,int num)
{
	int cnt = 0;
	memset(vis,0,sizeof(vis));
	vis[0] = 1;
	if(!vis[belong[sa[0]]])
	{
		cnt++;
	}
	vis[belong[sa[0]]] = 1;
	
	for(int i = 1;i < n;i++)
	{
		if(height[i] < len)
		{
			if(cnt >= num)
			{
				for(int j = sa[i-1];j < sa[i-1]+len;j++)
					printf("%c",str[j]-18);
				printf("\n");
			}
			cnt = 0;
			memset(vis,0,sizeof(vis));
			vis[0] = 1;
			if(!vis[belong[sa[i]]])
			{
				cnt++;
				vis[belong[sa[i]]] = 1;
			}
		}
		if(!vis[belong[sa[i]]])
		{
			cnt++;
			vis[belong[sa[i]]] = 1;
		}
	}
	if(cnt >= num)
	{
		for(int i = sa[n-1];i < sa[n-1] + len;i++)
			printf("%c",str[i]-18);
		printf("\n");
	}
}

inline int max(int a,int b)
{
	return a>b?a:b;
}

char s[1008];
int main()
{
	//freopen("in.txt","r",stdin);
	int n;
	bool flag = true;
	while(scanf("%d",&n)!=EOF && n)
	{
		if(!flag)	printf("\n");
		else flag = false;
		memset(belong,0,sizeof(belong));
		int pos = 0,cat = 1;
		int l = 0,r = 0;
		for(int i = 1;i <= n;i++)
		{
			scanf("%s",s);
			int len = strlen(s);
			r = max(r,len);
			for(int j = 0;j < len;j++)
			{
				str[pos+j] = (int)(s[j])+18;
				belong[pos+j] = i;
			}
			str[pos+len] = cat++;
			pos = pos + len + 1;
		}
		str[pos] = 0;
		build_sa(str,pos+1,150);
		getHeight(str,pos+1);
		int maxlen = 0;
		while(l <= r)
		{
			int mid = (l+r) >> 1;
			if(Judge(pos+1,mid,n/2+1))
			{
				maxlen = mid;
				l = mid + 1;
			}
			else r = mid - 1;
		}
		if(maxlen == 0)
			printf("?\n");
		else Print(pos+1,maxlen,n/2+1);
	}
	return 0;
}

你可能感兴趣的:(字符串_后缀数组)