题目链接:http://poj.org/problem?id=3450

Corporate Identity
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 8549   Accepted: 2856

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

 
题目大意:输入n,代表有n个串,问你m个串中最长公共子串是什么,如果有长度相同的,输出字典序最小的
思路:也算是KMP的裸题了,但是注意一下这题要有一个优化操作,找出长度最短的串,以这个串为基础寻找是否有子串,还有值得一提的是:那个C++加速的东西好像没有一点用,一直超时,改成c的
输入输出才过了,就因为这个卡了好几天,难受。。。
看代码1:

#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=;
const int maxn=4e3+;
const int maxk=5e3+;
const int maxx=1e4+;
const ll maxe=+;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
char a[maxn][];
int next[maxn];
void cal_next(char s[])
{
int len=strlen(s);
int k=-;
next[]=-;
for(int i=;i<len;i++)
{
while(k>-&&s[k+]!=s[i])
{
k=next[k];
}
if(s[k+]==s[i]) k++;
next[i]=k;
}
}
bool kmp(char x[],char y[])
{
int k=-;
int len1=strlen(x);
int len2=strlen(y);
for(int i=;i<len1;i++)
{
while(k>-&&y[k+]!=x[i])
{
k=next[k];
}
if(x[i]==y[k+]) k++;
if(k==len2-) return true;
}
return false;
}
int main()
{
//ios::sync_with_stdio(false);
int n,flag=;
while(scanf("%d",&n)!=EOF)
{
getchar();
if(n==) break;
char ans[]="",temp[];
scanf("%s",a[]);
getchar();
strcpy(temp,a[]);//这里不用自己加'\0',因为a[0]本身就有'\0'的,直接复制过去了
for(int i=;i<n;i++)
{
scanf("%s",a[i]);
getchar();
if(strlen(a[i])<strlen(temp))
{
strcpy(temp,a[i]);
}
}
int len=strlen(temp);
for(int i=;i<len;i++)//起点
{
for(int j=;i+j<=len;j++)//长度
{
char op[];
strncpy(op,temp+i,j);//不会在结尾给你自动加'\0'
op[j]='\0';//注意这里手动加一个'\0',因为你只是得到了那几个字符,并没有得到'\0'
cal_next(op);
for(int k=;k<n;k++)
{
flag=;
if(!kmp(a[k],op))
{
flag=;
break;
}
}
if(flag==)//有一个找不到就不用遍历以该起点长度更大的子串了,因为肯定也会找不到
break;
else
{
if(strlen(op)>strlen(ans)) strcpy(ans,op);
else if(strlen(op)==strlen(ans)&&strcmp(ans,op)>)
{
strcpy(ans,op);
}
}
}
}
if(strlen(ans)==)
printf("IDENTITY LOST\n");
else
printf("%s\n",ans);
}
return ;
}

看代码2:

#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=;
const int maxn=5e3+;
const int maxk=5e3+;
const int maxx=1e4+;
const ll maxe=+;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
char a[maxn][];
int next[maxn];
void cal_next(char op[])
{
int k=-;
next[]=-;
int len=strlen(op);
for(int i=;i<len;i++)
{
while(k>-&&op[k+]!=op[i])
{
k=next[k];
}
if(op[k+]==op[i]) k++;
next[i]=k;
}
return ;
}
bool kmp(char x[],char y[])
{
int k=-;
int len1=strlen(x);
int len2=strlen(y);
for(int i=;i<len2;i++)
{
while(k>-&&x[k+]!=y[i])
{
k=next[k];
}
if(x[k+]==y[i]) k++;
if(k==len1-) return true;
}
return false;
}
int main()
{
int n,flag;
char b[],temp[];
while(scanf("%d",&n)!=EOF)
{
int minn=;
if(n==) break;
getchar();
char ans[]="";
for(int i=;i<n;i++)
{
scanf("%s",a[i]);
int len=strlen(a[i]);
if(len<minn)
{
minn=len;
strcpy(b,a[i]);
}
getchar();
}
for(int i=;i<=minn;i++)//长度
{
for(int j=;j+i<=minn;j++)//起点
{
flag=;
strncpy(temp,b+j,i);
temp[i]='\0'; cal_next(temp);
for(int k=;k<n;k++)
{
if(!kmp(temp,a[k]))
{
flag=;
break;
}
}
if(flag==)
{
if(strlen(temp)>strlen(ans)) strcpy(ans,temp);
else if(strlen(temp)==strlen(ans)&&strcmp(ans,temp)>) strcpy(ans,temp);
}
}
}
if(strlen(ans)==) printf("IDENTITY LOST\n");
else
{
printf("%s\n",ans);
}
}
}

