POJ 题目3450 Corporate Identity(KMP 暴力)
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 5493 | Accepted: 2015 |
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
Source
field=source&key=CTU+Open+2007">CTU Open 2007
- #include<stdio.h>
- #include<string.h>
- char str[4040][220];
- int next[4040];
- void getnext(char *s)
- {
- int len=strlen(s);
- int j=0,k=-1;
- next[0]=-1;
- while(j<=len)
- {
- if(k==-1||s[k]==s[j])
- {
- k++;
- j++;
- next[j]=k;
- }
- else
- k=next[k];
- }
- }
- int kmp(char *a,char *b)
- {
- int i,j;
- i=j=0;
- int n=strlen(a);
- int m=strlen(b);
- getnext(b);
- while(i<n)
- {
- if(j==-1||a[i]==b[j])
- {
- i++;
- j++;
- }
- else
- j=next[j];
- if(j==m)
- return 1;
- }
- return 0;
- }
- int main()
- {
- int n;
- while(scanf("%d",&n),n)
- {
- int i,j,k;
- for(i=0;i<n;i++)
- {
- scanf("%s",str[i]);
- }
- int len=strlen(str[0]);
- char temp[220],ans[220]="";
- for(i=0;i<len;i++)
- {
- int cnt=0;
- for(j=i;j<len;j++)
- {
- temp[cnt++]=str[0][j];
- temp[cnt]='\0';
- // getnext(temp);
- int flag=0;
- for(k=1;k<n;k++)
- {
- if(!kmp(str[k],temp))
- {
- flag=1;
- break;
- }
- }
- if(!flag)
- {
- if(strlen(temp)>strlen(ans)||(strlen(temp)==strlen(ans)&&strcmp(temp,ans)<0))
- memcpy(ans,temp,sizeof(temp));
- }
- }
- }
- if(strlen(ans)==0)
- printf("IDENTITY LOST\n");
- else
- printf("%s\n",ans);
- }
- }
POJ 题目3450 Corporate Identity(KMP 暴力)的更多相关文章
- POJ 3450 Corporate Identity KMP解决问题的方法
这个问题,需要一组字符串求最长公共子,其实灵活运用KMP高速寻求最长前缀. 请注意,意大利愿父亲:按照输出词典的顺序的规定. 另外要提醒的是:它也被用来KMP为了解决这个问题,但是很多人认为KMP使用 ...
- POJ 3450 Corporate Identity kmp+最长公共子串
枚举长度最短的字符串的所有子串,再与其他串匹配. #include<cstdio> #include<cstring> #include<algorithm> #i ...
- 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 Corporate Identity(KMP)
[题目链接] http://poj.org/problem?id=3450 [题目大意] 求k个字符串的最长公共子串,如果有多个答案,则输出字典序最小的. [题解] 我们对第一个串的每一个后缀和其余所 ...
- POJ 3450 Corporate Identity (KMP+暴搞)
题意: 给定N个字符串,寻找最长的公共字串,如果长度相同,则输出字典序最小的那个. 找其中一个字符串,枚举它的所有的字串,然后,逐个kmp比较.......相当暴力,可二分优化. #include & ...
- POJ 3450 Corporate Identity (KMP,求公共子串,方法很妙)
http://blog.sina.com.cn/s/blog_74e20d8901010pwp.html我采用的是方法三. 注意:当长度相同时,取字典序最小的. #include <iostre ...
- poj 3450 Corporate Identity
题目链接:http://poj.org/problem?id=3450 题目分类:后缀数组 题意:求n个串的最长公共字串(输出字串) //#include<bits/stdc++.h> # ...
- POJ - 3080 Blue Jeans 【KMP+暴力】(最大公共字串)
<题目链接> 题目大意: 就是求k个长度为60的字符串的最长连续公共子串,2<=k<=10 限制条件: 1. 最长公共串长度小于3输出 no significant co ...
随机推荐
- 【php】 php-fpm 配置见解
来源:php官方文档 Init script setup=== You will probably want to create an init script for your new php-fpm ...
- Python三元表达式和列表生成式
三元表达式 取代 if …… else……的简单表达方式 # 常规写法 x = 1 y = 2 if x>y: print(x) else: print(y) #三元表达式写法 res ...
- DSP中-stack和-heap的作用
-stack 0x00000800-heap 0x00000800 stack - 又称系统栈(system stack),用于: 保存函数调用后的返回地址; ...
- LeetCode(79) Word Search
题目 Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fro ...
- 水题:UVa253-Cube painting
Cube painting We have a machine for painting cubes. It is supplied with three different colors: blue ...
- leetcode刷题——动态规划
知识点 专题-B-动态规划 题目 斐波那契数列 矩阵路径 数组区间 分割整数 最长递增子序列 最大连续子序列和 最长公共子序列 最长回文子序列 最长公共子串 最长回文子串 背包 题解 CS-Notes ...
- 序列化 random模块应用
序列化 我们今天学习下序列化,什么是序列化呢? 将原本的字典.列表等内容转换成一个字符串的过程就叫做序列化. 为什么要有序列化模块: 比如,我们在python代码中计算的一个数据需要给另外一段程序使用 ...
- visual studio 的生成、重新生成、清理功能的说明
生成 生成当前选中的项目,依赖的项目如果已经生成dll,则不生成,直接拷贝过来 重新生成 生成当前选中的项目,依赖的项目也会生成 清理 清除掉生成的dll和相关文件
- C++类指针初始化
上面的代码会打印“A”. C++ 类指针定义的时候没有初始化的时候,居然可以安全的调用类内部的成员函数而不出错. 在网上查了一下: 初始化为NULL的类指针可以安全的调用不涉及类成员变量的类成员函 ...
- 【MVC 1】MVC+EF实体框架—原理解析
导读:在之前,我们学过了三层框架,即:UI.BLL.DAL.我们将页面显示.逻辑处理和数据访问进行分层,避免了一层.两层的混乱.而后,我们又在经典三层的基础上,应用设计模式:外观.抽象工厂+反射,使得 ...