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,否则最长的公共子串.有多组解时输出字典序最 ...
随机推荐
- 一片非常有趣的文章 三分钟读懂TT猫分布式、微服务和集群之路
原文http://www.cnblogs.com/smallSevens/p/7501932.html#3782600 三分钟读懂TT猫分布式.微服务和集群之路 针对新手入门的普及,有过大型网站技 ...
- 1113: [视频]树形动态规划(TreeDP)8:树(tree)(树形dp状态设计总结)
根据最近做的几道树形dp题总结一下规律.(从这篇往前到洛谷 P1352 ) 这几道题都是在一颗树上,然后要让整棵树的节点或边 满足一种状态.然后点可以影响到相邻点的这种状态 然后求最小次数 那么要从两 ...
- [置顶]
Docker学习总结(7)——云端基于Docker的微服务与持续交付实践
本文根据[2016 全球运维大会•深圳站]现场演讲嘉宾分享内容整理而成 讲师简介 易立 毕业于北京大学,获得学士学位和硕士学位:目前负责阿里云容器技术相关的产品的研发工作. 加入阿里之前,曾在IBM中 ...
- 简单编写makefile文件,实现GCC4.9编译项目,增加boost库測试等等。。
一.须要用到的hw.cpp hw.h funtest.cpp funtest.h makefile 几个測试文件 1.hw.cpp代码例如以下: #include "hw.h" # ...
- java webproject中logback换配置文件的路径
本人小站点: http://51kxd.com/ 欢迎大家不开心的时候訪问訪问,调节一下心情 web.xml中配置: <!-- windows logback.xml文件跟web容器(比 ...
- hdoj 1719 Friend
Friend Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- JNI中java类型的简写
在JNI中,当我们使用GetFieldID/GetStaticFieldID或GetMethodID/GetStaticMethodID及定义JNINativeMethod等时,我们需要表示成员变 ...
- hdoj--1869--六度分离(floyd)
六度分离 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- Word histogram
Here is a program that reads a file and builds a histogram of the words in the file: process_file lo ...
- 防雪崩利器:熔断器 Hystrix 的原理与使用--转
原文地址:https://segmentfault.com/a/1190000005988895 前言 分布式系统中经常会出现某个基础服务不可用造成整个系统不可用的情况, 这种现象被称为服务雪崩效应. ...