• UVA 11107(Life Forms-后缀数组+二分)


    Problem C: Life Forms

    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
    
    ?
    

    后缀数组第二题
    lrj书上的例题。
    无聊就去做了。
    可是又RE,估计做数据的把n改成10w了
    把n改成1千万,过了……【开玩笑的,真相在程序中】

    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    #include<functional>
    #include<iostream>
    #include<cmath>
    #include<cctype>
    using namespace std;
    #define For(i,n) for(int i=1;i<=n;i++)
    #define Fork(i,k,n) for(int i=k;i<=n;i++)
    #define Rep(i,n) for(int i=0;i<n;i++)
    #define ForD(i,n) for(int i=n;i;i--)
    #define RepD(i,n) for(int i=n;i>=0;i--)
    #define Forp(x) for(int p=pre[x];p;p=next[p])
    #define Lson (x<<1)
    #define Rson ((x<<1)+1)
    #define MEM(a) memset(a,0,sizeof(a));
    #define MEMI(a) memset(a,127,sizeof(a));
    #define MEMi(a) memset(a,128,sizeof(a));
    #define INF (2139062143)
    #define F (100000007)
    #define MAXN (10000000+10)
    #define MAXL (10001000+10)
    long long mul(long long a,long long b){return (a*b)%F;}
    long long add(long long a,long long b){return (a+b)%F;}
    long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
    typedef long long ll;
    int sa[MAXL],w[MAXN],wa[MAXL*2]={0},wb[MAXL*2]={0}; //真相:w需要开到MAXL 原因大家都懂的……
    bool cmp(int *a,int x,int y,int j){return a[x]==a[y]&&a[x+j]==a[y+j];}
    void suffix_array(char *s,int n,int m)
    {
    	int *x=wa,*y=wb;
    	For(i,m) w[i]=0;
    	For(i,n) w[x[i]=s[i]]++;
    	Fork(i,2,m) w[i]+=w[i-1];
    	ForD(i,n) sa[w[x[i]]--]=i;
    //	For(i,n) cout<<sa[i]<<' ';cout<<endl;
    	for(int j=1,p=0;p<n;j*=2,m=p)
    	{
    	//	cout<<j<<endl;
    		p=0;
    		Fork(i,n-j+1,n) y[++p]=i;
    		For(i,n) if (sa[i]>j) y[++p]=sa[i]-j;
    		
    	//	cout<<"y:";For(i,n) cout<<y[i]<<' ';cout<<endl;
    		
    		For(i,m) w[i]=0;
    		For(i,n) w[x[i]]++;
    		Fork(i,2,m) w[i]+=w[i-1];
    		ForD(i,n) sa[w[x[y[i]]]--]=y[i];
    		
    		p=y[sa[1]]=1;
    		Fork(i,2,n)
    			y[sa[i]]=(p+=(!cmp(x,sa[i],sa[i-1],j)));		
    	//	cout<<p<<endl;
    		int *t=x;x=y;y=t;	
    	//	For(i,n) cout<<sa[i]<<' ';cout<<endl;
    	}
    }
    int rank[MAXL],height[MAXL];
    void make_height(char *s,int n)
    {
    	height[1]=0;
    	For(i,n) rank[sa[i]]=i;
    	For(i,n)
    	{
    		if (rank[i]==1) continue;
    		height[rank[i]]=max(height[rank[i-1]]-1,0);
    		while (s[i+height[rank[i]]]==s[sa[rank[i]-1]+height[rank[i]]]) height[rank[i]]++;
    	}
    }
    int n,pre[MAXN],tai[MAXN];
    char s[MAXL];
    
    int b[MAXN]={0},belong[MAXL];
    int st[2][MAXL]={0},size[2]={0},now_col=1;
    int check(int *st,int &size,int p)
    {
    	memset(b,0,sizeof(b));now_col=1;
    	int tot=1;size=0;
    	b[belong[sa[1]]]=now_col;
    	Fork(i,2,pre[n+1]-1) 
    		if (height[i]>=p) {if (b[belong[sa[i]]]^now_col) b[belong[sa[i]]]=now_col,tot++;   }
    		else {if (tot>n/2) st[++size]=sa[i-1]; tot=1;now_col++;b[belong[sa[i]]]=now_col; }
    	return size;
    }
    char fillchar[MAXN];
    int main()
    {
    //	freopen("uva11107.in","r",stdin);
    	
    	int p=0;
    	For(i,'a'-1) fillchar[++p]=i;
    	for(int i='z'+1;p<=100;i++) fillchar[++p]=i;
    	
    	bool b=0;
    	while(scanf("%d",&n))
    	{
    		if (n) {if (b) puts("");}
    		else break;
    		pre[1]=1;
    		For(i,n)
    		{
    			scanf("%s",s+pre[i]);tai[i]=strlen(s+pre[i]);
    			s[pre[i]+tai[i]]=fillchar[i];pre[i+1]=pre[i]+tai[i]+1;
    			Fork(j,pre[i],pre[i]+tai[i]) belong[j]=i;
    		}
    		s[pre[n+1]]=0;
    		suffix_array(s,pre[n+1]-1,200);
    		make_height(s,pre[n+1]-1);
    	//	For(i,pre[n+1]-1) cout<<s[i]<<' ';cout<<endl;
    	//	For(i,pre[n+1]-1) cout<<rank[i]<<' ';cout<<endl;
    	//	For(i,pre[n+1]-1) cout<<height[i]<<' ';cout<<endl;
    		int l=1,r=pre[n+1]-1,ans=0,p=0;
    		while (l<=r)
    		{
    			int m=l+r>>1;
    			if (check(st[p],size[p],m)) ans=m,l=m+1,p^=1;
    			else r=m-1;
    		}
    		if (ans==0) puts("?");
    		else
    		{
    			p^=1;
    		//	cout<<ans<<endl;
    			For(i,size[p]) {Fork(k,st[p][i],st[p][i]+ans-1) cout<<s[k];cout<<endl;}
    		}
    		b=1;
    	}
    	
    	return 0;
    }
    






  • 相关阅读:
    springboot的build.gradle增加阿里仓库地址以及eclipse增加lombok
    mqttfx无法选择证书
    EMQX源码编译过程
    新环境chart包helmlint校验
    安装rebar3
    Ubuntu16.04下,erlang安装和rabbitmq安装步骤
    UTF-8,GBK,ANSI之间的关系和区别
    write命令
    bunzip2命令
    bzip2命令
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/3177759.html
Copyright © 2020-2023  润新知