<题目链接>

题目大意:

就是求k个长度为60的字符串的最长连续公共子串,2<=k<=10

限制条件:

1、  最长公共串长度小于3输出   no significant commonalities

2、  若出现等长的最长的子串,则输出字典序最小的串

解题分析:

将第一个字串的所有子串枚举出来,然后用KMP快速判断该子串是否在所有主串中出现,如果都出现过,那么就按该子串的长度和字典序,不断更新答案,直到得到最终的最优解。

#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
const int N=+;
const int M=+; string s[N];
int nxt[M]; void getnext(string str,int len){
int i=,j=-;
nxt[]=-;
while(i<len){
if(j==-||str[i]==str[j])
nxt[++i]=++j;
else j=nxt[j];
}
}
bool kmp(string s1,int len1,string s2,int len2){
int i=,j=;
while(i<len1){
if(j==-||s1[i]==s2[j])
++i,++j;
else j=nxt[j];
if(j==len2)return true;
}
return false;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
int T,n;
cin>>T;
while(T--){
cin>>n;
for(int i=;i<n;i++)cin>>s[i];
string ans="";
for(int i=;i<s[].size();i++) //起点
{
for(int j=;i+j<=s[].size();j++) //长度
{
string res=s[].substr(i,j);
getnext(res,res.size());
bool flag=;
for(int k=;k<n;k++)
if(!kmp(s[k],s[k].size(),res,res.size()))
flag=;
if(!flag)
{
if(ans.size()<res.size())ans=res; //优先长度更长的公共子串
else if(ans.size()==res.size())ans=min(ans,res); //长度相同,按字典序排序
}
}
}
if(ans.size()<)cout<<"no significant commonalities"<<endl;
else cout<<ans<<endl;
}
return ;
}

2018-08-07

POJ - 3080 Blue Jeans 【KMP+暴力】(最大公共字串)的更多相关文章

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

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

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

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

  3. POJ 3080 Blue Jeans(Java暴力)

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

  4. poj 3080 Blue Jeans 解题报告

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

  5. POJ 3080 Blue Jeans (多个字符串的最长公共序列,暴力比较)

    题意:给出m个字符串,找出其中的最长公共子序列,如果相同长度的有多个,输出按字母排序中的第一个. 思路:数据小,因此枚举第一个字符串的所有子字符串s,再一个个比较,是否为其它字符串的字串.判断是否为字 ...

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

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

  7. poj 3080 Blue Jeans

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

  8. 【POJ 3080 Blue Jeans】

    Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 19026Accepted: 8466 Description The Genogr ...

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

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

随机推荐

  1. Python(十) Python 中的 *args 和 **kwargs

    转载:团子的小窝 http://kodango.com/variable-arguments-in-python

  2. mysql 架构 ~ binlog_server

    一 简介 mysql binlog server 二 目的 实时备份线上的binlog 利用备份+binlog进行任何时间点的恢复 三  详细信息    1 基本命令 /usr/local/mysql ...

  3. JS浮点数运算Bug的解决办法

    方法一:重写浮点运算的函数 //除法函数,用来得到精确的除法结果 //说明:javascript的除法结果会有误差,在两个浮点数相除的时候会比较明显.这个函数返回较为精确的除法结果. //调用:acc ...

  4. android fragment解析

    1.fragment加载到Activity (1).添加fragment到Activity的布局文件 (2).动态在activity中添加fragment 例子: // 步骤1:获取FragmentM ...

  5. Spring+SpringMVC+mybatis整合以及注解的使用(三)

    1.包结构:

  6. vlc-android 的编译过程

    参考官方文档:https://wiki.videolan.org/AndroidCompile#Get_VLC_Source 值得注意的的地方: 1.切记安装以下工具 sudo apt-get ins ...

  7. SpringMvc整合Mybatis并使用声明式事务

    (1).引入相关依赖 <dependency> <groupId>com.alibaba</groupId> <artifactId>druid< ...

  8. C++获取当前所有进程的完整路径

    实现代码 #include <stdio.h> #include <windows.h> #include <tlhelp32.h> #include <st ...

  9. Python Tools for Machine Learning

    Python Tools for Machine Learning Python is one of the best programming languages out there, with an ...

  10. NandFlash和iNand

    nand 1.nand的单元组织:block与page(大页Nand与小页Nand)(1)Nand的页和以前讲过的块设备(尤其是硬盘)的扇区是类似的.扇区最早在磁盘中是512字节,后来也有些高级硬盘扇 ...