POJ 3080 Blue Jeans (思维+kmp)

题目链接https://vjudge.net/contest/350796#problem/C

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

翻译
t组数据,每一组数据先输入一个n,接着n个长度为60的字符串,求出这n个字符串的最长公共子串
公共子串长度小于3 ,输出no significant commonalities,否则输出这个公共字串
长度相同,输出字典序小的
分析
总共n个字符串,对于第一个字符串枚举所有可能的字串
在剩余的n-1个字符串中找是否存在枚举的字符串,若都存在,说明这个枚举的字串可以作为一个备用答案
根据长度和字典序的原则,更新答案

  1. 对于第一个字符串,如何枚举出所有的字串(体现思维的地方)

枚举以每一个字符为开头所有可能的字符串
例如: abcbdf
先以a开头:ab abc abcb abcbd abcbdf
再枚举b开头:bc bcb bcbd bcbdf
以此类推

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char a[11][70];
char temp[70],ans[70];
int net[70];
void kmp(char *s,int len)
{
    int i=0,k=-1;
    net[0]=-1;
    while(i<len)
    {
        if(k<0||s[i]==s[k])
            net[++i]=++k;
        else
            k=net[k];
    }
}
int solve(char *s,int len1,char *t,int len2)/*s为模式串,t为母串*/
{
    kmp(s,len1);
    int i=0,k=0;
    while(i<len2)
    {
        if(k<0||t[i]==s[k])
            k++,i++;
        else
            k=net[k];
        if(k>=len1)/*在主串中存在模式串*/
            return 1;
    }
    return 0;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,flag=0;
        scanf("%d",&n);
        for(int i=0; i<n; i++)
            scanf("%s",a[i]);
        for(int i=0; i<strlen(a[0]); i++)/*枚举起始位置*/
        {
            int tot=0,k;
            temp[tot++]=a[0][i];
            for(int j=i+1; j<strlen(a[0]); j++)/*枚举结束位置*/
            {
                temp[tot++]=a[0][j];
                temp[tot]='\0';/*tot的位置被赋成'\0',枚举下一个字串时tot的位置又会被重新赋成字符*/
                for(k=1; k<n; k++) /*剩余的m-1个字符串*/
                {
                    if(!solve(temp,tot,a[k],strlen(a[k])))
                        break;
                }
                if(k>=n)/*n-1个字符串中都存在枚举的字串*/
                {
                    if(flag==0||tot>strlen(ans))/*flag标记存在答案*/
                    {
                        flag=1;
                        strcpy(ans,temp);
                    }
                    else
                    {
                        if(tot==strlen(ans)&&strcmp(temp,ans)<0)/*长度相同就按照字典序的原则*/
                            strcpy(ans,temp);
                    }
                }
            }
        }
        if(flag==0||strlen(ans)<3)
        {
            printf("no significant commonalities\n");
            continue;
        }
        printf("%s\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zaiyang遇见

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值