地址:http://acm.hdu.edu.cn/showproblem.php?pid=2328

题目:

Corporate Identity

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1599    Accepted Submission(s): 614

Problem Description
Beside other services, ACM helps companies to clearly state their “corporate identity”, which includes company logo but also other signs, like trademarks. One of such companies is Internet Building Masters (IBM), which has recently asked ACM for a help with their new identity. IBM do not want to change their existing logos and trademarks completely, because their customers are used to the old ones. Therefore, ACM will only change existing trademarks instead of creating new ones.

After several other proposals, it was decided to take all existing trademarks and find the longest common sequence of letters that is contained in all of them. This sequence will be graphically emphasized to form a new logo. Then, the old trademarks may still be used while showing the new identity.

Your task is to find such a sequence.

 
Input
The input contains several tasks. Each task begins with a line containing a positive integer N, the number of trademarks (2 ≤ N ≤ 4000). The number is followed by N lines, each containing one trademark. Trademarks will be composed only from lowercase letters, the length of each trademark will be at least 1 and at most 200 characters.

After the last trademark, the next task begins. The last task is followed by a line containing zero.

 
Output
For each task, output a single line containing the longest string contained as a substring in all trademarks. If there are several strings of the same length, print the one that is lexicographically smallest. If there is no such non-empty string, output the words “IDENTITY LOST” instead.
 
Sample Input
3
aabbaabb
abbababb
bbbbbabb
2
xyz
abc
0
 
Sample Output
abb
IDENTITY LOST
 
Source
 
Recommend
teddy
 

思路:kmp+暴力枚举

 #include <cstdio>
#include <cstring>
#include <iostream> using namespace std; #define MP make_pair
#define PB push_back
typedef long long LL;
const double eps=1e-;
const int K=1e6+;
const int mod=1e9+; int nt[K];
char sa[][],sb[];
void kmp_next(char *T,int *next)
{
next[]=;
for(int i=,j=,len=strlen(T);i<len;i++)
{
while(j&&T[i]!=T[j]) j=next[j-];
if(T[i]==T[j]) j++;
next[i]=j;
}
}
int kmp(char *S,char *T,int *next)
{
int ans=;
int ls=strlen(S),lt=strlen(T);
for(int i=,j=;i<ls;i++)
{
while(j&&S[i]!=T[j]) j=next[j-];
if(S[i]==T[j]) j++;
if(j==lt) ans++;
}
return ans;
}
int cmp(char *sb,int si,int st,int len)
{
for(int i=;i<len;i++)
if(sb[si+i]<sb[st+i])
return -;
else if(sb[si+i]>sb[st+i])
return ;
return ;
}
int main(void)
{
int t,n;
while(scanf("%d",&n)&&n)
{
for(int i=;i<=n;i++)
scanf("%s",sa[i]);
int len,st,se;
len=strlen(sa[]);
st=,se=-;
for(int i=;i<len;i++)
{
for(int j=i;j<len;j++)
{
sb[j-i]=sa[][j],sb[j-i+]='\0';
int ff=;
kmp_next(sb,nt);
for(int k=;k<=n&&ff;k++)
if(!kmp(sa[k],sb,nt))
ff=;
if(ff&&j-i>se-st)
st=i,se=j;
else if(ff&&j-i==se-st&&cmp(sa[],i,st,j-i+)<)
st=i,se=j;
}
}
if(se-st+<)
printf("IDENTITY LOST\n");
else
{
for(int i=st;i<=se;i++)
printf("%c",sa[][i]);
printf("\n");
} }
return ;
}

hdu2328 Corporate Identity的更多相关文章

  1. hdu2328 Corporate Identity【string库使用】【暴力】【KMP】

    Corporate Identity Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  2. hdu2328 Corporate Identity 扩展KMP

    Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...

  3. kuangbin专题十六 KMP&&扩展KMP HDU2328 Corporate Identity

    Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...

  4. [HDU2328]Corporate Identity(后缀数组)

    传送门 求 n 个串的字典序最小的最长公共子串. 和 2 个串的处理方法差不多. 把 n 个串拼接在一起,中间连上一个没有出现过的字符防止匹配过界. 求出 height 数组后二分公共子串长度给后缀数 ...

  5. POJ-3450 Corporate Identity (KMP+后缀数组)

    Description Beside other services, ACM helps companies to clearly state their “corporate identity”, ...

  6. hdu 2328 Corporate Identity(kmp)

    Problem Description Beside other services, ACM helps companies to clearly state their “corporate ide ...

  7. (KMP 暴力)Corporate Identity -- hdu -- 2328

    http://acm.hdu.edu.cn/showproblem.php?pid=2328 Corporate Identity Time Limit: 9000/3000 MS (Java/Oth ...

  8. POJ3450 Corporate Identity 【后缀数组】

    Corporate Identity Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 7662   Accepted: 264 ...

  9. POJ3450 Corporate Identity —— 后缀数组 最长公共子序列

    题目链接:https://vjudge.net/problem/POJ-3450 Corporate Identity Time Limit: 3000MS   Memory Limit: 65536 ...

随机推荐

  1. 【DeepLearning】一些资料

    记录下,有空研究. http://nlp.stanford.edu/projects/DeepLearningInNaturalLanguageProcessing.shtml http://nlp. ...

  2. hdu 2686(状压dp)

    题目链接:http://poj.org/problem?id=2686 思路:典型的状压dp题,dp[s][v]表示到达剩下的车票集合为S并且现在在城市v的状态所需要的最小的花费. #include& ...

  3. STL 源代码剖析 算法 stl_algo.h -- inplace_merge

    本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie inplace_merge(应用于有序区间) ----------------------- ...

  4. Hibernate_day02--Hibernate的一级缓存

    Hibernate的一级缓存 什么是缓存 1 数据存到数据库里面,数据库本身是文件系统,使用流方式操作文件效率不是很高. (1)把数据存到内存里面,不需要使用流方式,可以直接读取内存中数据 (2)把数 ...

  5. 写一个SingleTon,(饿最终、懒同步)

    1.饿汉式: public class SingleTon { private SingleTon(){ } private final static SingleTon instance = new ...

  6. nmon工具

    下载地址 1.http://nmon.sourceforge.net/pmwiki.php?n=Site.Download tar xvzf  *.tar.gz,需要配置权限 chmod -x,同时配 ...

  7. IOS 十位数0补齐

    NSCalendar *calendar = [NSCalendar currentCalendar]; unsigned unitFlags = NSYearCalendarUnit | NSMon ...

  8. SpringMVC笔记——Spring+MyBatis组合开发简单实例

    简介 SSH框架很强大,适合大型项目开发.但学无止境,多学会一门框架组合开发会让自己增值许多. SSM框架小巧精致,适合中小型项目快速开发,对于新手来说也是简单上手的.在SSM框架搭建之前,我们先学习 ...

  9. 事件处理程序中 this 的指向

    js 高程 事件处理程序 章节原话(斜体表示): 13.2.1 HTML事件处理程序 在这个函数内部,this 值等于事件的目标元素,例如: <!-- 输出 "Click Me&quo ...

  10. 160525、高并发之mysql主从复制(linux)

    第一步:新建两台linux主机(我这里是使用虚拟机,linux版本是CentOS-6.3-x86_64-bin-DVD1.iso,注意:我下载的是dvd1,如果其他版本默认安装可能会自动还原) 第二步 ...