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 ...
随机推荐
- 【原创】一起学C++ 之enum ---------C++ primer plus(第6版)
枚举 定义:在默认情况下讲整数值赋给枚举量,第一个枚举量的值为0,第二个枚举量的值为1,依次+1 一.定义一个枚举,枚举类型,枚举量 *与C#相比个人认为C++的enum不好一点是不能通过枚举名点其中 ...
- C#委托的语法
using System;using System.Collections.Generic;using System.Linq;using System.Text;using Delegate; na ...
- Linux安装包
关于SWT SWT首先要在Eclipse中添加SWT的安装包:Windowsbuilder Pro.下载路径:http://www.eclipse.org/windowbuilder/download ...
- poj 3250 Bad Hair Day (单调栈)
http://poj.org/problem?id=3250 Bad Hair Day Time Limit: 2000MS Memory Limit: 65536K Total Submissi ...
- How To Call Stored Procedure In Hibernate
How To Call Stored Procedure In Hibernate In this tutorial, you will learn how to call a store proce ...
- sass教程汇总
Sass @at-root http://www.w3cplus.com/preprocessor/Sass-3-3-new-feature-at-root-bem.html Sass中连体符(&am ...
- Python安装模块出错(ImportError: No module named setuptools)解决方法
原地址:http://www.cnblogs.com/BeginMan/archive/2013/05/28/3104928.html 在window平台下安装第三方模块时,出现这样的错误:
- 简单讨论数据类型(byte)强制转化后的数值变化规律
package com.wangzhu.datatype; /** * Java基本数据类型练习 * * @ClassName: DataTypes * @Description: TODO * @a ...
- easyui源码翻译1.32--Droppable(放置)
前言 使用$.fn.droppable.defaults重写默认值对象.下载该插件翻译源码 源码 /** * jQuery EasyUI 1.3.2 * *翻译:lbq --放置 拉伸 */ (fun ...
- LinuxShell_variable+if+while
[root@ossec-server mybash]# vim ./hello.sh #! /bin/sh # This is a example bash script echo "Hel ...