zoukankan      html  css  js  c++  java
  • poj 3080 -- Blue Jeans

    Blue Jeans
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 11789   Accepted: 5095

    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
    AACATCATCATTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT

    Sample Output

    no significant commonalities
    AGATAC
    CATCATCAT
    

    思路:可以枚举第一个字符串,去匹配剩余n-1个。kmp解决。

      1 /*======================================================================
      2  *           Author :   kevin
      3  *         Filename :   BlueJeans.cpp
      4  *       Creat time :   2014-07-20 09:12
      5  *      Description :
      6 ========================================================================*/
      7 #include <iostream>
      8 #include <algorithm>
      9 #include <cstdio>
     10 #include <cstring>
     11 #include <queue>
     12 #include <cmath>
     13 #define clr(a,b) memset(a,b,sizeof(a))
     14 #define M 100
     15 using namespace std;
     16 
     17 char str[15][M];
     18 struct Node{
     19     char str[M];
     20 }node[15];
     21 int n;
     22 
     23 void GetNext(char str[],int next[])
     24 {
     25     int j = 0, k = -1;
     26     next[0] = -1;
     27     int len = strlen(str);
     28     while(j < len){
     29         if(k == -1 || str[j] == str[k]){
     30             j++;
     31             k++;
     32             next[j] = k;
     33         }
     34         else k = next[k];
     35     }
     36 }
     37 
     38 bool KMP(char str[],char t[])
     39 {
     40     int len1 = strlen(t);
     41     int len2 = strlen(str);
     42     int next[M],i = 0,j = 0;
     43     clr(next,0);
     44     GetNext(str,next);
     45     while(i < len1 && j < len2){
     46         if(j == -1 || str[j] == t[i]){
     47             i++;
     48             j++;
     49         }
     50         else j = next[j];
     51     }
     52     if(j >= len2) return true;
     53     return false;
     54 }
     55 
     56 bool cmp(struct Node a,struct Node b)
     57 {
     58     return strcmp(a.str,b.str)<0;
     59 }
     60 int main(int argc,char *argv[])
     61 {
     62     int cas;
     63     scanf("%d",&cas);
     64     while(cas--){
     65         scanf("%d",&n);
     66         for(int i = 0; i < n; i++){
     67             getchar();
     68             scanf("%s",str[i]);
     69         }
     70         char s[M];
     71         int l,flag = 0;
     72         int len = strlen(str[0]);
     73         int num = 0;
     74         for(int i = len; i >= 1; i--){
     75             for(int j = 0; j <= len-i; j++){
     76                 int cnt = 0;
     77                 for(int k = j; cnt < i; k++){
     78                     s[cnt++] = str[0][k];
     79                 }
     80                 s[cnt] = '';
     81                 for(l = 1; l < n; l++){
     82                     if(!KMP(s,str[l])){
     83                         break;
     84                     }
     85                 }
     86                 if(l == n){
     87                     flag = 1;
     88                     strcpy(node[num++].str,s);
     89                 }
     90             }
     91             if(flag) break;
     92         }
     93         int len1 = strlen(s);
     94         if(len1 < 3){
     95             printf("no significant commonalities
    ");
     96         }
     97         else{
     98             sort(node,node+num,cmp);
     99             printf("%s
    ",node[0].str);
    100         }
    101     }
    102     return 0;
    103 }
    View Code
  • 相关阅读:
    重写Nacos服务发现逻辑动态修改远程服务IP地址
    手撸一个SpringBoot配置中心实现配置动态刷新
    使用CompletableFuture实现多个异步任务并行完成后合并结果
    SpringBoot实现Flyway的Callback回调钩子
    Java实现相似结构表算法
    使用Druid解析SQL实现血缘关系计算
    记一次解决RestTemplate和HttpClient请求结果乱码的问题
    double转json格式化添加自定义注解
    了解23种设计模式
    BigDecimal四舍五入
  • 原文地址:https://www.cnblogs.com/ubuntu-kevin/p/3856022.html
Copyright © 2011-2022 走看看