zoukankan      html  css  js  c++  java
  • Life Forms[poj3294]题解

    Life Forms

    Description

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

    Input

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

    Output

    - 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
    

    Sample Output

    - bcdefg
      cdefgh
    
          ?
    

    思路

    • 后缀数组
    • 由于答案子串长度和答案个数具有单调性,可用二分答案法
    #include <iostream>
    #include <cstdlib>
    #include <cstdio>
    #include <cmath>
    #include <string>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    
    const int Max=501;
    const int MAX=1e5+1500;
    string s,ss[Max];
    int n,mx;
    int rnk[MAX],sa[MAX];
    int tmp[MAX],c[MAX];
    int h[MAX],sy[MAX];
    
    void lcp()
    {
    	h[0]=0;
    	for(int i=0,j=rnk[0],k=0; i<n-1; i++,k++)
    		while(k>=0&&s[i]!=s[sa[j-1]+k])
    			h[j]=k--,j=rnk[sa[j]+1];
    }
    
    void sarank()
    {
    	int na=256;
    	memset(c,0,na*sizeof(int));
    	n=s.size();
    	s[n]=1;n++;
    	for(int i=0; i<n; i++)	rnk[i]=(int)s[i],c[rnk[i]]++;
    	for(int i=1; i<na; i++)	c[i]=c[i]+c[i-1];
    	for(int i=0; i<n; i++)	c[rnk[i]]--,sa[c[rnk[i]]]=i;
    	int j;
    	for(int len=1; len<n; len=len<<1)
    	{
    		for(int i=0; i<n; i++)
    		{
    			j=sa[i]-len;
    			if(j<0)	j=j+n;
    			tmp[c[rnk[j]]++]=j;
    		}
    		sa[tmp[c[0]=0]]=j=0;
    		for(int i=1; i<n; i++)
    		{
    			if(rnk[tmp[i]]!=rnk[tmp[i-1]]||rnk[tmp[i]+len]!=rnk[tmp[i-1]+len])	c[++j]=i;
    			sa[tmp[i]]=j;
    		}
    		memcpy(rnk,sa,n*sizeof(int));
    		memcpy(sa,tmp,n*sizeof(int));
    		if(j>=n-1)	break;
    	}
    }
    
    int T;
    bool fl[Max];
    
    void print(int ans)
    {
    	int tot=0,i=0;
    	memset(fl,false,sizeof(fl));
    	while(i<n)
    	{
    		tot=0;
    		if(h[i]>=ans)
    		{
    			while(h[i]>=ans)
    			{
    				if(!fl[sy[sa[i]]]&&sy[sa[i]]!=0&&sy[sa[i]]!=sy[sa[i-1]]) tot++,fl[sy[sa[i]]]=true;
    				if(!fl[sy[sa[i-1]]]&&sy[sa[i-1]]!=0&&sy[sa[i]]!=sy[sa[i-1]]) tot++,fl[sy[sa[i-1]]]=true;
    				i++;
    			}
    			if(tot>T/2)
    			{
    				for(int j=sa[i-1]; j<sa[i-1]+ans; j++)
    					cout<<s[j];
    				cout<<endl;
    			}
    			memset(fl,false,sizeof(fl));
    		}
    		i++;
    	}
    }
    
    bool pd(int m)
    {
    	int tot=0,i=1;bool f;
    	memset(fl,false,sizeof(fl));
    	while(i<n)
    	{
    		tot=0;f=false;
    		if(h[i]>=m)
    		{
    			while(h[i]>=m)
    			{
    				if(h[i]==m)	f=true;
    				if(!fl[sy[sa[i]]]&&sy[sa[i]]!=0&&sy[sa[i]]!=sy[sa[i-1]]) tot++,fl[sy[sa[i]]]=true;
    				if(!fl[sy[sa[i-1]]]&&sy[sa[i-1]]!=0&&sy[sa[i]]!=sy[sa[i-1]]) tot++,fl[sy[sa[i-1]]]=true;
    				i++;
    				if(tot>T/2&&f)	return true;
    			}
    			memset(fl,false,sizeof(fl));
    		}
    		i++;
    	}
    	return false;
    }
    
    void solve()
    {
    	int l=1,r=mx,mid,ans=0;
    	while(l<=r)
    	{
    		mid=(l+r)>>1;
    		if(pd(mid))	ans=mid,l=mid+1;
    		else	r=mid-1;
    	}
    	if(ans)	print(ans);
    	else	printf("?\n");
    }
    
    int main()
    {
    	int sl;
            bool flag=true;
    	while(true)
    	{
            if(!flag) printf("\n");
            else flag = false;
    		scanf("%d",&T);
    		if(T==0)	break;
    		s="";mx=0;
    		for(int i=0; i<Max; i++)	c[i]=h[i]=sa[i]=sy[i]=tmp[i]=rnk[i]=0;
    		for(int i=1; i<=T; i++)
    		{
    			cin>>ss[i];sl=ss[i].size();
    			for(int j=s.size(); j<s.size()+sl; j++)	sy[j]=i;
    			s=s+ss[i]+char(i);
    			mx=max(mx,sl);
    		}
    		sarank(),lcp();
    		solve();
    	}
    	return 0;
    }
    
  • 相关阅读:
    hdu 1181 (搜索BFS,深搜DFS,并查集)
    [置顶] ZSTACK之OSAL_Nv非易失性存储解读上
    Android中利用Fragment显示为两屏
    WCF也可以做聊天程序
    Myeclipse 连接MSSqlServer
    Mysql和Oracle的卸载
    第 5堂作业
    hdu 3421 Max Sum II
    【求助】一个菜鸟java作业,帮忙看一下错在哪儿,题目是判断回文数
    netcat使用
  • 原文地址:https://www.cnblogs.com/vasairg/p/12228784.html
Copyright © 2011-2022 走看看