Blue Jeans
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 20163   Accepted: 8948

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

Source

题意:输入t组数据,每组有n个60个字符大小的字符串,求他们的最长公共子序列,在长度相同的情况下,输出字典序最小的那个,如果子序列的长度小于3,输出

no significant commonalities

思路:随便找一个字符串,从第1号位置一直枚举到第57号位置,然后这每一种情况都与其他n-1个字符串匹配,看每一种情况与这n-1个字符串最长可以匹配多长,在每一种情况下取与这m-1个字符串匹配最小的长度(保证它可以和这n-1个字符都匹配的上),在57种情况中取最大的长度(保证是这n个字符的最长公共子序列)。
代码:

#include<stdio.h>
#include<string.h>
char s[12][62],p[62];
char ans[62];
int next[62];
int N;
int getnext(int n)
{
  next[0]=-1;
  int i,j=1,k=-1;
  while(j<n)
  {
    while(k>-1&&p[j]!=p[k+1])
    {
      k=next[k];
    }
    if(p[j]==p[k+1])
    k++;
    next[j]=k;
    j++;
  }
  return 0;
}
int kmp(int n)
{
  getnext(n);
  int i,j,k,sum,mx=0;
  int max=100;
  for(i=1;i<N;i++)//与剩下n-1个字符匹配
  {
    j=0,k=0,mx=0;
    while(j<60&&k<n)
    {
      if(p[k]==s[i][j])//匹配时
      {
        k++;
        j++;
      }
      else
      {
        if(k==0)//回到了模式串的开头
        j++;
        else
        k=next[k-1]+1;                

      }
      if(mx<k)
      mx=k;
    }
    if(max>mx)
    max=mx;
  }
  return max;
}
int main()
{
  int t;
  scanf("%d",&t);
  int i,j;
  int len;
  while(t--)
  {
    len=0;
    scanf("%d",&N);
    for(i=0;i<N;i++)
    {
      scanf("%s",s[i]);
      //printf("%s\n",s[i]);
    }
    for(i=0;i<58;i++)
    {
      strcpy(p,s[0]+i);
      p[60-i]='\0';
      int mx=kmp(60-i);
      if(len<mx)
      {
        strncpy(ans,s[0]+i,mx);
        ans[mx]='\0';
        len=mx;
      }
      else if(len==mx)
      {
        p[mx]='\0';
        if(strcmp(p,ans)<0)
        {
          strcpy(ans,p);
          ans[mx]='\0';
        }
      }
    }
    if(len>=3)
    printf("%s\n",ans);
    else
    printf("no significant commonalities\n");
  }
  return 0;
}

