poj 3080 Blue Jeans 解题报告
题目链接:http://poj.org/problem?id=3080
该题属于字符串处理中的串模式匹配问题。题目要求我们:给出一个DNA碱基序列,输出最长的相同的碱基子序列。(保证在所有的序列中都有出现)
这里采用了Brute Force算法(由于碱基序列的串长仅为60,规模比较小),这是模式匹配的一种最简单的做法。
设: 最长公共字串为ans,其长度为maxlen。
m个碱基序列为p[0]...p[m-1]。由于公共子序列是每个碱基序列的子串,因此不妨枚举p[0]的每一个可能的子串s。以s为模式,分别以p[1]...p[m-1]为目标进行匹配计算:
若s为p[1]...p[m-1]的公共子串(strstr(p[k], s) != NULL, 1 <= k <= m-1),且s串的长度>maxlen,或者s的长度虽等于maxlen,但字典序小于目前最长的公共子串ans(strcmp(ans, s) > 0),则将s调整为最长公共子串(maxlen = s串的长度; strcpy(ans, s))。在枚举了p[0]的所有子串与p[1]...p[m-1]后,最终得出的最长公共子串ans即为问题的解。
#include <iostream>
#include <string.h>
using namespace std;
const int maxn = + ; // 碱基序列数的上限
const int maxs = + ; // 串长上限 int main()
{
char p[maxn][maxs], ans[maxs], s[maxs];
int i, j, k, len, maxlen, m, n;
while (scanf("%d", &n) != EOF)
{
while (n--)
{
memset(ans, , sizeof(ans)); // 最长公共子串
scanf("%d", &m); // 输入碱基序列的数目
for (i = ; i < m; i++) // 输入第i个碱基序列
scanf("%s", p[i]);
len = strlen(p[]);
maxlen = ; // 最长公共子串的长度
for (i = ; i < len; i++) // 枚举p[0]的每个子串,判断其是否为目标子串,子串的起始位置为i,结束位置为j
{
for (j = i+; j < len; j++)
{
strncpy(s, p[]+i, j-i+); // 提取该子串s(即长度为j-i+1,p[0]+i的所有字符复制到s中
s[j-i+] = '\0';
bool ok = true;
for (k = ; ok && k < m; k++)
{
if (strstr(p[k], s) == NULL) // 试探s是否为p[1]...p[m-1]的公共子串
{
ok = false;
break;
}
}
if (ok && (j-i+ > maxlen || maxlen == j-i+ && strcmp(ans, s) > )) // 若s是目前最长的公共子串,或者虽然s同属最长公共子串但字典序小,则s设为最长公共子串
{
maxlen = j-i+;
strcpy(ans, s);
}
}
}
if (maxlen < ) // 若最长的公共子串的长度不足3,则给出错误信息,否则输出最长公共子串
{
printf("no significant commonalities\n");
}
else
printf("%s\n", ans);
}
}
return ;
}
poj 3080 Blue Jeans 解题报告的更多相关文章
- POJ 3080 Blue Jeans (求最长公共字符串)
POJ 3080 Blue Jeans (求最长公共字符串) Description The Genographic Project is a research partnership between ...
- poj 3080 Blue Jeans
点击打开链接 Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10243 Accepted: 434 ...
- POJ 3080 Blue Jeans (字符串处理暴力枚举)
Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21078 Accepted: ...
- POJ 3080 Blue Jeans(Java暴力)
Blue Jeans [题目链接]Blue Jeans [题目类型]Java暴力 &题意: 就是求k个长度为60的字符串的最长连续公共子串,2<=k<=10 规定: 1. 最长公共 ...
- POJ 3080 Blue Jeans 找最长公共子串(暴力模拟+KMP匹配)
Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20966 Accepted: 9279 Descr ...
- poj 3080 Blue Jeans(水题 暴搜)
题目:http://poj.org/problem?id=3080 水题,暴搜 #include <iostream> #include<cstdio> #include< ...
- POJ 3080 Blue Jeans(后缀数组+二分答案)
[题目链接] http://poj.org/problem?id=3080 [题目大意] 求k个串的最长公共子串,如果存在多个则输出字典序最小,如果长度小于3则判断查找失败. [题解] 将所有字符串通 ...
- POJ 3080 Blue Jeans 后缀数组, 高度数组 难度:1
题目 http://poj.org/problem?id=3080 题意 有m个(2<=m<=10)不包含空格的字符串,长度为60个字符,求所有字符串中都出现过的最长公共子序列,若该子序列 ...
- poj 3080 Blue Jeans【字符串处理+ 亮点是:字符串函数的使用】
题目:http://poj.org/problem?id=3080 Sample Input 3 2 GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCA ...
随机推荐
- CoreOS Architecture Learning
目录 . CoreOS简介 . CoreOS部署.安装.使用 . CoreOS命令使用 1. CoreOS简介 0x1: CoreOS和Docker的关系 我们先来看一张Docker的架构图
- RIP、OSPF、BGP、动态路由选路协议、自治域AS
相关学习资料 tcp-ip详解卷1:协议.pdf http://www.rfc-editor.org/rfc/rfc1058.txt http://www.rfc-editor.org/rfc/rfc ...
- TCP/IP详解 学习三
网际协议 ip Ip 是不可靠和无连接的 ip首部 4个字节的 32 bit值以下面的次序传输:首先是 0-7 bit,其次 8-15 bit,然后 1 6-23 bit,最后是 24~31 bit. ...
- javax/faces/webapp/FacesServlet
严重: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component ...
- Longest Increasing Common Subsequence (LICS)
最长上升公共子序列(Longest Increasing Common Subsequence,LICS)也是经典DP问题,是LCS与LIS的混合. Problem 求数列 a[1..n], b[1. ...
- abstract 类 构造函数
public abstract class CommonReq { private String TransNo { get; set; } public String SubmitData { ge ...
- ios 随机色 宏定义
#define RGBColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1] #d ...
- 如何使用网盘托管git项目
话说近年来git已经成为项目源代码管理的标准工具,有不少免费托管网站可供使用,详情参考这篇文章: http://www.cnblogs.com/zdz8207/archive/2012/05/20/2 ...
- Javascript输出表格
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- SQL2005删除复制数据库的发布与订阅的方法(转载)
SQL2005删除复制数据库的发布与订阅的方法 --在测试环境中恢复从正式数据库服务器 上备份下来的bak文件后,正式环境里数据库复制的发布.订阅也被带进来了,结果恢复的数据库无法更改表结构,直接删除 ...