题意:给出m个字符串,找出其中的最长公共子序列,如果相同长度的有多个,输出按字母排序中的第一个。

思路:数据小,因此枚举第一个字符串的所有子字符串s,再一个个比较,是否为其它字符串的字串。判断是否为字串的时候,将s的字符依次与其他字符串的字符比较。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <algorithm> using namespace std;
const int maxm=;
const int maxlen=;
char str[maxm][maxlen]; //存储m个字符串
char s[maxlen],ans[maxlen]; //s存储枚举str[0]的子字符串,ans存储m个字符串序列的最长公共序列
int n,m;
int len; //枚举子字符串的长度 //判断s是否为str[i]的子字符串
bool common(int i){
int flag;
for(int j=;j+len<=;j++){
flag=;
for(int k=;k<len;k++){
if(str[i][j+k]!=s[k]){
flag=;
break;
}
}
if(flag)
return ;
}
return ;
}
//判断子字符串是否为str[1]~str[m]的子字符串
bool isOk(){
for(int i=;i<m;i++){
if(!common(i)){
return ;
}
}
return ;
}
int main()
{
scanf("%d",&n);
while(n--){
int maxl=;
scanf("%d",&m);
for(int i=;i<m;i++)
scanf("%s",str[i]);
for(len=;len<=;len++){
for(int i=;i+len-<;i++){
strncpy(s,str[]+i,len); //将str[0]的前len个字符拷贝到s中去
s[len]='\0';
if(isOk()){
if(len>maxl){
maxl=len;
strcpy(ans,s);
}
//如果长度相同,但s的顺序在ans前,则改变ans值
else if(len==maxl && strcmp(s,ans)<){
strcpy(ans,s);
}
} }
}
if(maxl==)
printf("no significant commonalities\n");
else{
printf("%s\n",ans);
}
}
return ;
}

POJ 3080 Blue Jeans (多个字符串的最长公共序列,暴力比较)的更多相关文章

  1. poj 3080 kmp求解多个字符串的最长公共字串,(数据小,有点小暴力 16ms)

    Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14113   Accepted: 6260 Descr ...

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

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

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

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

  4. poj 3080 Blue Jeans 解题报告

    题目链接:http://poj.org/problem?id=3080 该题属于字符串处理中的串模式匹配问题.题目要求我们:给出一个DNA碱基序列,输出最长的相同的碱基子序列.(保证在所有的序列中都有 ...

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

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

  6. POJ 3080 Blue Jeans(Java暴力)

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

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

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

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

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

  9. poj 3080 Blue Jeans

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

随机推荐

  1. search help 概述

    所谓search help 就是在前台调出一个query 得到一个value list 让用户选择需要的值 我们要做的就是给某些表/视图 设计一个可供查询的query 然后把这个query绑定到需要的 ...

  2. sizeWithFont方法被弃用了,该怎么办?

    之前使用了NSString类的sizeWithFont:constrainedToSize:lineBreakMode:方法,但是该方法已经被iOS7 Deprecated了,而iOS7新出了一个bo ...

  3. div层遮盖flash(兼容浏览器)

    今天测试div层和flash的交互,发现div层总是被flash层遮盖,在百度上找了一会,说是加个<param name="wmode" value="transp ...

  4. 【转】IL编织 借助PostSharp程序集实现AOP

    ref:   C# AOP实现方法拦截器 在写程序的时候,很多方法都加了.日志信息.比如打印方法开始,方法结束,错误信息,等等. 由于辅助性功能的代码几乎是完全相同的,这样就会令同样的代码在各个函数中 ...

  5. Scrapy源码学习(一)

    用Scrapy已经有一段时间了,觉得该是看一下源码的时候了.最开始用的时候还是0.16的版本,现在稳定版已经到了0.18.结合使用Scrapy的过程,先从Scrapy的命令行看起. 一.准备 下载源代 ...

  6. php + apache + mysql环境搭建

    别人写的很好,若是不改变php默认访问路径的话,能够成功搭建是没问题的 http://www.cnblogs.com/pharen/archive/2012/02/06/2340628.html

  7. 简单的下拉刷新以及优化--SwipeRefreshLayout

    代码工程简要说明:以一个SwipeRefreshLayout包裹ListView,SwipeRefreshLayout接管ListView的下拉事件,若ListView被用户触发下拉动作后,Swipe ...

  8. ASP.NET中的母版页机制

    项目中用到了母版页,由于好长时间没用了,不太熟悉起原理,在网上找了一下: http://www.cnblogs.com/_zjl/archive/2011/06/12/2078992.html 有时间 ...

  9. MySQL多实例-精典故障案例

    很久以前搭建过MySQL多实例,记得当时很顺利,呵呵!今天公司因为业务需要,我再一次搭建多实例.安装完MySQL后,初始化两个实例时,出现如下报错: 150915  1:10:36 [ERROR] C ...

  10. ASP.NET Core 行军记 -----拔营启程

    ASP.NET MVC 6:https://docs.asp.net/en/latest/mvc/index.html ASP.NET Core :https://docs.asp.net/en/la ...