题目链接: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 解题报告的更多相关文章

  1. POJ 3080 Blue Jeans (求最长公共字符串)

    POJ 3080 Blue Jeans (求最长公共字符串) Description The Genographic Project is a research partnership between ...

  2. poj 3080 Blue Jeans

    点击打开链接 Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10243   Accepted: 434 ...

  3. POJ 3080 Blue Jeans (字符串处理暴力枚举)

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

  4. POJ 3080 Blue Jeans(Java暴力)

    Blue Jeans [题目链接]Blue Jeans [题目类型]Java暴力 &题意: 就是求k个长度为60的字符串的最长连续公共子串,2<=k<=10 规定: 1. 最长公共 ...

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

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

  6. poj 3080 Blue Jeans(水题 暴搜)

    题目:http://poj.org/problem?id=3080 水题,暴搜 #include <iostream> #include<cstdio> #include< ...

  7. POJ 3080 Blue Jeans(后缀数组+二分答案)

    [题目链接] http://poj.org/problem?id=3080 [题目大意] 求k个串的最长公共子串,如果存在多个则输出字典序最小,如果长度小于3则判断查找失败. [题解] 将所有字符串通 ...

  8. POJ 3080 Blue Jeans 后缀数组, 高度数组 难度:1

    题目 http://poj.org/problem?id=3080 题意 有m个(2<=m<=10)不包含空格的字符串,长度为60个字符,求所有字符串中都出现过的最长公共子序列,若该子序列 ...

  9. poj 3080 Blue Jeans【字符串处理+ 亮点是:字符串函数的使用】

    题目:http://poj.org/problem?id=3080 Sample Input 3 2 GATACCAGATACCAGATACCAGATACCAGATACCAGATACCAGATACCA ...

随机推荐

  1. Cocos2d-X3.0 刨根问底(五)----- Node类及显示对象列表源码分析

    上一章 我们分析了Cocos2d-x的内存管理,主要解剖了 Ref.PoolManager.AutoreleasePool这三个类,了解了对象是如何自动释放的机制.之前有一个类 Node经常出现在各种 ...

  2. 12.Android之Tabhost组件学习

    TabHost是整个Tab的容器,TabHost的实现有两种方式: 第一种继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost.各个Tab中的内容在布 ...

  3. POJ2286 The Rotation Game

    Description The rotation game uses a # shaped board, which can hold 24 pieces of square blocks (see ...

  4. POJ1065 Area

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18499   Accepted: 5094 Description You ...

  5. nginx配置ssl双向验证 nginx https ssl证书配置

    1.安装nginx 参考<nginx安装>:http://www.ttlsa.com/nginx/nginx-install-on-linux/ 如果你想在单IP/服务器上配置多个http ...

  6. CentOS 6.4 32位系统 LAMP(Apache+MySQL+PHP)安装步骤

    先来解释一下,什么是 LAMP.正如标题所言,LAMP 实际上就是 Linux.Apache.MySQL.PHP 四个名称的缩写,当然最后一个 “P” 还有其他说法是 Perl 或者 Python.不 ...

  7. tableView左滑删除功能

    实现三个代理方法即可 -(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtI ...

  8. JS 下拉菜单

    HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <titl ...

  9. C# 根据IP地址获取城市

    using System; using System.IO; using System.Net; using System.Text; using System.Web.Script.Serializ ...

  10. (转)MFC中获得各个类的指针/句柄 ID的总结

    http://www.cnblogs.com/ylhome/archive/2009/10/06/1578478.html 一般我们使用的框架是VC提供的Wizard生成的MFC App Wizard ...