<题目链接>

题目大意:

就是求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. D - Maximizing Advertising

    题目链接:https://cn.vjudge.net/contest/250168#problem/D 题目大意:给你一些点的坐标,这些点属于两个帮派,让你将这些点分进两个不能重叠的矩形中,问你最多两 ...

  2. mysql 架构~多写模式MGR

    一  简介:今天咱们来聊聊MGR的单主切换和新节点加入二 单主模式下变成多主:  1 3306  STOP group_replication;  set global group_replicati ...

  3. linux相关设置

    mysql开机自启: [root@workstudio system]# systemctl enable mysqld

  4. java 多线程二

    java 多线程一 java 多线程二 java 多线程三 java 多线程四 线程中断: /** * Created by root on 17-9-30. */ public class Test ...

  5. Android常用逆向工具+单机游戏破解

    android开发环境搭建 我理解的学习路线是首先要掌握和了解常见的工具.搭建环境.然后就是缓慢的积累特征,通过长期的练习使自己进步,通过android逆向课程的学习.常用的工具如下: android ...

  6. Two Sum I & II & III & IV

    Two Sum I Given an array of integers, find two numbers such that they add up to a specific target nu ...

  7. [转]数据对齐对CPU的影响

    [转]http://www.cnblogs.com/wuzhenbo/archive/2012/06/05/2537465.html 1.前言 在IBM开发社区上发现一篇叫'Data alignmen ...

  8. [转]解决win7 64位操作系统下安装PL/SQL后连接报错问题: make sure you have the 32 bits oracle client installed

    1. 在Oracle官网(http://www.oracle.com/technetwork/topics/winsoft-085727.html)下载文件: instantclient-basic- ...

  9. Ex 3_17 无穷路径..._十一次作业

    (a) Inf(p)在p中出现了无穷多次,说明Inf(p)存在一个环当中,所以这个环的顶点肯定是某一个强连通部件的子集. (b) 若G中存在一条无穷路径,则G中至少存在一个环,且这个环至少有两个顶点, ...

  10. EntityFramework codefirst Enable-Migrations No context type was found in the assembly “MyApp.Web” error

    Enable-Migrations -Force -ContextTypeName "MyApp.Repository.DataContext" -ProjectName &quo ...