频道栏目
首页 > 资讯 > C++ > 正文

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

13-08-09        来源:[db:作者]  
收藏   我要投稿

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;
}

 

 

相关TAG标签
上一篇:mysql千万级的count统计对比
下一篇:支付宝公布支付漏洞 陌陌、微信成主要攻击对象
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站