poj 3450 Corporate Identity
来源:互联网 发布:数据图的类型 编辑:程序博客网 时间:2024/06/05 14:18
http://poj.org/problem?id=3450
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
3aabbaabbabbababbbbbbbabb2xyzabc0
Sample Output
abbIDENTITY LOST
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <cmath>#include <cstdlib>#include <limits>#include <queue>#include <stack>#include <vector>#include <map>using namespace std;#define N 1010#define INF 0x3f3f3f3f#define PI acos (-1.0)#define EPS 1e-8#define met(a, b) memset (a, b, sizeof (a))struct node{ char s[N];}Ans[N];bool cmp (node a, node b){ return (strcmp (a.s, b.s) < 0);//按字典序列排序}char str[N][N], dest[N];int Next[N], flag, n, len;void get_next (char dest[]);int kmp (char s[], char s1[]);void solve ();int main (){ while (scanf ("%d", &n), n) { for (int i=0; i<n; i++) scanf ("%s", str[i]); len = strlen (str[0]); int k = 0; for (int i=len; i>=1; i--) { for (int j=len-i; j>=0; j--) { flag = 0; met (dest, 0); strncpy (dest, str[0]+j, i); get_next (dest); solve (); if (!flag) strcpy (Ans[k++].s, dest); } if (k) break; } if (k) { sort (Ans, Ans+k, cmp); printf ("%s\n", Ans[0].s); } else puts ("IDENTITY LOST"); } return 0;}void get_next (char dest[]){ int i = 0, j = -1; Next[0] = -1; while (dest[i]) { if (j == -1 || dest[i] == dest[j]) Next[++i] = ++j; else j = Next[j]; } return;}int kmp (char s[], char s1[]){ int i = 0, j = 0; int len1 = strlen (s); int len2 = strlen (s1); while (i < len1 && j < len2) { if (j == -1 || s[i] == s1[j]) i++, j++; else j = Next[j]; if (j == len2) return i - len2; } return -1;}void solve (){ for (int i=1; i<n; i++) { if (kmp (str[i], dest) == -1) { flag = 1; return; } } return;}
- poj 3450 Corporate Identity
- POJ-3450-Corporate Identity
- POJ 3450 Corporate Identity
- POJ 3450 Corporate Identity
- poj 3450 Corporate Identity
- POJ 3450-Corporate Identity
- POJ 3450 Corporate Identity
- POJ-3450 Corporate Identity
- poj 3450 Corporate Identity
- POJ 3450 Corporate Identity
- poj 3450 Corporate Identity
- POJ 3450Corporate Identity
- POJ-3450-Corporate Identity(KMP)
- [KMP或者暴力]POJ 3450 Corporate Identity
- POJ 3450 Corporate Identity 暴力枚举+KMP
- POJ 3450Corporate Identity(暴力枚举+KMP)
- 【后缀数组】 POJ 3450 Corporate Identity
- POJ 3450 Corporate Identity KMP题解
- 数据结构--单链表(C语言)
- Ubuntu下安装nvidia显卡驱动
- mysql-binlog
- 弱校联萌十一大决战之强力热身 A. Easy Math (水)
- iOS 音频播放
- poj 3450 Corporate Identity
- SVN版本控制工具的使用
- 第三周——项目4顺序表的应用
- 在一个数组中查找两个重复出现两次的数
- 简单导航页制作--代码
- light oj 1384 - Stream My Contest (最小树形图+二分)
- hihoCoder 1234 Fractal
- js 验证表单 js提交验证类
- 查看ubuntu的版本