zoukankan      html  css  js  c++  java
  • [POJ3080] Blue Jeans

    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

    题解

    这题可以用(KMP)或后缀数组,虽然后缀数组运算比较快,但是后缀数组常用的倍增求(sa[],rk[])的时间复杂度为(O(n*log_2n)),所以这题我选择用(O(n))(KMP)

    我们枚举第(1)个串的后缀与其他的串分别比较,用(KMP)求出最长匹配长度,统计最长匹配长度的最小值,将最小值设为(Res_i),显而易见,最后的最大长度为(max{Res_i|i≤n,i∈N^*})

    • 贡献一次(WA):注意字典序
    #include<iostream>
    #include<cstdio>
    using namespace std;
    
    const int L=120;
    int n,lp,ls,nt[L];
    char s[12][L],p[L],out[L];
    
    void Make_nt()
    {
    	int i=0,j=-1;
    	nt[i]=-1;
    	while(i<lp)
    	{
    		if(j==-1||p[i]==p[j])
    			++i,++j,nt[i]=j;
    		else j=nt[j];
    	}
    }
    
    int KMP(char s[])
    {
    	int i=0,j=0,Res=0;
    	while(i<ls&&j<lp)
    	{
    		if(j==-1||s[i]==p[j]) ++i,++j;
    		else j=nt[j];
    		Res=max(Res,j);
    	}
    	return Res;
    }
    
    void Check(int i,int Res)
    {
    	for(int j=0;j<Res;++j)
    	{
    		if(out[j]<s[1][i+j]) return;
    		if(out[j]>s[1][i+j]) break;
    	}
    	for(int j=0;j<Res;++j) out[j]=s[1][i+j];
    }
    
    void Solve()
    {
    	scanf("%d",&n);
    	for(int i=1;i<=n;++i) scanf("%s",s[i]);
    	lp=ls;
    	int Ans=0,Res;
    	for(int i=0;i<ls-3;++i,--lp)
    	{
    		for(int j=i;j<ls;++j) p[j-i]=s[1][j];
    		Make_nt();
    		Res=lp;
    		for(int j=2;j<=n;++j)
    		{
    			Res=min(Res,KMP(s[j]));
    			if(Res<3) break;//优化
    		}
    		if(Res>Ans)
    		{
    			Ans=Res;
    			for(int j=0;j<Res;++j) out[j]=s[1][i+j];
    		}
    		if(Res==Ans) Check(i,Ans);
    	}
    	if(Ans<3) puts("no significant commonalities");
    	else
    	{
    		for(int i=0;i<Ans;++i) printf("%c",out[i]);
    		putchar('
    ');
    	}
    }
    
    int main()
    {
    	int T; ls=60;
    	for(scanf("%d",&T);T;--T) Solve();
    	return 0;
    }
    
  • 相关阅读:
    程序猿身边有个漂亮女程序媛~~~那是种什么样的体验?
    前端程序猿分九段,一段又一段,你是哪一段?
    10个屌炸天的设计网址导航带你嗨翻科技设计界 #精选前端开发设计素材
    人工智能一定高大上?盘点那些让人哭笑不得的人工智障 #精选黑科技人工智能
    javascript奇技淫巧之位运算符
    曾经的超级明星类库jQuery未来也许不再会被前端程序猿追捧了!
    谷歌为什么把上十亿行代码都放在一个仓库里
    全功能响应式模板:黑暗元素
    程序员的福音,AI可以自动修复bug了!
    机器学习原来如此有趣:如何故意欺骗神经网络
  • 原文地址:https://www.cnblogs.com/hihocoder/p/12214329.html
Copyright © 2011-2022 走看看