Corporate Identity
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 5696   Accepted: 2075

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

思路:枚举一个串的所有子串。推断该子串有没有在其他串中出现。若在其他串中所有出现 则更新子串,否则枚举下一个子串。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char str[4010][210];
int next[210]; void getnext(char *s1){
int j = 0, k = -1;
int len = strlen(s1);
next[0] = -1;
while(j < len){
if(k == -1 || s1[j] == s1[k]){
++j;
++k;
next[j] = k;
}
else k = next[k];
}
return ;
} bool kmp(char *s1, char *s2){
int len1 = strlen(s2);
int len2 = strlen(s1);
getnext(s1);
int i = 0, j = 0;
while(i < len1){
if(j == -1 || s1[j] == s2[i]){
++i;
++j;
}
else j=next[j];
if(j == len2)
return true;
}
return false;
} int main(){
int n;
while(scanf("%d", &n), n){
for(int i = 0 ; i < n; ++i)
scanf("%s", str[i]);
char temp[220];
char sum[220] = "";
int len = strlen(str[0]);
for(int i = 0; i < len; ++i){
int ans = 0;
for(int j = i; j < len; ++j){
temp[ans++] = str[0][j];
temp[ans] = '\0';
int flag = 1;
for(int k = 0 ; k < n; ++k){
if(!kmp(temp, str[k])){
flag = 0;
break;
}
}
if(flag){
if(strlen(temp) > strlen(sum))
memcpy(sum, temp, sizeof(temp));
else if(strlen(temp) == strlen(sum) && strcmp(temp, sum) < 0)
memcpy(sum, temp, sizeof(temp));
}
}
}
if(strlen(sum) == 0)
printf("IDENTITY LOST\n");
else
printf("%s\n", sum);
}
return 0;
}

POJ 3450--Corporate Identity【KMP &amp;&amp; 枚举】的更多相关文章

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

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

  2. POJ 3450 Corporate Identity KMP解决问题的方法

    这个问题,需要一组字符串求最长公共子,其实灵活运用KMP高速寻求最长前缀. 请注意,意大利愿父亲:按照输出词典的顺序的规定. 另外要提醒的是:它也被用来KMP为了解决这个问题,但是很多人认为KMP使用 ...

  3. POJ 3450 Corporate Identity(KMP)

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

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

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

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

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

  6. poj 3450 Corporate Identity

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

  7. POJ 题目3450 Corporate Identity(KMP 暴力)

    Corporate Identity Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5493   Accepted: 201 ...

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

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

  9. hdu 2328 Corporate Identity(kmp)

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

  10. POJ 3450 后缀数组/KMP

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

随机推荐

  1. BZOJ 1594 [Usaco2008 Jan]猜数游戏(线段数)

    1594: [Usaco2008 Jan]猜数游戏 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 626  Solved: 260[Submit][S ...

  2. Java Class文件结构

    此文件格式为JAVA7的格式,可能与JAVA6 CLASS不一致. 每一个Class都对应着唯一的一个类或借口的定义信息.这里,我们称为"Class文件格式"只是通俗的将任意一个符 ...

  3. ip iproute2的典型应用

    net-tools和iproute2的命令做对比,做到简单明了,分别演示如何去获取.配置和操作系统网络信息. 以下是net-tools和iproute2的大致对比: 4.1 ip link set-- ...

  4. 锐捷SNMp注意:

    http://www.ruijie.com.cn/fw/wt/18855 1)设备名称.运行时间.sysOID.设备描述.联系人.设备位置:分别来自RFC-1213-MIB的system组中的sysN ...

  5. Linux的中断和系统调用 & esp、eip等寄存器

    http://www.linuxidc.com/Linux/2012-11/74486.htm 一共三篇 中断一般分为三类: 1.由计算机硬件异常或故障引起的中断,称为内部异常中断: 2.由程序中执行 ...

  6. Unity 相机花式分屏

    花式分屏,顾名思义,可以实现各种不规则几何边界的分屏,是无法直接通过调整相机视口能达到效果的(只能实现矩形的分屏),例如斜对角分屏,几何图形分屏: 假设我们有两个相机,需要上面的斜对角分屏画面,和镜子 ...

  7. UItableView中的一些方法

    有关UITableView的知识点相对照较简单,一些简单的经常使用的方法有时间在写上: 以下的几个方法假设仅仅是展示UITableView是用不到的,须要对对应的区段进行操作的时候才会用到. 方法例如 ...

  8. 12、NIO、AIO、BIO二

    一.NIO2快速读写文件 写完之后记得flush一下,NIO2不能自行创建文件,需要在文件中判断一下. package com.zxc.L; import org.junit.Test; import ...

  9. Android4.2.2下Stagefright下OMX编解码器组件的控制流

    本文均属自己阅读源代码的点滴总结.转账请注明出处谢谢. 欢迎和大家交流. qq:1037701636 email:gzzaigcn2012@gmail.com Android源代码版本号Version ...

  10. Android5.0以上系统的移动网络开关

    笔者近期遇到一个非常有意思的bug,贴出来和大家分享下. 那是一个温暖的早晨,阳光晒得人非常舒服.一封bug邮件像一片叶子飘到我的邮箱. 一番交流.笔者确认负责的Widget开关在Android5.0 ...