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 ...
随机推荐
- poj 2104 K-th Number 划分树,主席树讲解
K-th Number Input The first line of the input file contains n --- the size of the array, and m --- t ...
- poj 1113 Mall
Mall 水题:注意题目上面有一个至少离城堡的距离为L,其实思考一下就知道是指离凸包(凸多边形)的距离为L,这时很容易知道外围的圆的圆心角叠加之后就是一个整圆:和poj2187一样使用graham形成 ...
- Lucene基础(三)-- 中文分词及高亮显示
Lucene分词器及高亮 分词器 在lucene中我们按照分词方式把文档进行索引,不同的分词器索引的效果不太一样,之前的例子使用的都是标准分词器,对于英文的效果很好,但是中文分词效果就不怎么样,他会按 ...
- MVC5 ModelBinder
MVC5 ModelBinder 什么是ModelBinding ASP.NET MVC中,所有的请求最终都会到达某个Controller中的某个Action并由该Action负责具体的处理和响应.为 ...
- 简单的map转换成Bean的工具
简单的map转换成Bean的工具 package com.sd.microMsg.util; import java.lang.reflect.Field; import java.lang.refl ...
- 1013: [JSOI2008]球形空间产生器sphere
很直观的一个gauss题: 用的是以前用过的一个模板: #include<cstdio> #include<algorithm> #include<cmath> # ...
- 2D多边形碰撞器优化器
http://www.unity蛮牛.com/thread-19827-1-1.html http://pan.baidu.com/s/1qW2mWS8 Asset Store Link: http: ...
- http://blog.csdn.net/shirdrn/article/details/6270506
http://blog.csdn.net/shirdrn/article/details/6270506
- [二分匹配]URAL1721Two Sides of the Same Coin
题意:给n个人,每个人都有3个参数,分别是名字,能做的事(a:statements b:testdate a.b都可以:anything),Rank 要求:一个人只能做一个事件,要两个人Rank相 ...
- easyui源码翻译1.32--Menu(菜单)
前言 使用$.fn.menu.defaults重写默认值对象.下载该插件翻译源码 菜单组件通常用于快捷菜单.他是构建其他菜单组件的必备基础组件.比如:menubutton和splitbutton.它还 ...