【poj 3080】Blue Jeans(字符串--KMP+暴力枚举+剪枝)
题意:求n个串的字典序最小的最长公共子串。
解法:枚举第一个串的子串,与剩下的n-1个串KMP匹配,判断是否有这样的公共子串。从大长度开始枚举,找到了就break挺快的。而且KMP的作用就是匹配子串,近乎O(n)的速度,很快。
P.S.对于字符串要仔细!!!
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std; const int L=;
int n;
int next[],nextt[];
char s[][],ss[],str[]; bool kmp(int x,int l,int r)
{
/*for (int i=1;i<=L;i++)
{
if (!next[i]) nextt[i]=l-1;
else nextt[i]=next[i];
}WA!!*/
for (int i=l;i<=r;i++) ss[i-l+]=s[][i];
memset(next,,sizeof(next));
int p=;
next[]=;
for (int i=;i<=r-l+;i++)
{
while (ss[i]!=ss[p+] && p) p=next[p];
if (ss[i]==ss[p+]) p++;
next[i]=p;
}
p=;
for (int i=;i<=L;i++)
{
while (s[x][i]!=ss[p+] && p) p=next[p];
if (s[x][i]==ss[p+]) p++;
if (p==r-l+) return true;
}
return false;
}
bool check(int l,int r)
{
for (int i=;i<=n;i++)
if (!kmp(i,l,r)) return false;//O(n*n)匹配
return true;
}
int main()
{
int T;
scanf("%d",&T);
while (T--)
{
scanf("%d",&n);
for (int i=;i<=n;i++) scanf("%s",s[i]+);
bool ok=false;
for (int len=L;len>=;len--)
{
for (int i=;i+len-<=L;i++)
{
if (!check(i,i+len-)) continue;
memset(ss,'\0',sizeof(ss));//
for (int j=i;j<=i+len-;j++) ss[j-i]=s[][j];
if (!ok||(ok && strcmp(ss,str)<)) strcpy(str,ss);
ok=true;
}
if (ok) break;
}
if (!ok) printf("no significant commonalities\n");
else printf("%s\n",str);
}
return ;
}
【poj 3080】Blue Jeans(字符串--KMP+暴力枚举+剪枝)的更多相关文章
- POJ 3080 Blue Jeans (字符串处理暴力枚举)
Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21078 Accepted: ...
- POJ - 3080 Blue Jeans 【KMP+暴力】(最大公共字串)
<题目链接> 题目大意: 就是求k个长度为60的字符串的最长连续公共子串,2<=k<=10 限制条件: 1. 最长公共串长度小于3输出 no significant co ...
- POJ 3080 Blue Jeans (KMP)
求出公共子序列 要求最长 字典序最小 枚举第一串的所有子串 然后对每一个串做KMP.找到目标子串 学会了 strncpy函数的使用 我已可入灵魂 #include <iostre ...
- 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
点击打开链接 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 ...
随机推荐
- java对过反射调用方法
public class InvokeTester { public InvokeTester() { } String str; public InvokeTester(String str) ...
- Java开发者值得关注的7款新工具
云计算.大数据地快速发展催生了不少热门的应用及工具.作为老牌语言Java,其生态圈也出来了一些有关云服务.监控.文档分享方面的工具.本文总结了7款较新的Java工具,大家不妨看下. 1. JClari ...
- android 模拟器
参考:http://www.syscs.com/node/504 --skin WVGA800 - -no-boot-anim -wipe-: the dpi for the device you a ...
- 译:C#面向对象的基本概念 (Basic C# OOP Concept) 第二部分(封装,抽象,继承)
6.封装 封装就是对外部类隐藏成员或变量.我已经说过房子的保安仅仅被限制在房子的入口处,不需要知道屋内发生了什么.房主对保安隐藏了屋内所发生的任何事,以便更安全.隐藏和限制就被称为封装. 例如我们有两 ...
- 各种字符串Hash函数比较(转)
常用的字符串Hash函数还有ELFHash,APHash等等,都是十分简单有效的方法.这些函数使用位运算使得每一个字符都对最后的函数值产生影响.另外还有以MD5和SHA1为代表的杂凑函数,这些函数几乎 ...
- undefined reference to `_imp___ZN8QWebViewC1EP7QWidget'
add this line to your .pro file: QT += webkitwidgets
- Probabilistic Graphical Models
http://innopac.lib.tsinghua.edu.cn/search~S1*chx?/YProbabilistic+Graphical+Models&searchscope=1& ...
- Linux SSH 连接不上
http://blog.csdn.net/cryhelyxx/article/details/46473783 在xshell下用ssh登录远程主机centos出现以下问题: Connection e ...
- 各种Camera,总有一款适合你(二)
在实际的项目开发中,一般需要程序抽象出一些在几何意义上有明确意义的参数,这样方便策划或美术在自己的机器上进行调试. 下面是一个可变参的地下城摄像机的简单实现: // 第三人称摄像机,平移和旋转会同时进 ...
- [python]pip常用命令(转载)
用阿里云服务器,使用pip安装第三方库的时候卡的要死.所以我就想pip能不能安装本地的包. 找到了这篇博客: http://me.iblogc.com/2015/01/01/pip%E5%B8%B8% ...