POJ - 3080 Blue Jeans 【KMP+暴力】(最大公共字串)
<题目链接>
题目大意:
就是求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+暴力】(最大公共字串)的更多相关文章
- POJ 3080 Blue Jeans (求最长公共字符串)
POJ 3080 Blue Jeans (求最长公共字符串) Description The Genographic Project is a research partnership between ...
- POJ 3080 Blue Jeans 找最长公共子串(暴力模拟+KMP匹配)
Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20966 Accepted: 9279 Descr ...
- POJ 3080 Blue Jeans(Java暴力)
Blue Jeans [题目链接]Blue Jeans [题目类型]Java暴力 &题意: 就是求k个长度为60的字符串的最长连续公共子串,2<=k<=10 规定: 1. 最长公共 ...
- poj 3080 Blue Jeans 解题报告
题目链接:http://poj.org/problem?id=3080 该题属于字符串处理中的串模式匹配问题.题目要求我们:给出一个DNA碱基序列,输出最长的相同的碱基子序列.(保证在所有的序列中都有 ...
- POJ 3080 Blue Jeans (多个字符串的最长公共序列,暴力比较)
题意:给出m个字符串,找出其中的最长公共子序列,如果相同长度的有多个,输出按字母排序中的第一个. 思路:数据小,因此枚举第一个字符串的所有子字符串s,再一个个比较,是否为其它字符串的字串.判断是否为字 ...
- POJ 3080 Blue Jeans (字符串处理暴力枚举)
Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21078 Accepted: ...
- poj 3080 Blue Jeans
点击打开链接 Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10243 Accepted: 434 ...
- 【POJ 3080 Blue Jeans】
Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 19026Accepted: 8466 Description The Genogr ...
- poj 3080 Blue Jeans (暴力枚举子串+kmp)
Description The Genographic Project is a research partnership between IBM and The National Geographi ...
随机推荐
- Android勒索软件研究报告
Android勒索软件研究报告 Author:360移动安全团队 0x00 摘要 手机勒索软件是一种通过锁住用户移动设备,使用户无法正常使用设备,并以此胁迫用户支付解锁费用的恶意软件.其表现为手机触摸 ...
- 论文笔记系列-Efficient Neural Architecture Search via Parameter Sharing
Summary 本文提出超越神经架构搜索(NAS)的高效神经架构搜索(ENAS),这是一种经济的自动化模型设计方法,通过强制所有子模型共享权重从而提升了NAS的效率,克服了NAS算力成本巨大且耗时的缺 ...
- Check Box、Radio Button、Combo Box控件使用
Check Box.Radio Button.Combo Box控件使用 使用控件的方法 1.拖动控件到对话框 2. 定义控件对应的变量(值变量或者控件变量) 3.响应控件各种消息 Check Box ...
- T_RegionNDS表创建及值
-- Table structure for t_regionnds -- ---------------------------- DROP TABLE IF EXISTS t_regionnds; ...
- Python 优雅获取本机 IP 方法【转】
转自:https://www.cnblogs.com/lfxiao/p/9672975.html 见过很多获取服务器本地IP的代码,个人觉得都不是很好,例如以下这些 不推荐:靠猜测去获取本地IP方法 ...
- find结合rm删除或mv移动文件的方法
删除过期的备份文件,多用find结合rm方法,可以使用-exec或xargs -exec rm -rf {} \; 或 find /home/mysqlbackup -name "*$thi ...
- 【转】如何安装JDK以及配置Java运行环境
具体的参考这篇博文就好了~~!http://www.cnblogs.com/liu-en-ci/p/6743106.html
- Visual Studio 2017中的快捷键
Ctrl+Tab: 快速切换活动文件
- Weex学习资料整合
1.weex weex文档:http://weex.apache.org/cn/guide/index.html Weex Ui awesome-weex WEEX免费视频教程-从入门到放肆 (共17 ...
- SeaJS入门教程系列之使用SeaJS(二)
SeaJS入门教程系列之使用SeaJS(二) 作者: 字体:[增加 减小] 类型:转载 时间:2014-03-03我要评论 这篇文章主要介绍了SeaJS入门教程系列之使用SeaJS,着重介绍了SeaJ ...