题目链接: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. bzoj 4815 小Q的表格 —— 反演+分块

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4815 思路就和这里一样:https://blog.csdn.net/leolyun/arti ...

  2. Linux系统中‘dmesg’命令处理故障和收集系统信息的7种用法

    转自:https://linux.cn/article-3587-1.html 'dmesg'命令显示linux内核的环形缓冲区信息,我们可以从中获得诸如系统架构.cpu.挂载的硬件,RAM等多个运行 ...

  3. 关于javaScript事件委托的那些事

    今天是第一次写稿,还是有那么一丢丢小鸡冻...回归正题啦... 关于javaScript事件委托不得不说的那些事,为什么要使用事件委托? 我们可以这么说,假设老板要分配一项任务,首先要秘书叫A君来到办 ...

  4. 如何从光盘本地安装CentOS 7图形界面(Gnome GUI)

    本例中通过在CentOS 7中修改repo文件,直接从光盘或者ISO镜像文件安装Gnome图形界面(Gnome GUI),从而避免耗时从官网或镜像下载. 1.首先确保光盘或者ISO镜像文件正确连接到客 ...

  5. windows、Linux 开放端口

    一.Linux开放端口: 1. CentOS7.x/RedHat7.x  , 参考 CentOS7使用firewalld打开关闭防火墙与端口 1.firewalld的基本使用 启动: systemct ...

  6. 树莓派 Learning 001 装机 ---之 1 安装NOOBS系统

    树莓派安装NOOBS系统 (使用的树莓派板卡型号:Raspberry Pi 2 Model B V1.1)(板卡的型号在板子正面的丝印层上印着,你可以看到.) RASPBERRY PI 2 MODEL ...

  7. LINQ中的陷阱--TakeWhile&SkipWhile

    在用TakeWhile,SkipWhile设置陷阱之前,我们先来看一看他们的兄弟Take和Skip: public static IEnumerable<T> Take<T>( ...

  8. hdu1059

    #include <stdio.h> #include <string.h> #define MAXN 120005 int main() { int num[7]; int ...

  9. IQA(图像质量评估)

    图像质量评价(Image Quality Assessment,IQA)是图像处理中的基本技术之一,主要通过对图像进行特性分析研究,然后评估出图像优劣(图像失真程度). 主要的目的是使用合适的评价指标 ...

  10. java第一天--Java开发环境的搭建以及使用eclipse从头一步步创建java项目

    一.java 开发环境的搭建 这里主要说的是在windows 环境下怎么配置环境. 1.首先安装JDK java的sdk简称JDK ,去其官方网站下载最近的JDK即可..http://www.orac ...