程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> POJ 3080 Blue Jeans (求最長公共字符串)

POJ 3080 Blue Jeans (求最長公共字符串)

編輯:C++入門知識

Blue Jeans
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9973   Accepted: 4210

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
AACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTSample Output

no significant commonalities
AGATAC
CATCATCAT
Source

South Central USA 2006


題意:求最長公共字符串。如果最大長度小與3,則輸出no significant commonalities.反之輸出最長的這個串。
分析:枚舉公共字符串的長度,然後再第2到N串中找是否存在字符串1中相應長度的子串。
感想:對於new動態申請內存空間,還是2維數組的空間真的用不好。還有開始不知道有strstr()函數,一直很苦逼的寫找相應子串的函數,還運行不了。換了很多
            種方法,比如先找第一個和第二個串的子串,再找第三個和所有已找到子串的公共部分,依此類推,最後都被自己寫黃了。。。。
知識點整理:
1、


包含文件:string.h
函數名: strstr
函數原型:extern char *strstr(char *str1, char *str2);
功能:從字符串str1中查找是否有字符串str2,如果有,從str1中的str2位置起,返回str1中str2起始位置的指針,如果沒有,返回null。
返回值:返回該位置的指針,如找不到,返回空指針。
例子:
123 char

str[]="1234
 xyz";char*
 str1=strstr(str,"34");cout<<str1<<endl;

顯示:    34 xyz
2、
原型:extern int strcmp(const char *s1,const char * s2);
所在頭文件:string.h
功能:比較字符串s1和s2。
一般形式:strcmp(字符串1,字符串2)
說明:
當s1<s2時,返回值= -1
當s1==s2時,返回值= 0
當s1>s2時,返回值 = 1
注:c++ 中
當s1<s2時,返回值小於0
當s1==s2時,返回值等於0
當s1>s2時,返回值 大於0
即:兩個字符串自左向右逐個字符相比(按ASCII值大小相比較),直到出現不同的字符或遇'\0'為止。如:
"A"<"B" "a">"A" "computer">"compare"
特別注意:strcmp(const char *s1,const char * s2)這裡面只能比較字符串,不能比較數字等其他形式的參數。
代碼:
 

?#include<cstdio> 
#include<iostream> 
#include<cstring> 
using namespace std; 
const int len=60; 
 
int main() 
{ 
    int t,n,i,j,k; 
    scanf("%d",&t); 
    while(t--) 
    { 
        scanf("%d",&n); 
        char **DNA=new char*[n]; 
        for(i=0;i<n;i++) 
        { 
            DNA[i]=new char[len+1]; 
            scanf("%s",DNA[i]); 
        } 
        char obj[len+1]; 
        int length=1; 
        int Strlen=0; 
        for(j=0;;j++) 
        { 
            char dna[len+1]; 
            int pj=j; 
            if(pj+length>len) 
            { 
                length++; 
                j=-1; 
                if(length>len) 
                    break; 
                continue; 
            } 
            for(k=0;k<length;k++) 
                dna[k]=DNA[0][pj++]; 
            dna[k]='\0'; 
 
            bool flag=true; 
            for(i=1;i<n;i++) 
            { 
                if(!strstr(DNA[i],dna)) 
                { 
                    flag=false; 
                    break; 
                } 
            } 
            if(flag) 
            { 
                if(length>Strlen) 
                { 
                    Strlen=length; 
                    strcpy(obj,dna); 
                } 
                else if(length==Strlen) 
                { 
                    if(strcmp(obj,dna)>0) 
                        strcpy(obj,dna); 
                } 
            } 
        } 
        if(Strlen<3) 
            printf("no significant commonalities\n"); 
        else 
            printf("%s\n",obj); 
        delete DNA; 
    } 
    return 0; 
} 

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int len=60;

int main()
{
    int t,n,i,j,k;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        char **DNA=new char*[n];
        for(i=0;i<n;i++)
        {
            DNA[i]=new char[len+1];
            scanf("%s",DNA[i]);
        }
        char obj[len+1];
        int length=1;
        int Strlen=0;
        for(j=0;;j++)
        {
            char dna[len+1];
            int pj=j;
            if(pj+length>len)
            {
                length++;
                j=-1;
                if(length>len)
                    break;
                continue;
            }
            for(k=0;k<length;k++)
                dna[k]=DNA[0][pj++];
            dna[k]='\0';

            bool flag=true;
            for(i=1;i<n;i++)
            {
                if(!strstr(DNA[i],dna))
                {
                    flag=false;
                    break;
                }
            }
            if(flag)
            {
                if(length>Strlen)
                {
                    Strlen=length;
                    strcpy(obj,dna);
                }
                else if(length==Strlen)
                {
                    if(strcmp(obj,dna)>0)
                        strcpy(obj,dna);
                }
            }
        }
        if(Strlen<3)
            printf("no significant commonalities\n");
        else
            printf("%s\n",obj);
        delete DNA;
    }
    return 0;
}

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved