POJ 3450--Corporate Identity【KMP && 枚举】
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 5696 | Accepted: 2075 |
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
0
Sample Output
abb
IDENTITY LOST
思路:枚举一个串的所有子串。推断该子串有没有在其他串中出现。若在其他串中所有出现 则更新子串,否则枚举下一个子串。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char str[4010][210];
int next[210]; void getnext(char *s1){
int j = 0, k = -1;
int len = strlen(s1);
next[0] = -1;
while(j < len){
if(k == -1 || s1[j] == s1[k]){
++j;
++k;
next[j] = k;
}
else k = next[k];
}
return ;
} bool kmp(char *s1, char *s2){
int len1 = strlen(s2);
int len2 = strlen(s1);
getnext(s1);
int i = 0, j = 0;
while(i < len1){
if(j == -1 || s1[j] == s2[i]){
++i;
++j;
}
else j=next[j];
if(j == len2)
return true;
}
return false;
} int main(){
int n;
while(scanf("%d", &n), n){
for(int i = 0 ; i < n; ++i)
scanf("%s", str[i]);
char temp[220];
char sum[220] = "";
int len = strlen(str[0]);
for(int i = 0; i < len; ++i){
int ans = 0;
for(int j = i; j < len; ++j){
temp[ans++] = str[0][j];
temp[ans] = '\0';
int flag = 1;
for(int k = 0 ; k < n; ++k){
if(!kmp(temp, str[k])){
flag = 0;
break;
}
}
if(flag){
if(strlen(temp) > strlen(sum))
memcpy(sum, temp, sizeof(temp));
else if(strlen(temp) == strlen(sum) && strcmp(temp, sum) < 0)
memcpy(sum, temp, sizeof(temp));
}
}
}
if(strlen(sum) == 0)
printf("IDENTITY LOST\n");
else
printf("%s\n", sum);
}
return 0;
}
POJ 3450--Corporate Identity【KMP && 枚举】的更多相关文章
- POJ 3450 Corporate Identity kmp+最长公共子串
枚举长度最短的字符串的所有子串,再与其他串匹配. #include<cstdio> #include<cstring> #include<algorithm> #i ...
- POJ 3450 Corporate Identity KMP解决问题的方法
这个问题,需要一组字符串求最长公共子,其实灵活运用KMP高速寻求最长前缀. 请注意,意大利愿父亲:按照输出词典的顺序的规定. 另外要提醒的是:它也被用来KMP为了解决这个问题,但是很多人认为KMP使用 ...
- POJ 3450 Corporate Identity(KMP)
[题目链接] http://poj.org/problem?id=3450 [题目大意] 求k个字符串的最长公共子串,如果有多个答案,则输出字典序最小的. [题解] 我们对第一个串的每一个后缀和其余所 ...
- POJ 3450 Corporate Identity (KMP,求公共子串,方法很妙)
http://blog.sina.com.cn/s/blog_74e20d8901010pwp.html我采用的是方法三. 注意:当长度相同时,取字典序最小的. #include <iostre ...
- POJ 3450 Corporate Identity (KMP+暴搞)
题意: 给定N个字符串,寻找最长的公共字串,如果长度相同,则输出字典序最小的那个. 找其中一个字符串,枚举它的所有的字串,然后,逐个kmp比较.......相当暴力,可二分优化. #include & ...
- poj 3450 Corporate Identity
题目链接:http://poj.org/problem?id=3450 题目分类:后缀数组 题意:求n个串的最长公共字串(输出字串) //#include<bits/stdc++.h> # ...
- POJ 题目3450 Corporate Identity(KMP 暴力)
Corporate Identity Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 5493 Accepted: 201 ...
- POJ-3450 Corporate Identity (KMP+后缀数组)
Description Beside other services, ACM helps companies to clearly state their “corporate identity”, ...
- hdu 2328 Corporate Identity(kmp)
Problem Description Beside other services, ACM helps companies to clearly state their “corporate ide ...
- POJ 3450 后缀数组/KMP
题目链接:http://poj.org/problem?id=3450 题意:给定n个字符串,求n个字符串的最长公共子串,无解输出IDENTITY LOST,否则最长的公共子串.有多组解时输出字典序最 ...
随机推荐
- tinymce原装插件源码分析(四)-fullscreen
fullscreen 作为一款文本编辑器,全屏功能是非常有必要的.在插件中主要是修改一些css style和触发resize事件. style问题(反例): 见github源码:https://git ...
- vue单文件中引用路径的处理
原文地址:vue单文件中引用路径的处理如有错误,欢迎指正! vue单文件的开发过程中,在单文件模版中可能会涉及到文件路径的处理,比如 <img>, style 中的 background ...
- window安装特定补丁(勒索病毒)
最近出现震惊的蠕虫病毒(勒索病毒),微软也做出相应的安全补丁来修复 MS17-010.这时有些同学不想打开电脑 的自动更新,这样会下载大量补丁,要更新完这些补丁要好几个小时,为了不影响正常工作,我们就 ...
- 实验二实验结论&实验总结与体会
Part1:格式化输出函数printf()和格式化输入函数scanf() ① /* <C语言程序设计教程学习指导>p118 实验内容(1) 这是一个常用格式控制符使用示例 运行程序,结合运 ...
- Python学习之三【对象和类型&&运算符】
[对象和类型] 学生的属性: 小明 对象 姓名:男 性别: 年龄: 身高: 体重: 籍贯: 五种基本对象类型 字符串 (string),简记为 str 使用 ' ' 或 " " 括 ...
- POJ 1741 Tree 树的分治(点分治)
题目大意:给出一颗无根树和每条边的权值,求出树上两个点之间距离<=k的点的对数. 思路:树的点分治.利用递归和求树的重心来解决这类问题.由于满足题意的点对一共仅仅有两种: 1.在以该节点的子树中 ...
- MapReduce----K-均值聚类算法
对于K-均值聚类算法MapReduce的过程理解例如以下: 如果有个Mapper,首先把数据集分为个子集,分布到个Mapper上,初始化..并同一时候广播到H个Mapper上. E步: 在第一台Map ...
- Hibernate_9_Person和IdCard实例_一对一关系:基于主键
1)建立Person类:(与8同样) 2)建立IdCard类:(与8同样) 3)建立持久化类: 1>保存方法(与8同样) 2>获取方法(与8同样) 3>删除方法(与8同样) ...
- json数据字典,以及数据在下拉框中显示
建立person_vocation.json数据字典文件,内容: [ {"id":1,"disabled":false,"selected" ...
- h5调用手机前后摄像头,拍照
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="pacam.aspx.cs& ...