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 ...
随机推荐
- [我的CVE][CVE-2017-15708]Apache Synapse Remote Code Execution Vulnerability
漏洞编号:CNVD-2017-36700 漏洞编号:CVE-2017-15708 漏洞分析:https://www.javasec.cn/index.php/archives/117/ [Apache ...
- Striker-一款功能较多的web渗透工具
项目地址:https://github.com/UltimateHackers/Striker 首先下载项目,并打开 ┌─[root@sch01ar]─[/sch01ar] └──╼ #git clo ...
- 部署和调优 3.2 dns安装配置-2
配置一个自定义的域,随便定义的,不实际存在. 在配置文件里,增加一个域 vim /etc/named.conf zone "123.com" IN { type master; f ...
- pl/sql对excel数据的导入和导出
本来这部分是在上篇pl/sql的,但笔者介于此篇的内容,就独立出来了, 1.导出查询结果到excel文件,在查询结果上右键,然后弹出选择框如下: 2.从excel向数据库中导入数据: a.创建要导入的 ...
- Excel向数据库插入数据(执行一次只需连接一次)-batch简单使用
由于前端时间向数据库插入excel中的数据时,每插入一条数据,就得连接一次数据库:后来发现这种做法不好,如果excel中有很多条数据,就得连接很多次数据库,这样就很浪费资源而且不安全,有时数据库也会报 ...
- Django的Model使用
创建模型 使用Django的模型主要注意两个方面:字段的类型和方法的重写.这里用一个例子来说明,其中包含了常用的字段类型和如何重写方法. from django.db import models cl ...
- hibernate学习笔记(2)持久化类测试
持久化类的创建: 创建一个共有的不带参数的构造方法: public void Students(){ } 创建一个带参数的构造方法: (快捷键创建) 生成get,set方法: *可以不用此方法创建持久 ...
- sizeof总结
1.sizeof常用总结 ①与strlen比较 strlen 计算字符串的字符数,以"\0"为结束判断,但不统计结束符. sizeof 计算数据(数组.变量.类型. ...
- Arduino Uno 在win7 64位下的驱动问题
1.解压[mdmcpq.inf_amd64_neutral_fbc4a14a6a13d0c8.rar],将[mdmcpq.inf_amd64_neutral_fbc4a14a6a13d0c8]文件夹复 ...
- [Elasticsearch2.x] 多字段搜索 (二) - 最佳字段查询及其调优 <译>
最佳字段(Best Fields) 假设我们有一个让用户搜索博客文章的网站,就像这两份文档一样: PUT /my_index/my_type/ { "title": "Q ...