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.

InputThe 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.OutputFor 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<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include <sstream>
#include<string>
#include<cstring>
#include<list>
using namespace std;
#define MAXN 202
#define INF 0x3f3f3f3f
#define N 4002
typedef long long LL;
/*
枚举第一个串的所有子串 找到就停止
*/
char s[N][MAXN];
int Next[MAXN];
void kmp_pre(char t[])
{
int m = strlen(t);
int j,k;
j = ;k = Next[] = -;
while(j<m)
{
if(k==-||t[j]==t[k])
Next[++j] = ++k;
else
k = Next[k];
}
}
bool KMP(char t[],char s[])
{
int n = strlen(s),m=strlen(t);
int i,j;
j = ;
for(i=;i<n;i++)
{
while(j>&&s[i]!=t[j])
j = Next[j];
if(s[i]==t[j])
j++;
if(j>=m)
return true;
}
return false;
}
int main()
{
int n;
while(scanf("%d",&n),n)
{
int len = INF,k=-;
for(int i=;i<n;i++)
{
scanf("%s",s[i]);
if(len>strlen(s[i]))
{
len = strlen(s[i]);
k = i;
}
}
bool f = false;
char ans[MAXN];
for(int l=len;l>;l--)
{
for(int i=;i+l<=len;i++)
{
char t[MAXN] = {};
strncpy(t,s[k]+i,l);
kmp_pre(t);
int j;
//cout<<t<<endl;
for(j=;j<n;j++)
{
if(j==k) continue;
if(KMP(t,s[j]))
continue;
else
break;
}
if(j==n)
{
if(!f)
{
strcpy(ans,t);
f = true;
}
else
{
if(strcmp(ans,t)>)
strcpy(ans,t);
}
}
if(f&&(i+l==len))
{
printf("%s\n",ans);
i = INF;
l = -;
break;
}
}
}
if(!f)
printf("IDENTITY LOST\n");
}
}

只需要枚举所有后缀,然后在其他串中找最长相同前缀即可

#include <stdio.h>
#include <string.h> const int MAX_N = ;
const int MAX_CHS = ;
const int ARR_SIZE = ;
int N;
char res[MAX_CHS], dict[MAX_N][MAX_CHS];
short next[MAX_CHS]; inline int min(int a, int b) { return a < b ? a : b; }
inline int max(int a, int b) { return a > b ? a : b; } void getNext(char *chs, int len)
{
memset(next, , sizeof(short)*len);
for (int i = , j = ; i < len; )
{
if (chs[i] == chs[j]) next[i++] = ++j;
else if (j > ) j = next[j-];
else i++;
}
} int getLogestPre(char *chs, int len)
{
getNext(chs, len);
for (int i = ; i < N; i++)
{
char *p = dict[i];
int j = , tmp = ;
for (; *p && j < len; )
{
if (*p == chs[j])
{
p++, j++;
tmp = max(tmp, j);
}
else if (j > ) j = next[j-];
else p++;
}
len = tmp;
}
return len;
} int main()
{
while (scanf("%d", &N) && N)
{
getchar(); // don't forget to get rid of '\'
for (int i = ; i < N; i++)
{
gets(dict[i]);
}
int len = strlen(dict[]), ans = , pos = ;
for (int i = ; i < len; i++)
{
int tmp = getLogestPre(dict[]+i, len-i);
if (tmp >= ans)
{
if (tmp > ans)
{
ans = tmp;
pos = i;
}
else
{
bool smaller = true;
for (int t = ; t < ans; t++)
{
if (dict[][pos+t] > dict[][i+t]) break;
else if (dict[][pos+t] < dict[][i+t])
{
smaller = false;
break;
}
}
if (smaller) pos = i;
}
}
}
if (ans)
{
for (int i = ; i < ans; i++)
{
putchar(dict[][pos+i]);
}
putchar('\n');
}
else puts("IDENTITY LOST");
}
return ;
}

N - Corporate Identity的更多相关文章

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

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

  2. hdu 2328 Corporate Identity(kmp)

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

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

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

  4. hdu2328 Corporate Identity 扩展KMP

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

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

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

  6. hdu2328 Corporate Identity

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=2328 题目: Corporate Identity Time Limit: 9000/3000 MS (J ...

  7. POJ3450 Corporate Identity 【后缀数组】

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

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

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

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

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

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

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

随机推荐

  1. Antenna Placement(二分图的最大匹配)

    http://poj.org/problem?id=3020 题意: 一个矩形中,有N个城市'*',现在这n个城市都要覆盖无线,若放置一个基站,它至多可以覆盖相邻的两个城市.问至少放置多少个基站才能使 ...

  2. flux,redux,vuex状态集管理工具之间的区别

    一:redux和flux的区别 1)redux是flux中的一个实现 2))在redux中我们只能定义一个store,在flux中我们可以定义多个 3)在redux中,store和dispatch都放 ...

  3. php 时间戳和时间的转换

    PHP的时间戳与具体时间转化 三个内置函数: time() //获取UNIX系统时间戳 mktime(hour,minute,second,month,day,year) //将指定时间转化为时间戳 ...

  4. Java并发编程系列之Semaphore详解

    简单介绍 我们以饭店为例,假设饭店只有三个座位,一开始三个座位都是空的.这时如果同时来了三个客人,服务员人允许他们进去用餐,然后对外说暂无座位.后来的客人必须在门口等待,直到有客人离开.这时,如果有一 ...

  5. Elasticsearch索引的操作,利用kibana(如何创建/删除一个es的索引?)

    我们已经通过索引一篇文档创建了一个新的索引 .这个索引采用的是默认的配置,新的字段通过动态映射的方式被添加到类型映射.现在我们需要对这个建立索引的过程做更多的控制:我们想要确保这个索引有数量适中的主分 ...

  6. 悼念512汶川大地震遇难同胞——老人是真饿了 hdu 2187

    在此对 曾经 努力参加 救援的人 致以深深的敬意 . 这一道题 挺简单的 就是简单的  结构体+贪心 而已 不过 用英文 注释  是一个 很大的 进步 ,  以后 要习惯 http://acm.hdu ...

  7. BZOJ 1129 exgcd+CRT+线段树

    思路: 先copy一下百度百科 作为预备知识吧多重全排列定义:求r1个1,r2个2,…,rt个t的排列数,设r1+r2+…+rt=n,设此排列数称为多重全排列,表示为$P(n;r1,r2,…,rt)$ ...

  8. 表格对象的获取和更改(原生js)

    表格对象的获取 var oT = document.getElementById("tb"); //获取head console.log(oT.tHead); console.lo ...

  9. 联想 A5(L18011) 免解锁BL 免rec Magisk Xposed ROOT 救砖 ZUI 3.9.068

    >>>重点介绍<<< 第一:本刷机包可卡刷可线刷,刷机包比较大的原因是采用同时兼容卡刷和线刷的格式,所以比较大第二:[卡刷方法]卡刷不要解压刷机包,直接传入手机后用 ...

  10. React Native状态机和应用设计思路

    React Native状态机和应用设计思路 在原生Android开发中:当用户点击“登录”按钮时,从用户名输入框中读取用户输入的用户名,从密码输入框中读取用户输入的密码,然后交给注册模块去处理.但是 ...