N - Corporate Identity

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;
}
时间: 2024-09-30 12:48:01

N - Corporate Identity的相关文章

hdu2328 Corporate Identity 扩展KMP

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

POJ 3450 Corporate Identity KMP题解

本题要求求一组字符串的最长公共子串,其实是灵活运用KMP快速求最长前缀. 注意肯爹的题意:要求按照字典顺序输出. 还有要提醒的就是:有人也是用KMP来解这道题,但是很多人都把KMP当成暴力法来用了,没有真正处理好细节,发挥KMP的作用.而通常这些人都大喊什么暴力法可以解决本题,没错,的确暴力法是可以解决本题的,本题的数据不大,但是请不要把KMP挂上去,然后写成暴力法了,那样会误导多少后来人啊. 建议可以主要参考我的getLongestPre这个函数,看看是如何计算最长前缀的. 怎么判断你是否把本

POJ-3450 Corporate Identity (KMP+后缀数组)

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 a

(KMP 暴力)Corporate Identity -- hdu -- 2328

http://acm.hdu.edu.cn/showproblem.php?pid=2328 Corporate Identity Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 698    Accepted Submission(s): 281 Problem Description Beside other services, AC

POJ 题目3450 Corporate Identity(KMP 暴力)

Corporate Identity 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

HDU - 2328 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 a

POJ 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 a

hdu2328 Corporate Identity【string库使用】【暴力】【KMP】

Corporate Identity Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3308    Accepted Submission(s): 1228 Problem Description Beside other services, ACM helps companies to clearly state their "cor

hdu 2328 Corporate Identity(kmp)

Problem 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 recentl