【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 ...
随机推荐
- [原]cocos2d-lua 常用法汇总
1.CCEditBox local back = CCScale9Sprite:create("res/ui/images/im_02.png", CCRect(20, 20, 1 ...
- iOS开发问题之Could not instantiate class named NSLayoutConstraint
这个问题在ios6.0之前的版本中运行会出现,因为使用Xcode 4.5之后的版本新建项目默认是选中AutoLayout的,但这个特性是在iOS6.0之后的版本中才支持的. 解决办法是选中.stroy ...
- MassTransit RabbitMQ 参考文档
Autofac http://docs.autofac.org/en/latest/lifetime/startup.html RabbitMQ http://www.rabbitmq.com/dot ...
- JAVA 汇编语言查看
http://blog.csdn.net/bingduanlbd/article/details/8524300 http://hllvm.group.iteye.com/group/topic/34 ...
- UDP网络通信OSC 协议
使用方法 ofxOscMessage mesg; mesg.setAddress("m"); mesg.addIntArg(); mesg.addIntArg(); mesg.ad ...
- NFC 与 Windows Phone 的那点事儿
说起NFC这个词儿应该已经不陌生了,在我们的生活中有很多使用场景都是使用的这项技术,例如公交卡,门禁,还有银联的闪付卡等等.并且近些年在移动设备上使用的场景也越来越多,例如 对 NFC TAG 的读写 ...
- POJ 2096 Collecting Bugs
Collecting Bugs Time Limit: 10000MS Memory Limit: 64000K Total Submissions: 1716 Accepted: 783 C ...
- Investigating Your RAM Usage
转载自:http://developer.android.com/intl/zh-cn/tools/debugging/debugging-memory.html Because Android is ...
- js apply/call/caller/callee/bind使用方法与区别分析
一.call 方法 调用一个对象的一个方法,以另一个对象替换当前对象(其实就是更改对象的内部指针,即改变对象的this指向的内容). Js代码 call([thisObj[,arg1[, arg2[, ...
- 一个简单的 ASP.NET MVC 例子演示如何在 Knockout JS 的配合下,使用 TypeScript 。
前言 TypeScript 是一种由微软开发的自由和开源的编程语言.它是JavaScript的一个超集,而且本质上向这个语言添加了可选的静态类型和基于类的面向对象编程.安德斯·海尔斯伯格,C#的首席架 ...