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 ...
随机推荐
- ORA-00907: 缺失右括号问题或com.alibaba.druid.sql.parser.ParserException: TODO :IDENTIFIER的原因
以上只是说明错误的原因的一种.
- selenium3 + python - autoit上传文件
一.环境准备: 1.可以autoit官网上下载,安装 http://www.autoitscript.com/site/ 2.AutoIt里面几个菜单功能介绍: SciTE Script Editor ...
- 【BZOJ2565】最长双回文串 (Manacher算法)
题目: BZOJ2565 分析: 首先看到回文串,肯定能想到Manacher算法.下文中字符串\(s\)是输入的字符串\(str\)在Manacher算法中添加了字符'#'后的字符串 (构造方式如下) ...
- 设置myeclipse的JSP、HTML的页面编码格式
JSP编码格式: 点击菜单上的window--->preferences 在弹出的对话框中点击MyEclise--->Files and Editors--->JSP, 在Encod ...
- 【Codeforces】383.DIV2
昨天一场CF发挥不好.抽点时间总结一下,然后顺带算是做个题解. 第一题水题 第二题思路很清晰,大概十分钟就想出来规模100000明显复杂度最多nlog所以只能一遍loop然后里利用map统计得到后面的 ...
- java.net.URISyntaxException: Illegal character in query
java使用httpclient爬取一个网站的时候,请求:String url3="http://sh.58.com/ershoufang/33562546149042x.shtml?amp ...
- [ 51Nod 1327 ] 棋盘游戏
\(\\\) \(Description\) 给出一张\(N\times M\)的棋盘,每个格子最多放置一个棋子,一个合法的放置方案需满足: 每列至多放置一个棋子 对于第\(i\)行,前\(L_i\) ...
- Android热修复方案比较
热修复的特点:无需重新发版,实时高效热修复:用户无感知修复,无需下载新的应用,代价小: 修复成功率高,把损失降到最低. 一.热修复开源方案和使用情况 方案名称 方案开发公司 开发时间 Github星评 ...
- 移动web——bootstrap模板
基本概念 1.bootstrap就是在媒体查询技术出现以后才开始出现的 2.此技术使响应式开发变得十分轻松,最大特点就是栅格系统(什么设备上如何显示)以及响应式工具(是否可见) 基本模板 <!D ...
- html5——3D案例(立方体)
立方体:父盒子规定了3d呈现属性,立方体做旋转运动 移动顺序:1.每个盒子都先移动100px,然后再做相应的旋转 2.只有这样立方体的几何中心点与父盒子的几何中心点是一样的 <!DOCTYPE ...