POJ3080 - Blue Jeans(KMP+二分)
题目大意
求N个字符串的最长公共字串
题解
和POJ1226做法一样。。。注意是字典序最小的。。。WA了一次
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAXN 65
char p[MAXN],T[MAXN][MAXN];
int f[MAXN];
void getfail(char *p,int len)
{
int j;
f[0]=j=-1;
for(int i=1;i<len;i++)
{
while(j>=0&&p[j+1]!=p[i]) j=f[j];
if(p[j+1]==p[i]) j++;
f[i]=j;
}
}
bool find(char *s,int len,int n)
{
getfail(s,len);
for(int t=0;t<n;t++)
{
int j=-1,lens=strlen(T[t]);
bool flag=false;
for(int i=0;i<lens;i++)
{
while(j>=0&&s[j+1]!=T[t][i]) j=f[j];
if(s[j+1]==T[t][i]) j++;
if(j+1==len)
{
flag=true;
break;
}
}
if(!flag) return false;
}
return true;
}
int main()
{
int cases;
scanf("%d",&cases);
while(cases--)
{
int n;
scanf("%d",&n);
char s[MAXN],temp[MAXN];
temp[0]='\0';
for(int i=0;i<n;i++) scanf("%s",T[i]);
strcpy(s,T[0]);
int len=strlen(s);
for(int i=0;i<len;i++)
{
char ss[MAXN];
ss[0]='\0';
int l=i,r=len-1;
while(l<=r)
{
int mid=(l+r)>>1;
bool flag=find(s+i,mid-i+1,n);
if(flag)
{
l=mid+1;
strncpy(ss,s+i,mid-i+1);
ss[mid-i+1]='\0';
}
else r=mid-1;
}
if(strlen(ss)>strlen(temp)||(strlen(ss)==strlen(temp)&&strcmp(ss,temp)==-1)) strcpy(temp,ss);
}
if(strlen(temp)>=3)
printf("%s\n",temp);
else printf("no significant commonalities\n");
}
return 0;
}
POJ3080 - Blue Jeans(KMP+二分)的更多相关文章
- POJ3080 Blue Jeans —— 暴力枚举 + KMP / strstr()
题目链接:https://vjudge.net/problem/POJ-3080 Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total ...
- poj3080 Blue Jeans【KMP】【暴力】
Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total Submissions:21746 Accepted: 9653 Descri ...
- POJ3080——Blue Jeans(暴力+字符串匹配)
Blue Jeans DescriptionThe Genographic Project is a research partnership between IBM and The National ...
- poj3080 Blue Jeans(暴枚+kmp)
Description The Genographic Project is a research partnership between IBM and The National Geographi ...
- kuangbin专题十六 KMP&&扩展KMP POJ3080 Blue Jeans
The Genographic Project is a research partnership between IBM and The National Geographic Society th ...
- POJ3080 Blue Jeans 题解 KMP算法
题目链接:http://poj.org/problem?id=3080 题目大意:给你N个长度为60的字符串(N<=10),求他们的最长公共子串(长度>=3). 题目分析:KMP字符串匹配 ...
- POJ3080 Blue Jeans
题目链接. 题目大意: 给定n个字符串,找出最长相同且长度大于3的子串,如果存在多个,找出字典序最小的. 分析: 直接枚举(暴搜). 对于s[0]的每一个子串,判断是否在其它n-1个字符串中都存在. ...
- (字符串 KMP)Blue Jeans -- POJ -- 3080:
链接: http://poj.org/problem?id=3080 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88230#probl ...
- POJ 3080 Blue Jeans 找最长公共子串(暴力模拟+KMP匹配)
Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20966 Accepted: 9279 Descr ...
随机推荐
- LocalContainerEntityManagerFactoryBean
http://doc.okbase.net/liuyitian/archive/109276.html
- objective-c常用数学方法
1. 三角函数 double sin (double);正弦 double cos (double);余弦 double tan (double);正切 2 .反三角函数 double as ...
- vs git .ignore
## Ignore Visual Studio temporary files, build results, and## files generated by popular Visual Stud ...
- hadoop 存储空间满了
-- ::, WARN mapred.LocalJobRunner - job_local_0001 org.apache.hadoop.util.DiskChecker$DiskErrorExcep ...
- Javascript编程模式(JavaScript Programming Patterns)Part 2.(高级篇)
模块编程模式的启示(Revealing Module Pattern) 客户端对象(Custom Objects) 懒函数定义(Lazy Function Definition) Christian ...
- Cocos2d-x 3.2编译Android程序错误的解决方案
最近的升级Cocos2d-x 3.2正式版.iOS不管是什么程序编译问题,使用结果cocos compile -p android编译APK计划.结果悲剧,出现以下错误. Android NDK: I ...
- 快速排序法QuickSort
/** * * @author Administrator * 功能:交换式排序之快速排序 */ package com.test1; import java.util.Calendar; publi ...
- Google的代码风格规范,各种语言都很全
https://code.google.com/p/google-styleguide/
- OLEDB和ODBC的区别(优缺点)
ODBC是一种连接数据库的开放标准,OLEDB(对象链接和嵌入数据库)位于ODBC层与应用程序之间. 在你的ASP页面里,ADO是位于OLEDB之上的应用程序. 你的ADO调用先被送到OLEDB,然后 ...
- 【HDOJ】1285 确定比赛名次
拓扑排序,居然要考虑重边,使用STL实现. #include <iostream> #include <cstdio> #include <cstring> #in ...