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
题意:和前一题几乎一模一样,只是没有反转的情况(变简单了)
题解:枚举第一个的子串进行kmp
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 10007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1 using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=(<<)-,inf=0x3f3f3f3f; int Next[N],slen,plen;
string a[N],ptr,str; void getnext()
{
int k=-;
Next[]=-;
for(int i=;i<slen;i++)
{
while(k>-&&str[k+]!=str[i])k=Next[k];
if(str[k+]==str[i])k++;
Next[i]=k;
}
}
bool kmp()
{
int k=-;
for(int i=;i<plen;i++)
{
while(k>-&&str[k+]!=ptr[i])k=Next[k];
if(str[k+]==ptr[i])k++;
if(k==slen-)return ;
}
return ;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
// cout<<setiosflags(ios::fixed)<<setprecision(2);
int n;
while(cin>>n,n){
for(int i=;i<n;i++)cin>>a[i];
string ans="";
for(int i=;i<=a[].size();i++)
{
for(int j=;j<=a[].size()-i;j++)
{
str=a[].substr(j,i);
slen=str.size();
getnext();
bool flag=;
for(int k=;k<n;k++)
{
ptr=a[k];
plen=a[k].size();
if(kmp())continue;
flag=;
break;
}
if(flag)
{
if(ans.size()<str.size())ans=str;
else if(ans.size()==str.size()&&str<ans)ans=str;
}
}
}
if(ans!="")cout<<ans<<endl;
else cout<<"IDENTITY LOST"<<endl;
}
return ;
}

hdu2328 kmp的更多相关文章

  1. hdu2328 Corporate Identity 扩展KMP

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

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

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

  3. hdu-2328(暴力枚举+kmp)

    题意:给你n个字符串,问你这n个串的最长公共子串 解题思路:暴力枚举任意一个字符串的所有子串,然后暴力匹配,和hdu1238差不多的思路吧,这里用string解决的: 代码: #include< ...

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

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

  5. poj3080 Blue Jeans【KMP】【暴力】

    Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:21746   Accepted: 9653 Descri ...

  6. KMP算法求解

    // KMP.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> using namespac ...

  7. 简单有效的kmp算法

    以前看过kmp算法,当时接触后总感觉好深奥啊,抱着数据结构的数啃了一中午,最终才大致看懂,后来提起kmp也只剩下“奥,它是做模式匹配的”这点干货.最近有空,翻出来算法导论看看,原来就是这么简单(先不说 ...

  8. KMP算法

    KMP算法是字符串模式匹配当中最经典的算法,原来大二学数据结构的有讲,但是当时只是记住了原理,但不知道代码实现,今天终于是完成了KMP的代码实现.原理KMP的原理其实很简单,给定一个字符串和一个模式串 ...

  9. 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)

    前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...

随机推荐

  1. iphone6 inline-flex兼容问题

    在编写微信端页面时,遇到这样的问题:position属性为flex的导航栏,其li标签在其余设备上显示正常,但在iphone6上浮动错误. 究其原因,是iphone6不支持position属性中的fl ...

  2. Javascript 判断对象是否相等

    在Javascript中相等运算包括"==","==="全等,两者不同之处,不必多数,本篇文章我们将来讲述如何判断两个对象是否相等? 你可能会认为,如果两个对象 ...

  3. Web负载均衡学习笔记之四层和七层负载均衡的区别

    0x00 简介 简单理解四层和七层负载均衡: ① 所谓四层就是基于IP+端口的负载均衡:七层就是基于URL等应用层信息的负载均衡:同理,还有基于MAC地址的二层负载均衡和基于IP地址的三层负载均衡. ...

  4. bzoj4591 / P4345 [SHOI2015]超能粒子炮·改

    P4345 [SHOI2015]超能粒子炮·改 题意:求$\sum_{i=1}^{k}C(n,i)\%(P=2333)$ 肯定要先拆开,不然怎么做呢(大雾) 把$C(n,i)$用$lucas$分解一下 ...

  5. P1283 平板涂色

    P1283 平板涂色 dfs 记忆化搜索 将矩阵转化为图求解,然后我们发现这是个DAG,于是就可以愉快地跑搜索了. 进行dfs时,我们可以用类似拓扑排序的方法.每次将上面所有矩形都被刷过(入度in[ ...

  6. vc++之stdafx.h

    关于stdafx.h的解释,其实蛮多的,在vs中,既然创建c++工程的时候,默认会给生成main.cpp,并且自动包含了stdafx.h,而且stdafx.h不是c++标准的一部分,那么个人认为,理解 ...

  7. 20145335郝昊《网络对抗》Exp 8 Web基础

    20145335郝昊<网络对抗>Exp 8 Web基础 实验内容 本实践的具体要求有: Web前端HTML:能正常安装.启停Apache.理解HTML,理解表单,理解GET与POST方法, ...

  8. Program Size

    在Keil中编译工程成功后,在下面的Bulid Ouput窗口中会输出下面这样一段信息: Program Size: Code=6320  RO-data=4864  RW-data=44  ZI-d ...

  9. 在服务中用管理员权限创建一个可弹出UI的进程 (转载)

    转载:http://blog.csdn.net/woshinia/article/details/7850295 转载:http://blog.csdn.net/hurryboylqs/article ...

  10. 《Python程序设计(第3版)》[美] 约翰·策勒(John Zelle) 第 3 章 答案

    判断对错 1.由计算机存储和操作的信息称为数据.2.由于浮点数是非常准确的,所以通常应该使用它们,而不是int.3.像加法和减法这样的操作在mAth库中定义.4.n 项的可能排列的数目等于 n!.5. ...