hdu_2328_Corporate Identity(暴力枚举子串+KMP)
题目链接:hdu_2328_Corporate Identity
题意:
给你n个串,让你找这n个串的最大公共子串
题解:
串比较小,暴力枚举第一个的子串,然后KMP判断是否可行
#include<cstdio>
#include<cstring>
#define F(i,a,b) for(int i=a;i<=b;i++) const int N=;
int nxt[N],n,lens[],ans,l,r,cnt;
char dt[][N]; int KMP(int n,char*a,int m,char*b){
int i,j;
for(nxt[]=j=-,i=;i<n;nxt[i++]=j){
while(~j&&a[j+]!=a[i])j=nxt[j];
if(a[j+]==a[i])j++;
}
for(j=-,i=;i<m;i++){
while(~j&&a[j+]!=b[i])j=nxt[j];
if(a[j+]==b[i])j++;
if(j==n-)return ;
}
return ;
} inline void up(int &a,int b){if(a<b)a=b;}
inline void update(int L,int R){
if(R-L+>ans)l=L,r=R,ans=r-l+;
else if(R-L+==ans)
{
for(int i=l,j=L;;i++,j++){
if(dt[][i]!=dt[][j]){
if(dt[][i]<dt[][j])return;
else{
l=L,r=R;
return;
}
}
if(i==r)return;
if(j==R)l=L,r=R;
}
}
} int main(){
while(~scanf("%d",&n),n)
{
F(i,,n)scanf("%s",dt[i]),lens[i]=strlen(dt[i]);
char tp[];
ans=-,l=,r=,cnt=;
F(i,,lens[]-)F(j,i+cnt,lens[]-)
{ int ed=,fg=;
F(ii,i,j)tp[ed++]=dt[][ii];
F(ii,,n)if(KMP(j-i+,tp,lens[ii],dt[ii])==){fg=;break;}
if(fg)update(i,j),cnt=ans<?:ans-;//剪枝,每次只找大于ans的
}
if(ans==-)puts("IDENTITY LOST");
else {F(i,l,r)printf("%c",dt[][i]);puts("");}
}
return ;
}
hdu_2328_Corporate Identity(暴力枚举子串+KMP)的更多相关文章
- poj 3080 Blue Jeans (暴力枚举子串+kmp)
Description The Genographic Project is a research partnership between IBM and The National Geographi ...
- 字符串截取模板 && POJ 3450、3080 ( 暴力枚举子串 && KMP匹配 )
//截取字符串 ch 的 st~en 这一段子串返回子串的首地址 //注意用完需要根据需要最后free()掉 char* substring(char* ch,int st,int en) { ; c ...
- hdu-2328(暴力枚举+kmp)
题意:给你n个字符串,问你这n个串的最长公共子串 解题思路:暴力枚举任意一个字符串的所有子串,然后暴力匹配,和hdu1238差不多的思路吧,这里用string解决的: 代码: #include< ...
- hdu2328 Corporate Identity【string库使用】【暴力】【KMP】
Corporate Identity Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- POJ 3080 Blue Jeans (字符串处理暴力枚举)
Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21078 Accepted: ...
- CodeForces 742B Arpa’s obvious problem and Mehrdad’s terrible solution (暴力枚举)
题意:求定 n 个数,求有多少对数满足,ai^bi = x. 析:暴力枚举就行,n的复杂度. 代码如下: #pragma comment(linker, "/STACK:1024000000 ...
- 2014牡丹江网络赛ZOJPretty Poem(暴力枚举)
/* 将给定的一个字符串分解成ABABA 或者 ABABCAB的形式! 思路:暴力枚举A, B, C串! */ 1 #include<iostream> #include<cstri ...
- HNU 12886 Cracking the Safe(暴力枚举)
题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12886&courseid=274 解题报告:输入4个数 ...
- 51nod 1116 K进制下的大数 (暴力枚举)
题目链接 题意:中文题. 题解:暴力枚举. #include <iostream> #include <cstring> using namespace std; ; ; ch ...
随机推荐
- private static
static: 静态成员,不能实例化,在你运行的时候他自己在内存中开辟了块空间,不用new, 有点像全局变量 private static 和 public static 都是静态变量,在类加载时 ...
- JSTL标签库--核心标签库
->JSTL的使用和EL表达式是分不开的 ->JSTL标签库分为5类 1.核心标签库(这里只介绍该标签库) 2.I18N格式化标签库 3.SQL标签库 4.XML标签库 5.函数标签库 - ...
- TCP connect EADDRNOTAVAIL(99)错误原因分析
转自:http://blog.chinaunix.net/uid-20662820-id-3371081.html 关于TCP connect 返回错误99,可以能大家都会遇到,这里就分析一下这个错误 ...
- assert的基本用法
assertion(断言)在软件开发中是一种常用的调试方式,很多开发语言中都支持这种机制,如C,C++和Eiffel等,但是支持的形式不尽相同,有的是通过语言本身.有的是通过库函数等.另外,从理论上来 ...
- 十分钟学会 tmux
tmux 是一款终端复用命令行工具,一般用于 Terminal 的窗口管理.在 macOS 下,使用 iTerm2 能应付绝大多数窗口管理的需求. 如上图所示,iTerm2 能新建多个标签页(快捷键 ...
- cursor属性
cursor光标类型 auto default none context-menu help pointer progress wait cell crosshair text vertical-te ...
- Ubuntu14.04浏览器Firefox安装flash播放插件
sudo apt-get update sudo apt-get install flashplugin-nonfree ================= 类飞秋软件 sudo apt-get in ...
- Two Pointers - leetcode [两指针问题]
125. Valid Palindrome consider only alphanumeric characters and ignore cases. transform(s.begin(), s ...
- <hdu - 1280> 前M大的数 (注意其中的细节)
这是杭电hdu上的链接http://acm.hdu.edu.cn/showproblem.php?pid=1280 Problem Description: 还记得Gardon给小希布置的那个作业么 ...
- ECOS-LNMP ZendGuard
因为ECOS产品加密后依赖PHP Zendguard运行(基于PHP5.3加密) 备注: Shopex485商城基于php5.2进行加密 URL:http://www.zend.com/en/pro ...