POJ 3080 Blue Jeans

POJ 3080 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 ©. 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

题意是找能与所有字符串匹配的最长长度且字典序最小的子串,因为数据量小可以直接暴力+KMP,求出每个串的子串与其他串逐个匹配即可,我一开始写了一发字符串的,改了半天还是T,(⊙﹏⊙),所以就算数据量小也不能浪,还是得用字符数组,AC代码如下:

#include<cstring>
#include<cstdio>
using namespace std;
typedef long long ll;
char s[15][100],p[100];
int n[100];
void get(char p[]){
    int j=-1,i=0;
    n[0]=-1;
    int len1=strlen(p);
    while(i<len1){
        if(j==-1 || p[i]==p[j]){
            i++,j++;
            if(p[i]!=p[j]) n[i]=j;
            else n[i]=n[j];
        }
        else j=n[j];
    }
}

int kmp(char s[],char p[]){
    int i=0,j=0;
    int len1=strlen(s),len2=strlen(p);
    while(i<len1 && j<len2){
        if(j==-1 || s[i]==p[j]){
            i++,j++;
        }
        else j=n[j];
    }
    if(j==len2) return 1;
    return 0;
}

int main(){
    int t,m;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&m);
        char ans[100]="";
        for(int i=0;i<m;i++)
                scanf("%s",s[i]);
        for(int i=0;i<m;i++){
            int len=strlen(s[i]);
            for(int j=0;j<len;j++){
                for(int k=1;k<=len-j;k++){
                    strncpy(p,s[i]+j,k);
                    p[k]='\0';//封尾防止溢出
                    get(p);
                    int flag=0;
                    for(int u=0;u<m;u++){
                        if(kmp(s[u],p) && k>=3) flag++;
                    }
                    if(flag==m){
                        if(strlen(p)==strlen(ans) && strcmp(p,ans)<0){
                            strcpy(ans,p);
                        }
                        else if(strlen(p)>strlen(ans))
                            strcpy(ans,p);
                    }
                }
            }
        }
        if(strcmp(ans,"")==0) puts("no significant commonalities");
        else printf("%s\n",ans);
    }
    return 0;
}
发布了425 篇原创文章 · 获赞 32 · 访问量 17万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 像素格子 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览