poj3080(kmp+枚举)的更多相关文章

  1. poj-3080(kmp+暴力枚举)

    题意:给你多个字符串,问你这几个字符串的最长公共子串是哪个,如果有多个,输出字典序最大的那个,如果最长的公共子串长度小于3,输出一个奇怪的东西: 解题思路:首先看数据,数据不大,开始简单快乐的暴力之路 ...

  2. hdu-1238(kmp+枚举)

    题意:给你n个字符串,问你这里面最长的公共子串的长度是多少,一个公共子串的反串也算,比如样例二: 解题思路:随便找一个字符,枚举它的子串然后跑kmp就行了,很多人的博客都是用string类里面的函数来 ...

  3. HDU 4763 Theme Section(KMP+枚举公共前后缀)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4763 题目大意: 给你一个字符串s,存在一个子串E同时出现在前缀.中间.后缀,即EAEBE这种模式,A ...

  4. HDU3613 Best Reward —— Manacher算法 / 扩展KMP + 枚举

    题目链接:https://vjudge.net/problem/HDU-3613 Best Reward Time Limit: 2000/1000 MS (Java/Others)    Memor ...

  5. POJ-3080(KMP+多个字符串的最长公共子串)

    Blue Jeans HDOJ-3080 本题使用的是KMP算法加暴力解决 首先枚举第一个字符串的所有子串,复杂度为O(60*60),随后再将每个子串和所有剩下的m-1个字符串比较,看是否存在这个子串 ...

  6. POJ 2185 - Milking Grid (二维KMP)

    题意:给出一个字符矩形,问找到一个最小的字符矩形,令它无限复制之后包含原来的矩形. 此题用KMP+枚举来做. 一维的字符串匹配问题可以用KMP来解决.但是二维的就很难下手.我们可以将二维问题转化为一维 ...

  7. hdu4300 Clairewd’s message

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=4300 题目: Clairewd’s message Time Limit: 2000/1000 MS (J ...

  8. POJ3080 Blue Jeans —— 暴力枚举 + KMP / strstr()

    题目链接:https://vjudge.net/problem/POJ-3080 Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total ...

  9. 【poj 3080】Blue Jeans(字符串--KMP+暴力枚举+剪枝)

    题意:求n个串的字典序最小的最长公共子串. 解法:枚举第一个串的子串,与剩下的n-1个串KMP匹配,判断是否有这样的公共子串.从大长度开始枚举,找到了就break挺快的.而且KMP的作用就是匹配子串, ...

随机推荐

  1. java开学第一周测试自我感想

    开学第一周,王建民老师就对我们进行了java测试,对我们说测试题目是基于期末考试的基础难度来出的.我们的考试完全是靠暑假在家自学的基础,如果在家没有自学java,那完全就是看不懂试卷到底要考什么.由于 ...

  2. 【模板/经典题型】树上第k大

    直接对树dfs一发,对每个节点建出主席树. 查询的时候主席树上二分,四个参数x+y-lca(x,y)-fa[lca(x,y)]. 如果要求支持动态加边的话,只需要一个启发式合并即可,每次暴力重构主席树 ...

  3. 直播 APP 的直播实现流程

    直播平台搭建所涉及的事项非常的广泛, 不仅需要直播源码. 直播系统开发. 后台服务 器.专门的运维人员等, 还需要技术团队切实的把控.下面, 小编就给大家确切的说下直播 平台搭建需要用到哪些步骤. 1 ...

  4. https请求排错过程

    1. 看请求有没有到nginx 此时需要查看nginx的日志.一般每一个项目都会配置一个nginx站点,而一个站点都会又一个nginx配置文件,这个文件位于哪里呢?不出意外应该在:下面,如果找不到的话 ...

  5. js的正则表达式的替换

    <!DOCTYPE html><html><head><meta charset="UTF-8"><title>Inse ...

  6. python-day75--django项目问题详细

    1.项目名要小写 2.表中字段 AutoField()   表示整形字段,建表时不用写,当整形的数字范围不够你用的时候, 你可以用 BigAutoField()字段, 表示长整形    当表内新增有关 ...

  7. php获得时间段的月

    1.时间:$start_time = $_GET['start_time']; //2015-01$end_time = $_GET['end_time']; //2015-052.对时间进行拆分:$ ...

  8. UI基础五:简单的OP组件POPUP搜索帮助

    需求:给一个配置表,需要根据配置表来弹出选择框,并将选择的数据添加到SALES ORDER的项目 BSP_WD_CMPWB 新建组件:ZHSI_JPMPG 新建视图,适用VALUE NODE 参考表Z ...

  9. CRM WEB UI 03搜索界面新建按钮调到详细界面

    这个和上一个差不多,简单说下: 1.因为NEW是在创建搜索界面的时候加的,所以此时只需在结果界面重定义NEW事件: method EH_ONNEW. OP_NEW( ). endmethod. 2.结 ...

  10. Mybatis异常There is no getter for property named 'XXX' in 'class java.lang.String'

    1.当入参为 string类型时 (包括java.lang.String.)  我们使用#{xxx}引入参数.会抛异常There is no getter for property named 'XX ...