zoukankan      html  css  js  c++  java
  • Blue Jeans[poj3080]题解

    题目

    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 1

    - 3
      2
      GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
      AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
      3
      GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATA
      GATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
      GATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA
      3
      CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
      ACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
      AACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
    

    Sample Output 1

    - no significant commonalities
      AGATAC
      CATCATCAT
    

    思路

    • 暴力循环子串长度
    • \(KMP\)判断每个串中是否有该子串
    • 详细见\(code\)
    #include <iostream>
    #include <cstdlib>
    #include <cstdio>
    #include <cmath>
    #include <string>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    
    const int Max=1001;
    int nx[Max];
    string S[Max],P;
    int T,M,N[Max];
    
    void makenx(int M)
    {
    	memset(nx,0,sizeof(nx));
    	int i=0,j=-1;
    	nx[i]=j;
    	while(i<M)
    	{
    		if(j==-1||P[i]==P[j])	i++,j++,nx[i]=j;
    		else    j=nx[j];
    	}
    }
    
    int Kmp(int k,int M)
    {
    	int i=0,j=0;
    	while((i<N[k])&&(j<M))
    	{
    		if(j==-1||S[k][i]==P[j])   i++,j++;
    		else    j=nx[j];
    	}
    	if(j>=M)    return true;
    	else    return false;
    }
    
    int main()
    {
    	int n,m;bool fl;
        scanf("%d",&T);
        string ans;int lans;
        while(T--)
        {
            scanf("%d",&n);
            for(int i=1; i<=n; i++)		cin>>S[i],N[i]=S[i].size();
    		ans="";lans=0;
    		m=S[1].size();
    		for(int i=0; i<m; i++)//循环子串起点 
    		{
    			for(int j=3; j<=m-i; j++)//循环子串长度 
    			{
    				fl=true;
    				P=S[1].substr(i,j);
    				M=j;makenx(M);
    				for(int k=2; k<=n; k++)
    					if(!Kmp(k,M))//Kmp判断 
    						{fl=false;break;}
    				if(fl)
    				{
    					if(M>lans)	ans=P,lans=M;
    					else if(M==lans&&ans>P)	ans=P,lans=M;//字典序 
    				}
    			}
    		}
    		if(ans=="")	cout<<"no significant commonalities"<<endl;
    		else	cout<<ans<<endl;
        }
        return 0;
    }
    
  • 相关阅读:
    HDU 5883 F
    关于codeblock 为什么不能调试
    Codeforces Round #378 (Div. 2) D. Kostya the Sculptor 分组 + 贪心
    51NOD 区间的价值 V2
    NYOJ 42 一笔画问题
    如何对二维字符数组进行排序
    hihoCoder 1383 : The Book List 北京网络赛
    利用IDA学习一个简单的安卓脱壳
    iOS APP可执行文件的组成
    Mac10.11 搭建php开发环境
  • 原文地址:https://www.cnblogs.com/vasairg/p/12214482.html
Copyright © 2011-2022 走看看