N - Corporate Identity
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的更多相关文章
- POJ-3450 Corporate Identity (KMP+后缀数组)
Description Beside other services, ACM helps companies to clearly state their “corporate identity”, ...
- hdu 2328 Corporate Identity(kmp)
Problem Description Beside other services, ACM helps companies to clearly state their “corporate ide ...
- hdu2328 Corporate Identity【string库使用】【暴力】【KMP】
Corporate Identity Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu2328 Corporate Identity 扩展KMP
Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...
- (KMP 暴力)Corporate Identity -- hdu -- 2328
http://acm.hdu.edu.cn/showproblem.php?pid=2328 Corporate Identity Time Limit: 9000/3000 MS (Java/Oth ...
- hdu2328 Corporate Identity
地址:http://acm.hdu.edu.cn/showproblem.php?pid=2328 题目: Corporate Identity Time Limit: 9000/3000 MS (J ...
- POJ3450 Corporate Identity 【后缀数组】
Corporate Identity Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 7662 Accepted: 264 ...
- kuangbin专题十六 KMP&&扩展KMP HDU2328 Corporate Identity
Beside other services, ACM helps companies to clearly state their “corporate identity”, which includ ...
- POJ3450 Corporate Identity —— 后缀数组 最长公共子序列
题目链接:https://vjudge.net/problem/POJ-3450 Corporate Identity Time Limit: 3000MS Memory Limit: 65536 ...
- POJ 题目3450 Corporate Identity(KMP 暴力)
Corporate Identity Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 5493 Accepted: 201 ...
随机推荐
- etcd磁盘清理步骤
etcd默认的空间配额限制为2G,超出空间配额限制就会影响服务,所以需要定期清理 以下是etcd磁盘清理的步骤: 1. 显示空间配额: ETCDCTL_API=3 etcdctl --endpoint ...
- PCB 钻孔补偿那点事
没有优秀的个人,只有优秀的团队,在团队共同的协作下,PCB CAM自动化[net处理]与[钻孔处理] 第一阶段开发项完成了,,后续工作可以转向PCB规则引擎开发了.这里说说PCB工程钻孔补偿的那点事, ...
- E20170630-ts
displacement n. 取代,替代; 免职,停职; [船] 排水量; [化] 置换;
- [Swift通天遁地]三、手势与图表-(11)制作雷达图表更加形象表示各个维度的情况
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- Windows(7/8/10)搭建Elasticsearch 6.x版本
今天公司用到了Elasticsearch ,记录一下单机版搭建的流程. 首先我们来看下什么是Elasticsearch : ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分 ...
- Springboot2.0部署阿里云服务器(nginx+域名+SSL)供Http和Https访问
总算是弄出来了,先写下来供自己以后查阅. 1)首先你要有一个阿里云服务器,我用的是Centos7学生认证,10元/月,很便宜也很好用. 2)购买了域名,首年9元,很划算.域名买来之后经历了拍照备案,前 ...
- 判断IOS静态库(.a文件)是否支持模拟器和真机运行
判断IOS静态库(.a文件)是否支持模拟器和真机运行 在mac终端下,进入到.a文件目录下,然后输入: lipo -info libMyAlertView.a Architectures in the ...
- dynamic_cast 与 typeid
C++中的类型转换分为两种: 隐式类型转换: 显式类型转换. 隐式类型转换一般都是不经意间就发生了,比如int + float 时,int就被隐式的转换为float类型了. 显示类型转换包括四种方式: ...
- 多重背包(MultPack = ZeroOnePack + CompletePack)
HiHoCoder_offer6_04 题目4 : 奖券兑换 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi在游乐园中获得了M张奖券,这些奖券可以用来兑换奖品. ...
- [ JSOI 2015 ] Salesman
\(\\\) \(Description\) 给出一棵以\(1\)为根的\(N\)个节点的树,开始的时候你在\(1\)号节点. 除了\(1\)号节点以外,每个点都有访问次数限制\(t_i\),即到达该 ...