Blue Jeans
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 14113   Accepted: 6260

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
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
char test[][];
int next[];
int len1;
char head[];
int n;
int ma;
char result[];
void getnext(){
memset(next,,sizeof(next));
int i=,j=-;
next[]=-;
while(i<len1){
if(j==-||head[i]==head[j]){
i++;
j++;
next[i]=j;
}
else
j=next[j];
}
} int kmp(){
ma=;
for(int ti=;ti<n;ti++){
int i=,j=,m=;
while(i<){
if(j==-||test[ti][i]==head[j]){
i++;
j++;
}
else
j=next[j];
if(j>m)
m=j;
}
if(m<ma)
ma=m; }
return ma;
} int main(){
int T;
scanf("%d",&T);
while(T--){
memset(test,,sizeof(test));
memset(result,,sizeof(result));
scanf("%d",&n);
getchar();
for(int i=;i<n;i++){
cin>>test[i];
}
int ans=;
for(int i=;i<=;i++){
memset(head,,sizeof(head));
len1=-i;
strcpy(head,test[]+i); /// 枚举第一个串的所有后缀串(当然最后2个可以省去)
head[len1]='\0';
getnext();
int temp=kmp(); /// KMP求出这个后缀串与其余所有串的最大匹配。
if(temp>ans){
ans=temp;
strncpy(result,test[]+i,ans);
}
else if(temp==ans){ /// 存在多个最长公共子串,输出字典序最小的,WA了一次。
if(strcmp(test[]+i,result)<)
strncpy(result,test[]+i,ans); /// 复习: strncpy()没有复制最后的'\0'。
} }
if(ans<)
printf("no significant commonalities\n");
else
printf("%s\n",result);
}
return ;
}

poj 3080 kmp求解多个字符串的最长公共字串,(数据小,有点小暴力 16ms)的更多相关文章

  1. (字符串)最长公共字串(Longest-Common-SubString,LCS)

    题目: 给定两个字符串X,Y,求二者最长的公共子串,例如X=[aaaba],Y=[abaa].二者的最长公共子串为[aba],长度为3. 子序列是不要求连续的,字串必须是连续的. 思路与代码: 1.简 ...

  2. java_基础知识_字符串练习题_计算两个字符串的最长公共字串长度

    package tek; Java算法——求出两个字符串的最长公共字符串 /** * @Title: 问题:有两个字符串str1和str2,求出两个字符串中最长公共字符串. * @author 匹夫( ...

  3. poj 2774 后缀数组 两个字符串的最长公共子串

    Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 31904   Accepted: 12 ...

  4. POJ 2774 (后缀数组 最长公共字串) Long Long Message

    用一个特殊字符将两个字符串连接起来,然后找最大的height,而且要求这两个相邻的后缀的第一个字符不能在同一个字符串中. #include <cstdio> #include <cs ...

  5. POJ - 2774~POJ - 3415 后缀数组求解公共字串问题

    POJ - 2774: 题意: 求解A,B串的最长公共字串 (摘自罗穗骞的国家集训队论文): 算法分析: 字符串的任何一个子串都是这个字符串的某个后缀的前缀. 求 A 和 B 的最长 公共子串等价于求 ...

  6. Python-求解两个字符串的最长公共子序列

    一.问题描述 给定两个字符串,求解这两个字符串的最长公共子序列(Longest Common Sequence).比如字符串1:BDCABA:字符串2:ABCBDAB.则这两个字符串的最长公共子序列长 ...

  7. POJ - 3294~Relevant Phrases of Annihilation SPOJ - PHRASES~Substrings POJ - 1226~POJ - 3450 ~ POJ - 3080 (后缀数组求解多个串的公共字串问题)

    多个字符串的相关问题 这类问题的一个常用做法是,先将所有的字符串连接起来, 然后求后缀数组 和 height 数组,再利用 height 数组进行求解. 这中间可能需要二分答案. POJ - 3294 ...

  8. C++求解汉字字符串的最长公共子序列 动态规划

        近期,我在网上看了一些动态规划求字符串最长公共子序列的代码.可是无一例外都是处理英文字符串,当处理汉字字符串时.常常会出现乱码或者不对的情况. 我对代码进行了改动.使用wchar_t类型存储字 ...

  9. 求两个字符串的最长公共子串——Java实现

    要求:求两个字符串的最长公共子串,如“abcdefg”和“adefgwgeweg”的最长公共子串为“defg”(子串必须是连续的) public class Main03{ // 求解两个字符号的最长 ...

随机推荐

  1. Yarn下Map数控制

    public List<InputSplit> getSplits(JobContext job) throws IOException { long minSize = Math.max ...

  2. phpmyadmin高级功能尚未完全设置部分功能未激活

    1.登录phpmyadmin,点击导入,选择/var/ww/html/phpmyadmin/examples/create_tables.sql并执行 完成后可以看到多出了一个库phpmyadmin. ...

  3. AngularJS THML DOM

    AngularJS为HTML Dom元素属性提供了绑定应用数据的指令. data-ng-disabled指令直接提供了绑定应用程序的数据到HTML元素的disabled属性. <!DOCTYPE ...

  4. 最近的阅读list

    fast rcnn 统一了sppnet和rcnn,将原来rcnn分stage的训练合为一个整体的stage,一次完成cls, regression的训练.引入两个loss函数,一个是用来进行cls的, ...

  5. Sass 语法格式及编译

    一.sass语法格式 这里说的 Sass 语法是 Sass 的最初语法格式,他是通过 tab 键控制缩进的一种语法规则,而且这种缩进要求非常严格.另外其不带有任何的分号和大括号.常常把这种格式称为 S ...

  6. 1、SpringBoot+Mybatis整合------简单CRUD的实现

    编译工具:STS 代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis01/commit/b757cd9bfa4e2de551b2e9 ...

  7. LeetCode-177:第N高的薪水

    第N高的薪水与第二高的薪水,解题思路是一样的,只要对LeetCode-176的SQL做一下变形,便可以满足这题,详见:https://www.cnblogs.com/zouqf/p/10282392. ...

  8. 网络基础-交换机、路由器、OSI7层模型

    第1章 网络基础 1.1 网络的诞生 网络的诞生使命:通过各种互联网服务提升全球人类生活品质. 让人类的生活更便捷和丰富,从而促进全球人类社会的进步.并且丰富人类的精神世界和物质世界,让人类最便捷地获 ...

  9. ayui弹出层闪退,layer弹出层闪退,layer弹出层坑

    今天用layui的弹出层插件,发现两奇怪的问题: 1.弹窗打开事件还未绑定到任何按钮,可是点击form表单中的按钮可以打开我定义的弹出层 2.绑定弹出层到按钮,打开弹窗闪退 后面发现真如参考博文所说: ...

  10. storm实时计算实例(socket实时接入)

    介绍 实现了一个简单的从实时日志文件监听,写入socket服务器,再接入Storm计算的一个流程. 源码 日志监听实时写入socket服务器   package socket; import java ...