【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 ...
随机推荐
- c#如何读取相机手机的拍摄时间
/// 获中的照片拍摄日期 /// </summary> /// <param name="fileName">文件名</param> /// ...
- [原创]Android系统中常用JAVA类源码浅析之HashMap
由于是浅析,所以我只分析常用的接口,注意是Android系统中的JAVA类,可能和JDK的源码有区别. 首先从构造函数开始, /** * Min capacity (other than zero) ...
- apk签名
参考:http://stackoverflow.com/questions/13578134/how-to-automate-keystore-generation-using-the-java-ke ...
- 利用decorator和descriptor进行数据缓存
class cached_property(object): def __init__(self, func, name=None, doc=None): self.__name__ = name o ...
- textView字体颜色根据不同状态显示不同颜色
XML file saved at res/color/button_text.xml: <?xml version="1.0" encoding="utf-8&q ...
- 【转】Oracle RAC 环境下的连接管理
文章转自:http://www.oracle.com/technetwork/cn/articles/database-performance/oracle-rac-connection-mgmt-1 ...
- Failure is not fatal, but failure to change might be.
Failure is not fatal, but failure to change might be. 失败不是致命的,但无法改变却可能是致命的.
- STL中stack小结
(1)为了运用stack,你必须包含头文件<stack>:#include<stack> (2)在头文件中stack定义如下: namespace std{ template ...
- Web Fram 2 for IIS7.X(Microsoft Web Farm Framework)
Microsoft Web Farm Framework (WFF) 2.0 是微软开发的.基于IIS 7.x的小插件,能够帮助我们轻松实现Web网站的高性能.高可用性,用来在Web服务器群上提供和管 ...
- Understanding CMS GC Logs--转载
原文地址:https://blogs.oracle.com/poonam/entry/understanding_cms_gc_logs Understanding CMS GC Logs By Po ...