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. 20145316许心远《网络对抗》MSF基础应用

    20145316许心远<网络对抗>MSF基础应用 实验后回答问题 用自己的话解释什么是exploit,payload,encode. exploit:顾名思义就是攻击嘛,因为是个动词,所以 ...

  2. 使用原生的javascript封装动画函数(有callback功能)

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  3. (四)github之Git的初始设置

    设置姓名与邮箱地址 这里的姓名和邮箱地址会用在git的提交日志之中,在github上公开git仓库时会随着提交日志一起公开. 有两种方式, 第一种,在git bash下设置 第二种, 通过直接编辑.g ...

  4. P3868 [TJOI2009]猜数字

    [TJOI2009]猜数字 中国剩余定理 求解i=1 to n : x≡a[i] (mod b[i])的同余方程组 设 t= ∏i=1 to n b[i] 我们先求出 i=1 to n : x≡1 ( ...

  5. js面向对象编程: js类定义函数时prototype和this区别?

    参考文章的链接:http://www.2cto.com/kf/201406/307790.html 测试代码如下: function ListCommon2(afirst) { var first=a ...

  6. 20145101《Java程序设计》第4周学习总结

    20145101<Java程序设计>第4周学习总结 教材学习内容总结 第六章 继承与多态 继承:避免多个类间重复定义共同行为. 把相同代码提升为父类 运用extends关键字的子类会继承扩 ...

  7. 20135234mqy-——信息安全系统设计基础第五周学习总结

    程序的机器级表示 3.1 intel处理器系列俗称x86,经历了一开始个长期的,不断进化的发展过程. 开始时它是第一代单芯片,16位微处理器之一,由于当时集成电路技术水性有限,其中做了很多妥协,此后, ...

  8. python字符串格式化之format

    用法: 它通过{}和:来代替传统%方式 1.使用位置参数 要点:从以下例子可以看出位置参数不受顺序约束,且可以为{},只要format里有相对应的参数值即可,参数索引从0开,传入位置参数列表可用*列表 ...

  9. IntelliJ IDEA 连接数据库 详细过程

    IntelliJ IDEA集成了众多插件,方便开发者使用,使用其自带的Database模块就可以很方便的配置.连接到数据库,本次操作以MySQL为例,其中testjdbc数据库已经提前建好,里面有两张 ...

  10. 【入门详解】MyBatis入门基础详解

    什么是mybatis? MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索.MyBatis使用简单的XM ...