码迷,mamicode.com
首页 > 其他好文 > 详细

N - Corporate Identity

时间:2017-04-08 14:49:52      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:existing   service   therefore   getch   ade   tmp   name   ora   integer   

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.

InputThe 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.OutputFor 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<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include <sstream>
#include<string>
#include<cstring>
#include<list>
using namespace std;
#define MAXN 202
#define INF 0x3f3f3f3f
#define N 4002
typedef long long LL;
/*
枚举第一个串的所有子串 找到就停止
*/
char s[N][MAXN];
int Next[MAXN];
void kmp_pre(char t[])
{
    int m = strlen(t);
    int j,k;
    j = 0;k = Next[0] = -1;
    while(j<m)
    {
        if(k==-1||t[j]==t[k])
            Next[++j] = ++k;
        else
            k = Next[k];
    }
}
bool KMP(char t[],char s[])
{
    int n = strlen(s),m=strlen(t);
    int i,j;
    j = 0;
    for(i=0;i<n;i++)
    {
        while(j>0&&s[i]!=t[j])
            j = Next[j];
        if(s[i]==t[j])
            j++;
        if(j>=m)
            return true;
    }
    return false;
}
int main()
{
    int n;
    while(scanf("%d",&n),n)
    {
        int len = INF,k=-1;
        for(int i=0;i<n;i++)
        {
            scanf("%s",s[i]);
            if(len>strlen(s[i]))
            {
                len = strlen(s[i]);
                k = i;
            }
        }
        bool f = false;
        char ans[MAXN];
        for(int l=len;l>0;l--)
        {
            for(int i=0;i+l<=len;i++)
            {
                char t[MAXN] = {0};
                strncpy(t,s[k]+i,l);
                kmp_pre(t);
                int j;
                //cout<<t<<endl;
                for(j=0;j<n;j++)
                {
                    if(j==k) continue;
                    if(KMP(t,s[j]))
                        continue;
                    else
                        break;
                }
                if(j==n)
                {
                    if(!f)
                    {
                        strcpy(ans,t);
                        f = true;
                    }
                    else
                    {
                        if(strcmp(ans,t)>0)
                            strcpy(ans,t);
                    }
                }
                if(f&&(i+l==len))
                {
                    printf("%s\n",ans);
                    i = INF;
                    l = -1;
                    break;
                }
            }
        }
        if(!f)
            printf("IDENTITY LOST\n");
    }
}

 只需要枚举所有后缀,然后在其他串中找最长相同前缀即可

#include <stdio.h>
#include <string.h>

const int MAX_N = 4001;
const int MAX_CHS = 201;
const int ARR_SIZE = 26;
int N;
char res[MAX_CHS], dict[MAX_N][MAX_CHS];
short next[MAX_CHS];

inline int min(int a, int b) { return a < b ? a : b; }
inline int max(int a, int b) { return a > b ? a : b; }

void getNext(char *chs, int len)
{
    memset(next, 0, sizeof(short)*len);
    for (int i = 1, j = 0; i < len; )
    {
        if (chs[i] == chs[j]) next[i++] = ++j;
        else if (j > 0) j = next[j-1];
        else i++;
    }
}

int getLogestPre(char *chs, int len)
{
    getNext(chs, len);
    for (int i = 1; i < N; i++)
    {
        char *p = dict[i];
        int j = 0, tmp = 0;
        for (; *p && j < len; )
        {
            if (*p == chs[j])
            {
                p++, j++;
                tmp = max(tmp, j);
            }
            else if (j > 0) j = next[j-1];
            else p++;
        }
        len = tmp;
    }
    return len;
}

int main()
{
    while (scanf("%d", &N) && N)
    {
        getchar();    // don‘t forget to get rid of ‘\‘
        for (int i = 0; i < N; i++)
        {
            gets(dict[i]);
        }
        int len = strlen(dict[0]), ans = 0, pos = 0;
        for (int i = 0; i < len; i++)
        {
            int tmp = getLogestPre(dict[0]+i, len-i);
            if (tmp >= ans)
            {
                if (tmp > ans)
                {
                    ans = tmp;
                    pos = i;
                }
                else
                {
                    bool smaller = true;
                    for (int t = 0; t < ans; t++)
                    {
                        if (dict[0][pos+t] > dict[0][i+t]) break;
                        else if (dict[0][pos+t] < dict[0][i+t])
                        {
                            smaller = false;
                            break;
                        }
                    }
                    if (smaller) pos = i;
                }
            }
        }
        if (ans)
        {
            for (int i = 0; i < ans; i++)
            {
                putchar(dict[0][pos+i]);
            }
            putchar(\n);
        }
        else puts("IDENTITY LOST");
    }
    return 0;
}

 

N - Corporate Identity

标签:existing   service   therefore   getch   ade   tmp   name   ora   integer   

原文地址:http://www.cnblogs.com/joeylee97/p/6681758.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!