POJ 3450--Corporate Identity【KMP && 暴力】

Corporate Identity

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 5696   Accepted: 2075

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

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char str[4010][210];
int next[210];

void getnext(char *s1){
    int j = 0, k = -1;
    int len = strlen(s1);
    next[0] = -1;
    while(j < len){
        if(k == -1 || s1[j] == s1[k]){
            ++j;
            ++k;
            next[j] = k;
        }
        else k = next[k];
    }
    return ;
}

bool kmp(char *s1, char *s2){
    int len1 = strlen(s2);
    int len2 = strlen(s1);
    getnext(s1);
    int i = 0, j = 0;
    while(i < len1){
        if(j == -1 || s1[j] == s2[i]){
            ++i;
            ++j;
        }
        else j=next[j];
        if(j == len2)
            return true;
    }
    return false;
}

int main(){
    int n;
    while(scanf("%d", &n), n){
        for(int i = 0 ; i < n; ++i)
            scanf("%s", str[i]);
        char temp[220];
        char sum[220] = "";
        int len = strlen(str[0]);
        for(int i = 0; i < len; ++i){
            int ans  = 0;
            for(int j = i; j < len; ++j){
                temp[ans++] = str[0][j];
                temp[ans] = '\0';
                int flag = 1;
                for(int k = 0 ; k < n; ++k){
                    if(!kmp(temp, str[k])){
                        flag = 0;
                        break;
                    }
                }
                if(flag){
                    if(strlen(temp) > strlen(sum))
                        memcpy(sum, temp, sizeof(temp));
                    else if(strlen(temp) == strlen(sum) && strcmp(temp, sum) < 0)
                        memcpy(sum, temp, sizeof(temp));
                }
            }
        }
        if(strlen(sum) == 0)
            printf("IDENTITY LOST\n");
        else
            printf("%s\n", sum);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-26 19:17:26

POJ 3450--Corporate Identity【KMP && 暴力】的相关文章

POJ 3450 Corporate Identity KMP题解

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

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

POJ 3450 Corporate Identity(KMP)

[题目链接] http://poj.org/problem?id=3450 [题目大意] 求k个字符串的最长公共子串,如果有多个答案,则输出字典序最小的. [题解] 我们对第一个串的每一个后缀和其余所有串做kmp,取匹配最小值的最大值就是答案. [代码] #include <cstring> #include <cstdio> #include <algorithm> const int N=4050,M=210; using namespace std; int nx

poj 3080 Blue Jeans (kmp暴力)

# include <stdio.h> # include <algorithm> # include <cstring> using namespace std; int next[100]; char pat[100]; char a[100][100]; int ma; int lenp; int n; void Getnext() { int i=0,j=-1; next[0]=-1; while(i<=lenp) { if(j==-1||pat[j]==

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

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

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

POJ 3080--Blue Jeans【KMP &amp;&amp; 暴力枚举】

Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14316   Accepted: 6374 Description The Genographic Project is a research partnership between IBM and The National Geographic Society that is analyzing DNA from hundreds of thousa

poj 3518 Corporate Identity 后缀数组-&gt;多字符串最长相同连续子串

题目链接 题意:输入N(2 <= N <= 4000)个长度不超过200的字符串,输出字典序最小的最长公共连续子串; 思路:将所有的字符串中间加上分隔符,注:分隔符只需要和输入的字符不同,且各自不同即可,没有必要是最小的字符; 连接后缀数组求解出height之后二分长度,由于height是根据sa数组建立的,所以前面符合的就是字典序最小的,直接找到就停止即可; ps: 把之前的模板简化了下,A题才是关键; #include<iostream> #include<cstdio&

字符串截取模板 &amp;&amp; POJ 3450、3080 ( 暴力枚举子串 &amp;&amp; KMP匹配 )

//截取字符串 ch 的 st~en 这一段子串返回子串的首地址 //注意用完需要根据需要最后free()掉 char* substring(char* ch,int st,int en) { int length = en - st + 1; char* pch=ch; char* subch=(char*)malloc(length+1); pch=pch+st; for(int i=0;i<length;i++) subch[i]=*(pch++); subch[length]='\0';