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. 原生ajax实现文件上传

    视图层 JS 函数:    <input type="file" onchange="sendFile()" id="up" /> ...

  2. 【BZOJ4940】【YNOI2016】这是我自己的发明

    阅读此篇文章前请先跟我大喊三声:dllxl!dllxl!dllxl! 咳咳. 题意: Description 给一个树,n 个点,有点权,初始根是 1. m 个操作,每次操作: 1. 将树根换为 x. ...

  3. NOIp2018模拟赛四十二

    今天看标题终于回到了“NOIP模拟赛”,十分高兴啊! 然后一打开题目: ********** 所以今天又是一场NOIPlus模拟赛(微笑) 成绩:0+70+0=70 A题想了个贪心被myh两分钟cha ...

  4. [洛谷P1750]KC喝咖啡

    题目大意:给你n个物品,每个物品有一个价值$v_i$和一个时间$t_i$,要你取m个物品,使得他们的美味度($\frac{\sum v_i}{\sum t_i}$)最大,求这个美味度. 解题思路:由于 ...

  5. 【jQuery04】折叠树

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. /etc/rc.d/rc.sysinit

    [root@web02 ~]# ls /etc/rc.d/rc.sysinit /etc/rc.d/rc.sysinit [root@web02 ~]# [root@web02 ~]# ls /etc ...

  7. MHA搭建及故障维护

    MHA是一种方便简单可靠的MySQL高可用架构,具体的介绍我在这里就不多说了,下面是我在网上找的一个教程,我在此基础上进行了一些修改: 大致步骤 (一).环境介绍 (二).用ssh-keygen实现四 ...

  8. CentOS的基本设置界面

    系统的基本设置,如语言.键盘鼠标.时间.网络.壁纸.通知等功能的设置 高级设置:如磁盘分区.系统日志.各种系统分析工具

  9. 错误解决:error: expected ‘;’, ‘,’ or ‘)’ before ‘&’ token

    今天看到一个比较有趣的题目,如下代码,分析输出结果 #include <stdio.h> void num(int &b) { b = 222; return; } int mai ...

  10. ZOJ 3365 Integer Numbers

    Integer Numbers Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on ZJU. Origina ...