其他分享
首页 > 其他分享> > poj 3080 暴力

poj 3080 暴力

作者:互联网

Blue Jeans http://poj.org/problem?id=3080
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions:23922   Accepted: 10572

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:

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
//由于只查找是否出现过,算法复杂度差距不大,所以这里给出简单一些的写法
#include<stdio.h>//网上大佬代码 
#include<algorithm>
#include<iostream>
#include<string.h>

using namespace std;
const int maxn = 65;
char str[maxn];
string ansstr;
string str2[maxn];

int main()
{
    int n,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%s",str);
            str2[i]=str;
        }
        int ans=0;
        string tmp=str2[0];
        int tmplen=tmp.size();
        for(int i=0;i<tmplen;i++)
        {
            for(int j=1;i+j<=tmplen;j++)
            {
                int cnt=1;
                string ss=tmp.substr(i,j);  //返回一个string,
                                            //包含s中从pos开始的n个字符的拷贝(pos的默认值是0,n的默认值是s.size() - pos,
                                            //即不加参数会默认拷贝整个s)
                //ss为tmp从i开始长度为j的子串; 
                for(int k=1;k<n;k++)
                {
                    if(str2[k].find(ss)!=-1)//判断str2k串中有没有子串ss; 
                        cnt++;//记录有的数量,从而判断所有字符串是否都有子串ss; 
                }
                
                if(cnt==n)//判断是否所有字符串都有子串ss; 
                {
                    if(ans<j)
                    {
                        ans=j;
                        ansstr=ss;
                    }
                    else if(ans==j)
                    {
                        ansstr=min(ansstr,ss);//根据字典序比大小; 
                    }
                }
            }
        }
        if(ans<3) printf("no significant commonalities\n");
        else printf("%s\n",ansstr.c_str());//这是为了与c语言兼容,在c语言中没有string类型,
                                           //故必须通过string类对象的成员函数c_str()把string 
                                           //对象转换成c中的字符串样式。
                                           //ansstr为string类型,ansstr.c_str()转化为了char类型; 
    }
}
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;

const int maxn=65;
char str[maxn];
string ansstr;
string str2[maxn];

int main(){
    int t,n;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%s",str);
            str2[i]=str;
        }
        int cnt=0;
        for(int i=0;i<60;i++){
            for(int j=1;j+i<=60;j++){
                int ans=1;
                string ss=str2[0].substr(i,j);
                
                for(int k=1;k<n;k++){
                    if(str2[k].find(ss)!=-1){
                        ans++;
                    }
                }
                if(ans==n){
                //    printf("%s\n",ss.c_str());
                    if(cnt<j){
                        cnt=j;
                        ansstr=ss;
                        
                     }else if(cnt==j){
                        ansstr=min(ansstr,ss);
                     }else{
                        continue;
                    }
                    
                 }else{
                      continue;
                 }
            }
        }
        if(cnt<3){
            printf("no significant commonalities\n");
        }else{
            printf("%s\n",ansstr.c_str());
        }
    }
    return 0;
} 

 

标签:bases,暴力,int,base,poj,maxn,include,3080,string
来源: https://www.cnblogs.com/qqshiacm/p/11587372.html