hdu 2328 Corporate Identity(kmp)

时间:2022-09-08 13:43:59
Problem Description
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.

 
Input
The 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.

 
Output
For 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
 
Sample Output
abb
IDENTITY LOST

题意&思路:hdu 1238 数据的加强版 需要注意优化点已在代码处标记

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define ll long long int
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[]={,,,,,,,,,,,,};
int dir[][]={, ,, ,-, ,,-};
int dirs[][]={, ,, ,-, ,,-, -,- ,-, ,,- ,,};
const int inf=0x3f3f3f3f;
const ll mod=1e9+;
int n;
string s[];
int nextt[];
void getnext(string s){
nextt[]=;
int len=s.length();
for(int i=,j=;i<=len;i++){
while(j>&&s[i-]!=s[j]) j=nextt[j];
if(s[i-]==s[j]) j++;
nextt[i]=j;
}
}
bool kmp(string t,string p){
int len1=p.length();
int len2=t.length();
for(int i=,j=;i<=len1;i++){
while(j>&&p[i-]!=t[j]) j=nextt[j];
if(p[i-]==t[j]) j++;
if(j==len2) return true;
}
return false;
}
int main(){
ios::sync_with_stdio(false);
while(cin>>n&&n){
for(int i=;i<n;i++)
cin>>s[i];
int len=s[].length();
string ans="";
for(int i=len;i>=;i--){
for(int j=;j<=len-i;j++){
string temp=s[].substr(j,i);
getnext(temp);
bool f=;
for(int k=;k<n;k++)
if(!kmp(temp,s[k])){
f=;
break; //碰到假就弹出
}
if(f){
if(ans.size()<temp.size()) ans=temp;
else if(ans.size()==temp.size()) ans=min(ans,temp);
}
}
}
if(ans.size()<) cout<<"IDENTITY LOST"<<endl;
else cout<<ans<<endl; }
return ;
}

hdu 2328 Corporate Identity(kmp)的更多相关文章

  1. HDU - 2328 Corporate Identity(kmp&plus;暴力)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2328 题意:多组输入,n==0结束.给出n个字符串,求最长公共子串,长度相等则求字典序最小. 题解:(居 ...

  2. POJ-3450 Corporate Identity &lpar;KMP&plus;后缀数组&rpar;

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

  3. HDU 2328 POJ 3450 KMP

    题目链接:  HDU http://acm.hdu.edu.cn/showproblem.php?pid=2328 POJhttp://poj.org/problem?id=3450 #include ...

  4. POJ 3450 &Tab;Corporate Identity kmp&plus;最长公共子串

    枚举长度最短的字符串的所有子串,再与其他串匹配. #include<cstdio> #include<cstring> #include<algorithm> #i ...

  5. POJ 3450 Corporate Identity KMP解决问题的方法

    这个问题,需要一组字符串求最长公共子,其实灵活运用KMP高速寻求最长前缀. 请注意,意大利愿父亲:按照输出词典的顺序的规定. 另外要提醒的是:它也被用来KMP为了解决这个问题,但是很多人认为KMP使用 ...

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

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

  7. hdu2328 Corporate Identity 扩展KMP

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

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

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

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

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

随机推荐

  1. WebAPI图片上传

    public Task<HttpResponseMessage> PostFormData() { // Check if the request contains multipart/f ...

  2. The project&colon; project which is referenced by the classpath&comma; does not exist&period;

    有个java工程运行时报The project: project which is referenced by the classpath, does not exist.错误 上网查解决方案,说是将 ...

  3. mysql 存储过程简介

    存储过程类似一个存储在数据库的一个数据库脚本.它类似一个方法,可以批量执行一些数据库的操作. 本文编写一个简单的存储过程来快速了解存储过程. 1.因为存储过程类似编程语言的方法,所以方法中可能会用到 ...

  4. volley开源库乱码问题总结&lpar;持续更新&rpar;

    之前Android开发一直用的是多层封装的Final框架.最近开始学习使用小巧的volley. 在使用该框架的过程中,出现了数次乱码问题,再次做以总结. 分别是返回数据乱码和提交参数乱码两个问题: 一 ...

  5. Python自动化运维之8、正则表达式re模块

    re模块 正则表达式使用单个字符串来描述.匹配一系列符合某个句法规则的字符串,在文本处理方面功能非常强大,也经常用作爬虫,来爬取特定内容,Python本身不支持正则,但是通过导入re模块,Python ...

  6. 某xss挑战赛闯关笔记

    0x0 前言 在sec-news发现先知上师傅monika发了一个xss挑战赛的闯关wp([巨人肩膀上的矮子]XSS挑战之旅---游戏通关攻略(更新至18关)https://xianzhi.aliyu ...

  7. HighGUI图形图像界面初步——滑动条的创建和使用

    ---恢复内容开始--- 滑动条是OpenCV动态调节参数特别好用的一种工具,它依附于窗口而存在.由于OpenCV没有实现按钮功能,所以很多时候,我们可以用仅含0-1的滑动条来实现按钮的按下于弹起效果 ...

  8. libevent中evmap实现(哈希表)

    libevent中,需要将大量的监听事件event进行归类存放,比如一个文件描述符fd可能对应多个监听事件,对大量的事件event采用监听的所采用的数据结构是event_io_map,其实现通过哈希表 ...

  9. Codeforces 1130D1 Toy Train &lpar;Simplified&rpar; &lpar;思维&rpar;【贪心】

    <题目链接> 题目大意: 有一个的环形火车站,其中有$[1,n] n$个站台,站台上能够放糖果,火车只能朝一个方向移动,如果火车在站台$i$,那么下一秒就会在$i+1$站(如果$i=n$, ...

  10. kettle 6&period;1 按时间循环增量抽取数据

    场景:假设有一张表数据量很大,需要按一个时间来循环增量抽取 方法:主要是通过JOB自身调用,实现循环调用,类似于 函数自调用 的循环. 1.JOB全图: 2.获取增量时间,并设置增量时间环境变量 3. ...