POJ - 3450的更多相关文章

  1. 字符串截取模板 && POJ 3450、3080 ( 暴力枚举子串 && KMP匹配 )

    //截取字符串 ch 的 st~en 这一段子串返回子串的首地址 //注意用完需要根据需要最后free()掉 char* substring(char* ch,int st,int en) { ; c ...

  2. POJ - 3294~Relevant Phrases of Annihilation SPOJ - PHRASES~Substrings POJ - 1226~POJ - 3450 ~ POJ - 3080 (后缀数组求解多个串的公共字串问题)

    多个字符串的相关问题 这类问题的一个常用做法是,先将所有的字符串连接起来, 然后求后缀数组 和 height 数组,再利用 height 数组进行求解. 这中间可能需要二分答案. POJ - 3294 ...

  3. POJ 3450 后缀数组/KMP

    题目链接:http://poj.org/problem?id=3450 题意:给定n个字符串,求n个字符串的最长公共子串,无解输出IDENTITY LOST,否则最长的公共子串.有多组解时输出字典序最 ...

  4. POJ 3450 Corporate Identity(KMP)

    [题目链接] http://poj.org/problem?id=3450 [题目大意] 求k个字符串的最长公共子串,如果有多个答案,则输出字典序最小的. [题解] 我们对第一个串的每一个后缀和其余所 ...

  5. HDU 2328 POJ 3450 KMP

    题目链接:  HDU http://acm.hdu.edu.cn/showproblem.php?pid=2328 POJhttp://poj.org/problem?id=3450 #include ...

  6. poj 3450 Corporate Identity

    题目链接:http://poj.org/problem?id=3450 题目分类:后缀数组 题意:求n个串的最长公共字串(输出字串) //#include<bits/stdc++.h> # ...

  7. POJ 3450 Corporate Identity (KMP,求公共子串,方法很妙)

    http://blog.sina.com.cn/s/blog_74e20d8901010pwp.html我采用的是方法三. 注意:当长度相同时,取字典序最小的. #include <iostre ...

  8. POJ 3450 Corporate Identity kmp+最长公共子串

    枚举长度最短的字符串的所有子串,再与其他串匹配. #include<cstdio> #include<cstring> #include<algorithm> #i ...

  9. POJ 3450 Corporate Identity (KMP+暴搞)

    题意: 给定N个字符串,寻找最长的公共字串,如果长度相同,则输出字典序最小的那个. 找其中一个字符串,枚举它的所有的字串,然后,逐个kmp比较.......相当暴力,可二分优化. #include & ...

随机推荐

  1. Send Code to evernote by my specify notebook

    #coding:utf-8 import sys sys.path.append("lib") import thrift.protocol.TBinaryProtocol as ...

  2. poj 1208 Web Navigation(堆栈操作)

    一.Description Standard web browsers contain features to move backward and forward among the pages re ...

  3. oracle 实现 自增主键功能

    转自:https://blog.csdn.net/zxh2075/article/details/78488141 之前有一项工作是将mysql的数据库实现转移到oracle,遇到了自增主键实现的问题 ...

  4. qextserialport打不开com10及以上的串口

    需要在portname前添加\\\\.\\这样就可以了!! 例如 QString portname; portname.append("\\\\.\\").append(ui-&g ...

  5. Linux UDP通信例子

    UDP C/S编程的步骤如下图所示 ////server.c #include <sys/types.h> #include <sys/socket.h> #include&l ...

  6. 大数据学习路线copy自淘宝

    一.hadoop视频学习(入门到精通) 二.数据挖掘(入门到精通) 三.Hadoop学习路线 1.开发前期准备 首先,如果你没有Java和Linux基础,建议你先简单学一下这两门课程,此宝贝里面都为你 ...

  7. layui table中使用checkbox

    第一步: 1. <div class="layui-form"> <table class="layui-hide" lay-filter=& ...

  8. ubuntu上runsv/runit小记

    一个偶然的原因,在研究git使用时,发现有个自动启动的git-daemon进程: wellbye@AY130622174524343529Z:~$ ps aux|grep git root ? Ss ...

  9. Jenkins配置有用摘抄笔记

    使用jenkins配置.net mvc5网站自动构建全过程记录  转自:http://www.cnblogs.com/baiyunchen/p/4724350.html 持续集成是个简单重复劳动,人来 ...

  10. Code Page Identifiers - Copy from Microsoft

    Code Page Identifiers 78 out of 94 rated this helpful - Rate this topic   The following table define ...