F - Blue Jeans

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status

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 题意及题解转自:http://blog.csdn.net/qiqijianglu/article/details/7851454

题意:求n个字符串的最长公共串。

求n个字符长度最长公共子串。对于多模式匹配问题,一般是不可以用KMP解决得,因为忒暴力。

思路很简单:我们先按字符串的长度由短到长进行快排。枚举第一个字符串的不同长度子串,判断她是否为下面多有的公共子串?如果是的话,那么我们就表明找到,则比较其长度,如果比已经找到的串长,那么就替换结果串 否则按字典序比较。取字典序考前的,就可以。

 #include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<string> #define N 65
#define M 105
#define mod 10000007
//#define p 10000007
#define mod2 1000000000
#define ll long long
#define LL long long
#define eps 1e-6
#define inf 100000000
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int T;
int n;
char text[N][N];
char result[N];
int ma;
int l;
int le;
char pat[N];
int next[N];
int mma; void ini()
{
int i;
ma=-;
scanf("%d",&n);
for(i=;i<=n;i++){
scanf("%s",text[i]);
}
l=strlen(text[]);
} void get_next()
{
memset(next,-,sizeof(next));
int i,j;
j=-;next[]=-;
i=;
while(i<le)
{
if(j==- || pat[i]==pat[j]){
i++;j++;next[i]=j;
}
else{
j=next[j];
}
}
} void KMP()
{
int i,j,k,m;
mma=;
for(k=;k<=n;k++){
i=;j=;m=;
while(i<l && j<le)
{
if(j==- || text[k][i]==pat[j])
{
i++;j++;
m=max(m,j);
}
else{
j=next[j];
}
}
mma=min(m,mma);
}
} void solve()
{
int i;
char te[N];
for(i=;i<l;i++){
strcpy(pat,text[]+i);
le=strlen(pat);
get_next();
KMP();
if(mma>ma){
ma=mma;
strncpy(result,text[]+i,ma);
result[ma]='\0';
}
else if(mma==ma){
strncpy(te,text[]+i,ma);
result[ma]='\0';
if(strcmp(te,result)==-){
strcpy(result,te);
}
}
}
} void out()
{
if(ma<){
printf("no significant commonalities\n");
}
else{
printf("%s\n",result);
}
} int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
scanf("%d",&T);
//for(int ccnt=1;ccnt<=T;ccnt++)
while(T--)
//scanf("%d%d",&n,&m);
//while(scanf("%s",s)!=EOF)
{
ini();
solve();
out();
}
return ;
}

POJ Blue Jeans [枚举+KMP]的更多相关文章

  1. poj3080 Blue Jeans【KMP】【暴力】

    Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:21746   Accepted: 9653 Descri ...

  2. POJ 3080-Blue Jeans【kmp,字符串剪接】

    Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20695   Accepted: 9167 Descr ...

  3. POJ 3080 Blue Jeans (KMP)

    求出公共子序列  要求最长  字典序最小 枚举第一串的所有子串   然后对每一个串做KMP.找到目标子串 学会了   strncpy函数的使用   我已可入灵魂 #include <iostre ...

  4. POJ - 3080 Blue Jeans 【KMP+暴力】(最大公共字串)

    <题目链接> 题目大意: 就是求k个长度为60的字符串的最长连续公共子串,2<=k<=10 限制条件: 1.  最长公共串长度小于3输出   no significant co ...

  5. POJ3080 Blue Jeans 题解 KMP算法

    题目链接:http://poj.org/problem?id=3080 题目大意:给你N个长度为60的字符串(N<=10),求他们的最长公共子串(长度>=3). 题目分析:KMP字符串匹配 ...

  6. (字符串 KMP)Blue Jeans -- POJ -- 3080:

    链接: http://poj.org/problem?id=3080 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88230#probl ...

  7. POJ 3080 Blue Jeans 找最长公共子串(暴力模拟+KMP匹配)

    Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20966   Accepted: 9279 Descr ...

  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 (字符串处理暴力枚举)

    Blue Jeans  Time Limit: 1000MS        Memory Limit: 65536K Total Submissions: 21078        Accepted: ...

随机推荐

  1. 利用kubeadm快速部署k8s

    内外网络互通 [root@k8s-1 ~]# cat /etc/redhat-release CentOS Linux release 7.6.1810 (Core) 配置k8syum仓库,及Dock ...

  2. Codeforces Round #275 (Div. 2)-A. Counterexample

    http://codeforces.com/contest/483/problem/A A. Counterexample time limit per test 1 second memory li ...

  3. SniperOJ-leak-x86-64

    参考:1.借助DynELF实现无libc的漏洞利用小结 2.一步一步学ROP之linux_x64篇 - 蒸米 题目源码 #include <stdio.h> #include <un ...

  4. 洛谷五月月赛【LGR-047】划水记

    虽然月赛有些爆炸,但我永远资瓷洛谷! 因为去接水,所以迟到了十几分钟,然后洛谷首页就打不开了-- 通过洛谷题库间接打开了比赛,看了看\(TA\),WTF?博弈论?再仔细读了读题,嗯,判断奇偶性,不过要 ...

  5. 原 荐 使用Spring Boot Actuator、Jolokia和Grafana实现准实时监控

    原 荐 使用Spring Boot Actuator.Jolokia和[可视化]Grafana实现准实时监控.   监控系统:          日志- 基础处理 - 表格 - 可视化一体化解决方案. ...

  6. __new__.py

    def func(self): print('hello %s' %self.name)def __init__(self,name,age): self.name = name self.age = ...

  7. Apache安装错误 APR not found解决方法

    在配置Apache的时候,出现错误 原因是缺少一些依赖包,安装这些依赖包就行了 下载依赖包,注意我这里下载的与参考链接上的有些不同,安装上也有不一样 wget http://archive.apach ...

  8. 七周成为数据分析师04_Excel

    Excel适用于敏捷.快速.需要立即响应的需求: 而 Python.BI 等适用于常规.频繁.可复用可工程化的需求 设计到 Excel 的内容主要需要进行实操练习,这里只做一个陈列,具体知识请参考: ...

  9. u-boot顶层目录config.mk分析

    1. 设置obj与src ifneq ($(OBJTREE),$(SRCTREE)) ifeq ($(CURDIR),$(SRCTREE)) dir := else dir := $(subst $( ...

  10. 杭电 5773 The All-purpose Zero

    Description ?? gets an sequence S with n intergers(0 < n <= 100000,0<= S[i] <= 1000000). ...