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 ...
随机推荐
- XtraBackup增量备份
mysql:5.6.29xtrabackup:2.2.10mysql数据目录:/data/mysqlmysql备份目录:/data/dbbak/ #确保有足够的磁盘空间 官方文档:https://ww ...
- Arch安装KDE5
plasma desktop Install the plasma-meta meta-package or the plasma group. Alternatively, for a more m ...
- [MFC美化] USkin使用详解-使用方法
该种皮肤库资料很少,用法与前面几种类似. 它主要有:USkin.dll ,USkin.lib,USkin.h和Sakura.msstyles这四个文件.皮肤格式是.u3.SkinBuilder是USk ...
- python 基础篇第一篇
本节内容 1.python介绍 2.发展史 3.python2和python3 4.安装 5.简单程序,hello world程序 6.变量 7.用户输入 8.模块初识 9..pyc是什么? 10.数 ...
- CodeForces 670A Holidays
简单题. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #inclu ...
- CodeForces 670D Magic Powder
二分. 二分一下答案,然后验证一下. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cst ...
- November 12th 2016 Week 46th Saturday
Never love anyone who treats you like you are ordinary. 请爱那些爱你的人. Don't waste your limited energy on ...
- VC6.0 通过崩溃地址中找到异常代码行
来源:http://blog.csdn.net/mydeardingxiaoli/article/details/20371585 这是从“VC编程经验总结7”中转出来的借花献佛——如何通过崩溃地址找 ...
- 利用GCD实现单利模式的宏代码
以下是.h文件,使用时,直接在需要实现单例模式的类中导入头文件即可. // .h文件 #define DenglSingletonH(name) + (instancetype)shared##nam ...
- sudo命令出错 must set be suid
特殊权限 4,2 ,1 4 suid,控制用户执行的文件,以文件所属用户的身份执行.当一个可执行的文件拥有suid时,会将文件拥有者的x位变成s(---s--x--x),这时称为set uid,简写 ...