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. 胜利大逃亡 HDU - 1253

    Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的矩阵,刚开始Ignatius被关在(0 ...

  2. 【洛谷p1031】均分纸牌

    [博客园的第一条随笔,值得纪念一下] 均分纸牌[传送门] 洛谷上的算法标签是 这道题是一道贪心题,过了四遍才过(蒟蒻有点废) 第一遍的时候考虑的非常少,只想到了求出平均数→求差值→从左往右加差值: 这 ...

  3. flexbox与grid layout的区别

    flexbox是一种针对一维的局部布局,以轴为核心的弹性布局. grid layout是二维的更加全面的网格布局,

  4. node模块之path——path.join和path.resolve的区别

    1.path.join([...paths]) path.join() 方法使用平台特定的分隔符把全部给定的 path 片段连接到一起,并规范化生成的路径. 长度为零的 path 片段会被忽略. 如果 ...

  5. PAT 1011 World Cup Betting

    1011 World Cup Betting (20 分)   With the 2010 FIFA World Cup running, football fans the world over w ...

  6. [CodeForces - 197D] D - Infinite Maze

    D - Infinite Maze We've got a rectangular n × m-cell maze. Each cell is either passable, or is a wal ...

  7. Units about ASM

    1.ASM Striping and Mirroring:ASM supports two levels of striping: fine striping and coarse striping. ...

  8. vue 父组件通过props向子组件传递数据/方法的方式

    参考网址:https://segmentfault.com/a/1190000010507616 下面栗子中, callback是传递父组件的方法, mutationName是传递父组件的数据, Ap ...

  9. 网页定位点击事件js响应函数教程(Chrome)

    一.背景说明 在前端页面调试或者渗透测试(尤其是XSS)时,我们经常想定位js函数位置:比如点击了某个位置弹出了一个对话框,这是哪个文件的哪个js函数在响应. 本文以Chrome浏览器定位点击事件响应 ...

  10. 反向代理/负载均衡/session/cookie

    正向代理:客户端将流量重定向到burpsuite等软件或连接到VPN再访问服务器而不是直接访问服务器的场景.流量流动方向是真正机器--代理服务器.正向代理又称代理.普通代理. 反向代理:服务器端使用反 ...