POJ 3080 Blue Jeans(暴力模拟+stl)

来源:互联网 发布:php获取get数据 编辑:程序博客网 时间:2024/06/07 16:02

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
32GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA3GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATAGATACTAGATACTAGATACTAGATACTAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAAGATACCAGATACCAGATACCAGATACCAAAGGAAAGGGAAAAGGGGAAAAAGGGGGAAAA3CATCATCATCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCACATCATCATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
Sample Output
no significant commonalitiesAGATACCATCATCAT

题解:

题意:

给你n个串,让你求他们的公共最长子串是什么,如果长度相同,按字典序输出,还有子串长度要大于等于3

思路:

没啥思路的。。。直接暴力就行了,做法多种多样,我这里用了stl模拟,枚举第一个串的所有子串,然后一个个找,真不知道这题为什么会出现在KMP专题

代码:

#include<algorithm>#include<iostream>#include<cstring>#include<stdio.h>#include<math.h>#include<string>#include<stdio.h>#include<queue>#include<stack>#include<map>#include<vector>#include<deque>using namespace std;#define lson k*2#define rson k*2+1#define M (t[k].l+t[k].r)/2#define INF 1008611111#define ll long long#define eps 1e-15int vis[3605];int main(){    string s,p[3605];    string str;    int test,n,i,j,k,cent,d,index,maxx;    scanf("%d",&test);    while(test--)    {        scanf("%d",&n);        cent=0;        cin>>s;        memset(vis,0,sizeof(vis));        for(i=0;i<58;i++)//枚举第一个串的长度大于3的所有子串        {            for(j=3;i+j<61;j++)            {                p[cent]=s.substr(i,j);//截取从i开始往后j个子串                cent++;            }        }        sort(p,p+cent);        cent=unique(p,p+cent)-p;//用不用去重其实无所谓,去重可能会快一点        maxx=0;        index=-1;        for(i=1;i<n;i++)        {            cin>>str;            for(j=0;j<cent;j++)            {                if(vis[j])                    continue;                if(str.find(p[j])==string::npos)//如果没找到                {                    vis[j]=1;                }            }        }        for(i=0;i<cent;i++)        {            if(!vis[i])            {                if(p[i].size()>maxx)                {                    maxx=p[i].size();                    index=i;                }            }        }        if(index!=-1)        {            cout<<p[index]<<endl;        }        else            printf("no significant commonalities\n");    }    return 0;}