【poj 3450 Corporate Identity 】 KMP(暴力)

C - Corporate Identity
Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u

Submit

Status

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

题解:求n个串的最长连续公共子串。
思路:1.后缀数组。。。可惜目前不会;
  2. 枚举长度,各个KMP一下;
*优化
1.最长连续公共子串可由长度最小串枚举;
2.长度倒枚举,符合break;
3.*可以倒长度+二分枚举;

1.KMP模板:

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
int n;
char s[4005][205];
int nex[205];
char c[205];
void get_nex(char s[])
{
    int j=-1;
    nex[0]=-1;
    int len=strlen(s);
    for(int i=0;i<len;i++)
    {
        while(j!=-1&&s[j+1]!=s[i]) j=nex[j];
        if(s[i]==s[j+1]&&i!=0)   j++;
        nex[i]=j;
    }
}
bool KMP(char s[],char t[])
{            
    int j=-1;
    int len=strlen(s);
    int ok=strlen(t);
    for(int i=0;i<len;i++)
    {   
        while(j!=-1&&t[j+1]!=s[i])
        j=nex[j];
        if(s[i]==t[j+1])    //!!!
        j++;
        if(j==ok-1) return true;
    }
    return false;
}

2.题解:

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
int n;
char s[4005][205];
int nex[205];
char c[205];
void get_nex(char s[])
{
    int j=-1;
    nex[0]=-1;
    int len=strlen(s);
    for(int i=0;i<len;i++)
    {
        while(j!=-1&&s[j+1]!=s[i]) j=nex[j];
        if(s[i]==s[j+1]&&i!=0)   j++;
        nex[i]=j;
    }
}
bool KMP(char s[],char t[])
{            
    int j=-1;
    int len=strlen(s);
    int ok=strlen(t);
    for(int i=0;i<len;i++)
    {   
        while(j!=-1&&t[j+1]!=s[i])
        j=nex[j];
        if(s[i]==t[j+1])    //!!!
        j++;
        if(j==ok-1) return true;
    }
    return false;
}
int main()
{   
    while(scanf("%d",&n))               
    {
        if(n==0) return 0; 
        int now=0;
        int tmp=205;
        for(int i=1;i<=n;i++)  
        {
            scanf("%s",s[i]);
            if(tmp>strlen(s[i]))
            {
                tmp=strlen(s[i]);
                now=i;
            }
        }
        char ans[205]="";
        int flag=0;
        for(int len=tmp;len>=1;len--)
        {
            for(int i=0;i<=tmp-len;i++)
            {
                strncpy(c,s[now]+i,len);    
                c[len]='\0';
                get_nex(c);
                int j=1;
                for(j=1;j<=n;j++)
                {
                    if(j==now) continue; 
                    if(!KMP(s[j],c))
                    {   
                        j--;
                        break;
                    }  
                }
                if(j==n+1) 
                {
                    flag=1;
                    if(strcmp(c,ans)<0||!strlen(ans))
                    strncpy(ans,c,strlen(c));   
                }
            }   
            if(flag) break;         
        } 
        if(flag==0)
        printf("IDENTITY LOST\n");
        else 
        printf("%s\n",ans);
    }       
}

你可能感兴趣的:(KMP,poj)