POJ3080 Blue Jeans —— 暴力枚举 + KMP / strstr()
题目链接:https://vjudge.net/problem/POJ-3080
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 19152 | Accepted: 8524 |
Description
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
- 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
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()的更多相关文章
- POJ3080——Blue Jeans(暴力+字符串匹配)
Blue Jeans DescriptionThe Genographic Project is a research partnership between IBM and The National ...
- poj 3080 Blue Jeans (暴力枚举子串+kmp)
Description The Genographic Project is a research partnership between IBM and The National Geographi ...
- poj3080 Blue Jeans(暴枚+kmp)
Description The Genographic Project is a research partnership between IBM and The National Geographi ...
- poj3080 Blue Jeans【KMP】【暴力】
Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total Submissions:21746 Accepted: 9653 Descri ...
- hdu-2328(暴力枚举+kmp)
题意:给你n个字符串,问你这n个串的最长公共子串 解题思路:暴力枚举任意一个字符串的所有子串,然后暴力匹配,和hdu1238差不多的思路吧,这里用string解决的: 代码: #include< ...
- 【poj 3080】Blue Jeans(字符串--KMP+暴力枚举+剪枝)
题意:求n个串的字典序最小的最长公共子串. 解法:枚举第一个串的子串,与剩下的n-1个串KMP匹配,判断是否有这样的公共子串.从大长度开始枚举,找到了就break挺快的.而且KMP的作用就是匹配子串, ...
- kuangbin专题十六 KMP&&扩展KMP POJ3080 Blue Jeans
The Genographic Project is a research partnership between IBM and The National Geographic Society th ...
- POJ3080 Blue Jeans 题解 KMP算法
题目链接:http://poj.org/problem?id=3080 题目大意:给你N个长度为60的字符串(N<=10),求他们的最长公共子串(长度>=3). 题目分析:KMP字符串匹配 ...
- POJ3080 - Blue Jeans(KMP+二分)
题目大意 求N个字符串的最长公共字串 题解 和POJ1226做法一样...注意是字典序最小的...WA了一次 代码: #include <iostream> #include <cs ...
随机推荐
- NVMe与SCM结合将赋予存储介质的能力
转自:SCM是什么鬼,NVMe与其结合将赋予存储介质哪些能力? 全SSD闪存阵列在企业级存储得到广泛应用,相比传统机械硬盘,它的延迟.性能和可靠性都有了显著提高.许多早期开发商抓住其闪存技术优势的机遇 ...
- python和scrapy的安装【转:https://my.oschina.net/xtfjt1988/blog/364577】
抓取网站的代码实现很多,如果考虑到抓取下载大量内容scrapy框架无疑是一个很好的工具.Scrapy = Search+Pyton.下面简单列出安装过程.PS:一定要按照Python的版本下载,要不然 ...
- Day 13 Python 一之helloworld
直接肝程序吧! """ # 作业六:用户登录测试(三次机会) count = 1 while count <= 3: user = input('请输入用户名: ' ...
- git多人协作--分支
分支: 创建分支: git checkout -b 新分支 切换分支: git checkout 目标分支 删除分支: git branch -d 待删除分支 推送到远程分支: git checkou ...
- hdu5412CRB and Queries
动态修改求区间K大. 整体二分是一个神奇的东西: http://www.cnblogs.com/zig-zag/archive/2013/04/18/3027707.html 入门: 一般的主席树都挂 ...
- HSSF生成excel文件损坏
一开始我的代码是这样的: 然后打开创建的好的excel文件出现下面的问题:,, 这里改下代码就行,其实也不用改,添加下sheet就行,就是一开始是空的,没sheet,所以可能打不开,现在至少要创建一个 ...
- How to fill the background with image in landscape in IOS? 如何使image水平铺满屏幕
UIImageView *backgroundImage = [[UIImageView alloc] initWithFrame:self.view.frame]; [backgroundIm ...
- python遍历两个列表,若长度不等,用None填充
zip经常会遇到截断问题,如:a = [1,2,3], b = [4,5,6,7],则zip(a,b) = [(1, 4), (2, 5), (3, 6)] 可考虑使用map: map(lambda ...
- 转:linux下共享库的注意点之-fpic
转: http://www.cnblogs.com/leo0000/p/5691483.html linux下共享库的注意点之-fpic 在编译共享库必须加上-fpic.这是为什么呢? 首先看一个简单 ...
- three.js 源代码凝视(九)Math/Matrix4.js
商域无疆 (http://blog.csdn.net/omni360/) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:商域无疆 - 本博客专注于 敏捷开发 ...