题目链接:https://vjudge.net/problem/POJ-3080

Blue Jeans
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 19152   Accepted: 8524

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

题解:

找出所有字符串的最长公共连续子串。

直接枚举第一个字符串的每个子串,然后通过kmp算法或者strstr()函数,取判断该子串是否存在于每一个字符串。

KMP:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
typedef long long LL;
const double eps = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = +; char s[MAXN][MAXN], tmp[MAXN], ans[MAXN];
int Next[MAXN], Len[MAXN]; void get_next(char x[], int m)
{
int i, j;
j = Next[] = -;
i = ;
while(i<m)
{
while(j!=- && x[i]!=x[j]) j = Next[j];
Next[++i] = ++j;
}
} bool kmp(char x[], int m, char y[], int n)
{
int i, j;
get_next(x, m);
i = j = ;
while(i<n)
{
while(j!=- && y[i]!=x[j]) j = Next[j];
i++; j++;
if(j>=m) return true;
}
return false;
} int main()
{
int T, n;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
for(int i = ; i<=n; i++)
scanf("%s", s[i]), Len[i] = strlen(s[i]); bool hav_ans;
for(int len = Len[]; len>=; len--)
{
hav_ans = false;
for(int st = ; st<=Len[]-len; st++)
{
int en = st+len-, cnt = ;
for(int i = st; i<=en; i++)
tmp[cnt++] = s[][i];
tmp[cnt] = ; bool all = true;
for(int i = ; i<=n; i++)
all = all&&kmp(tmp, cnt, s[i], Len[i]); if(all)
{
if(!hav_ans || strcmp(tmp, ans)<)
{
strcpy(ans, tmp);
hav_ans = true;
}
}
}
if(hav_ans) break;
}
if(hav_ans) puts(ans);
else puts("no significant commonalities");
}
}

strstr():

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
typedef long long LL;
const double eps = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = +; char s[MAXN][MAXN], tmp[MAXN], ans[MAXN]; int main()
{
int T, n;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
for(int i = ; i<=n; i++)
scanf("%s", s[i]); int Len = strlen(s[]);
bool hav_ans;
for(int len = Len; len>=; len--)
{
hav_ans = false;
for(int st = ; st<=Len-len; st++)
{
int en = st+len-, cnt = ;
for(int i = st; i<=en; i++)
tmp[cnt++] = s[][i];
tmp[cnt] = ; bool all = true;
for(int i = ; i<=n; i++)
all = all&&(strstr(s[i], tmp)); if(all)
{
if(!hav_ans || strcmp(tmp, ans)<)
{
strcpy(ans, tmp);
hav_ans = true;
}
}
}
if(hav_ans) break;
}
if(hav_ans) puts(ans);
else puts("no significant commonalities");
}
}

POJ3080 Blue Jeans —— 暴力枚举 + KMP / strstr()的更多相关文章

  1. POJ3080——Blue Jeans(暴力+字符串匹配)

    Blue Jeans DescriptionThe Genographic Project is a research partnership between IBM and The National ...

  2. poj 3080 Blue Jeans (暴力枚举子串+kmp)

    Description The Genographic Project is a research partnership between IBM and The National Geographi ...

  3. poj3080 Blue Jeans(暴枚+kmp)

    Description The Genographic Project is a research partnership between IBM and The National Geographi ...

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

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

  5. hdu-2328(暴力枚举+kmp)

    题意:给你n个字符串,问你这n个串的最长公共子串 解题思路:暴力枚举任意一个字符串的所有子串,然后暴力匹配,和hdu1238差不多的思路吧,这里用string解决的: 代码: #include< ...

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

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

  7. kuangbin专题十六 KMP&&扩展KMP POJ3080 Blue Jeans

    The Genographic Project is a research partnership between IBM and The National Geographic Society th ...

  8. POJ3080 Blue Jeans 题解 KMP算法

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

  9. POJ3080 - Blue Jeans(KMP+二分)

    题目大意 求N个字符串的最长公共字串 题解 和POJ1226做法一样...注意是字典序最小的...WA了一次 代码: #include <iostream> #include <cs ...

随机推荐

  1. OpenJ_Bailian3375

    Farmer John has decided to reward his cows for their hard work by taking them on a tour of the big c ...

  2. 转 C++STL之string

    http://www.cnblogs.com/wangkangluo1/archive/2011/07/22/2114118.html string类的构造函数: string(const char ...

  3. 标准C程序设计七---07

    Linux应用             编程深入            语言编程 标准C程序设计七---经典C11程序设计    以下内容为阅读:    <标准C程序设计>(第7版) 作者 ...

  4. HDU 5667 Sequence【矩阵快速幂+费马小定理】

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5667 题意: Lcomyn 是个很厉害的选手,除了喜欢写17kb+的代码题,偶尔还会写数学题.他找到 ...

  5. Jetson TK1 五:移植工控机程序到板上

    1.gazebo xml 2.王 chmod 777 chmod 777 /home/robot2/bzrobot_ws/src/bzrobot/bzrobot_control/bzrobot_con ...

  6. raspi扩展板

    1.Ciseco Slice 2.Adafruit(Plate) 3.Fen的Gertboard:(详见F盘下的使用手册) 12个缓冲输入输出端口 LED状态指示灯 3个按钮开关 6个开路集成电极 1 ...

  7. logistics regression

    logistics regression用于解决一些二分类问题.比如(纯假设)网上购物时,网站会判断一个人退货的可能性有多大,如果该用户退货的可能性很大,那么网站就不会推荐改用户购买退费险.反之,如果 ...

  8. 【Linux学习笔记】栈与函数调用惯例

    栈与函数调用惯例(又称调用约定)— 基础篇 记得一年半前参加百度的校招面试时,被问到函数调用惯例的问题.当时只是懂个大概,比如常见函数调用约定类型及对应的参数入栈顺序等.最近看书过程中,重新回顾了这些 ...

  9. C# UserControl 判断是否是设计模式中

    In Windows Forms application, we can use Control.IsInDesignMode or LicenseManager.UsageMode == Licen ...

  10. 数据库系统学习(八)-SQL语言与数据库完整性和安全性

    第八讲 SQL语言与数据库完整性 重难点 数据库完整性的概念 关系数据库 防止和避免数据库中不合理数据的出现 输入错误,操作失误,程序处理错误等 完整性约束条件的一般形式 对O操作集合,当出现A情况时 ...