poj 3450 Corporate Identity

来源:互联网 发布:数据图的类型 编辑:程序博客网 时间:2024/06/05 14:18

http://poj.org/problem?id=3450

Corporate Identity

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

题意:给你n个的串,求出它们的最长公共子串,如果不存在这个子串,则输出“IDENTITY 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;}


0 0