kuangbin专题十六 KMP&&扩展KMP HDU2328 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.
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<stdio.h>
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int n,Next[];
vector<string> t;
string s,ans; void prekmp(string s) {
int len=s.size();
int i,j;
j=Next[]=-;
i=;
while(i<len) {
while(j!=-&&s[i]!=s[j]) j=Next[j];
Next[++i]=++j;
}
} int kmp(string t,string p) {
int len=t.size();
int i,j,res=-;
i=j=;
while(i<len) {
while(j!=-&&t[i]!=p[j]) j=Next[j];
++i;++j;
res=max(res,j);
}
return res;
} int main() {
//freopen("in","r",stdin);
while(~scanf("%d",&n)&&n) {
for(int i=;i<n;i++) {
cin>>s;
t.push_back(s);
}
string str=t[],tempstr;
int maxx=-,res;
int len=str.size();
for(int i=;i<len;i++) {
tempstr=str.substr(i,len-i);
prekmp(tempstr);
res=;
for(int j=;j<t.size();j++) {
res=min(kmp(t[j],tempstr),res);
}
if(res>=maxx&&res) {
if(res==maxx) {
string tempstr1=tempstr.substr(,maxx);
if(tempstr1<ans)
ans=tempstr1;
} else {
maxx=res;
ans=tempstr.substr(,maxx);
}
}
}
if(ans.size()) cout<<ans<<endl;
else cout<<"IDENTITY LOST"<<endl;
ans.clear();
t.clear();
}
}
kuangbin专题十六 KMP&&扩展KMP HDU2328 Corporate Identity的更多相关文章
- kuangbin专题十六 KMP&&扩展KMP HDU2609 How many (最小字符串表示法)
Give you n ( n < 10000) necklaces ,the length of necklace will not large than 100,tell me How man ...
- kuangbin专题十六 KMP&&扩展KMP HDU1238 Substrings
You are given a number of case-sensitive strings of alphabetic characters, find the largest string X ...
- kuangbin专题十六 KMP&&扩展KMP HDU3336 Count the string
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
- kuangbin专题十六 KMP&&扩展KMP POJ3080 Blue Jeans
The Genographic Project is a research partnership between IBM and The National Geographic Society th ...
- kuangbin专题十六 KMP&&扩展KMP HDU3746 Cyclic Nacklace
CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, ...
- kuangbin专题十六 KMP&&扩展KMP HDU2087 剪花布条
一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案.对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢? Input输入中含有一些数据,分别是成对出现的花布条和小 ...
- kuangbin专题十六 KMP&&扩展KMP HDU1686 Oulipo
The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e ...
- kuangbin专题十六 KMP&&扩展KMP HDU1711 Number Sequence
Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M ...
- kuangbin专题十六 KMP&&扩展KMP HDU3613 Best Reward(前缀和+manacher or ekmp)
After an uphill battle, General Li won a great victory. Now the head of state decide to reward him w ...
随机推荐
- windows下基于bat的每1分钟执行一次一个程序
@echo off cls mode con cols=35 lines=6 & color 5B :p call python C:\省局监控\ahwater_perf_monitor.py ...
- 基本的数据类型 void关键字 都存在类类型
- JVM实用参数(二)参数分类和即时(JIT)编译器诊断
JVM实用参数(二)参数分类和即时(JIT)编译器诊断 作者: PATRICK PESCHLOW 原文地址 译者:赵峰 校对:许巧辉 在这个系列的第二部分,我来介绍一下HotSpot J ...
- 【总结整理】dojo学习
Dojo Toolkit 的特性可以分到 4 个不同部分.这种划分使得开发人员可以将库大小保持到最小,确保应用程序性能不受大量 JavaScript 库下载的影响.例如,如果您只需要 Ajax 支持性 ...
- GSON 报错HibernateProxy. Forgot to register a type adapter? 的解决办法
使用Gson转换hibernate对象遇到一个问题,当对象的Lazy加载的,就会出现上面的错误.处理方式摘抄自网上,留存一份以后自己看. 网上找到的解决办法,首先自定义一个类继承TypeAdapter ...
- Windows cmd 将命令(/指令)写到一个文件里,直接运行这个文件。提高工作效率
Windows cmd 批处理(cmd/bat)文件的简单使用介绍 前言 如果你想我一样,要每天都需要在cmd上,用键盘去敲击相同的命令,时间一长,你就觉得很无聊.有没有什么比较高效的方法,让我们不用 ...
- Win10 VS2013 suitesparse-metis-for-windows 1.3.1
suitesparse-metis-for-windows 1.3.1 安装包内附SuiteSparse 4.5.1, Metis 5.1.0和 lapack 3.4.1 Github上面由整理好的s ...
- wpf 窗口打开后默认设置控件焦点
https://blog.csdn.net/Vblegend_2013/article/details/81771872 <Grid FocusManager.FocusedElement=&q ...
- docker卸载与安装
sudo apt remove docker.io 报错如下: Reading package lists... Done Building dependency tree Reading state ...
- Luogu 1081 [NOIP2012] 开车旅行
感谢$LOJ$的数据让我调掉此题. 这道题的难点真的是预处理啊…… 首先我们预处理出小$A$和小$B$在每一个城市的时候会走向哪一个城市$ga_i$和$gb_i$,我们有链表和平衡树可以解决这个问题( ...