【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 ...
随机推荐
- Python Base HTTP Server
import BaseHTTPServer import cgi, random, sys MESSAGES = [ "That's as maybe, it's still a frog. ...
- SAP GUI SAPLOGON.INI
GUI是SAP系统最常用的客户端,在一台客户机上,利用GUI可以连接多套SAP系统(连接方法参见<客户端连接配置(SAP GUI 710)>),也可以设置多个快捷方式登录(参见<用快 ...
- C# WinForm程序打印条码 Code39码1
做WinForm程序需要打印条码,为了偷懒不想自己写生成条码的程序在网上下载一个标准的39码的字体,在程序里面换上这个条码字体即可打印条码了. 最重要的一点作为记录: 如果想把“123456789”转 ...
- bit操作 转
http://www.catonmat.net/blog/low-level-bit-hacks-you-absolutely-must-know/ Bit Hack #6. Turn off the ...
- SQLServer 获得所有表结构(包括表名及字段)
then d.name else null end) 表名, a.colorder 字段序号,a.name 字段名, ( then '√'else '' end) 标识, (case when (SE ...
- 使用Highcharts实现图表展示
本篇随笔记录的是本人2011年做广州地铁协同办公项目时,图表需求的解决方案.(Demo中只是虚拟的测试数据) 关键技术点: 使用Highcharts实现图表展示: 另外使用Highslide弹窗.使用 ...
- Using the Cordova Camera API
使用ionic开发一款android或ios应用,估计少不了使用到Camera API,这里记录下使用过程. 创建空的ionic应用 ionic start myTabs tabs 通过cd demo ...
- 如何生成ipa文件
xcode--Product--Archive 弹出Organizer-Archives选择Distribute---Save for EnterPrise or Ad_Hoc Deployment- ...
- SmartJS 第一期(0.1)发布 - AOP三剑客
隔了好久才终于又发布了一点东西,SmartJS是最近才开始搞的一个开源js库,目的是做一些比较有特点的事情(smartjs暂时也是依赖于jquery). SmartJS的内容规划比较多,也无法在短时间 ...
- Linux 时钟与计时器
对 Linux 系统来说,时钟和计时器是两个十分重要的概念.时钟反应的是绝对时间,也可认为是实时时间.计时器反应的则是相对时间,即相对于系统启动后的计时.操作系统内核需要管理运行时间(uptime)和 ...