POJ

题目链接:http://poj.org/problem?id=3450

Corporate Identity
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 8549   Accepted: 2856

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

Source

 
题目大意:输入n,代表有n个串,问你m个串中最长公共子串是什么,如果有长度相同的,输出字典序最小的
思路:也算是KMP的裸题了,但是注意一下这题要有一个优化操作,找出长度最短的串,以这个串为基础寻找是否有子串,还有值得一提的是:那个C++加速的东西好像没有一点用,一直超时,改成c的
输入输出才过了,就因为这个卡了好几天,难受。。。
看代码1:
#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1000;
const int maxn=4e3+10;
const int maxk=5e3+10;
const int maxx=1e4+10;
const ll maxe=1000+10;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
char a[maxn][210];
int next[maxn];
void cal_next(char s[])
{
    int len=strlen(s);
    int k=-1;
    next[0]=-1;
    for(int i=1;i<len;i++)
    {
        while(k>-1&&s[k+1]!=s[i])
        {
            k=next[k];
        }
        if(s[k+1]==s[i]) k++;
        next[i]=k;
    }
}
bool kmp(char x[],char y[])
{
    int k=-1;
    int len1=strlen(x);
    int len2=strlen(y);
    for(int i=0;i<len1;i++)
    {
        while(k>-1&&y[k+1]!=x[i])
        {
            k=next[k];
        }
        if(x[i]==y[k+1]) k++;
        if(k==len2-1) return true;
    }
    return false;
}
int main()
{
    //ios::sync_with_stdio(false);
    int n,flag=0;
    while(scanf("%d",&n)!=EOF)
    {
        getchar();
        if(n==0) break;
        char ans[210]="",temp[210];
        scanf("%s",a[0]);
        getchar();
        strcpy(temp,a[0]);//这里不用自己加'',因为a[0]本身就有''的,直接复制过去了
        for(int i=1;i<n;i++)
        {
            scanf("%s",a[i]);
            getchar();
            if(strlen(a[i])<strlen(temp))
            {
                strcpy(temp,a[i]);
            }
        }
        int len=strlen(temp);
        for(int i=0;i<len;i++)//起点
        {
            for(int j=1;i+j<=len;j++)//长度
            {
                char op[210];
                strncpy(op,temp+i,j);//不会在结尾给你自动加''
                op[j]='';//注意这里手动加一个'',因为你只是得到了那几个字符,并没有得到''
                cal_next(op);
                for(int k=0;k<n;k++)
                {
                    flag=0;
                    if(!kmp(a[k],op))
                    {
                        flag=1;
                        break;
                    }
                }
                if(flag==1)//有一个找不到就不用遍历以该起点长度更大的子串了,因为肯定也会找不到
                    break;
                else
                {
                    if(strlen(op)>strlen(ans)) strcpy(ans,op);
                    else if(strlen(op)==strlen(ans)&&strcmp(ans,op)>0)
                    {
                        strcpy(ans,op);
                    }
                }
            }
        }
        if(strlen(ans)==0)
            printf("IDENTITY LOST
");
        else
            printf("%s
",ans);
    }
    return 0;
}

看代码2:

#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1000;
const int maxn=5e3+10;
const int maxk=5e3+10;
const int maxx=1e4+10;
const ll maxe=1000+10;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
char a[maxn][210];
int next[maxn];
void cal_next(char op[])
{
    int k=-1;
    next[0]=-1;
    int len=strlen(op);
    for(int i=1;i<len;i++)
    {
        while(k>-1&&op[k+1]!=op[i])
        {
            k=next[k];
        }
        if(op[k+1]==op[i]) k++;
        next[i]=k;
    }
    return ;
}
bool kmp(char x[],char y[])
{
    int k=-1;
    int len1=strlen(x);
    int len2=strlen(y);
    for(int i=0;i<len2;i++)
    {
        while(k>-1&&x[k+1]!=y[i])
        {
            k=next[k];
        }
        if(x[k+1]==y[i]) k++;
        if(k==len1-1) return true;
    }
    return false;
}
int main()
{
    int n,flag;
    char b[210],temp[210];
    while(scanf("%d",&n)!=EOF)
    {
        int minn=10000;
        if(n==0) break;
        getchar();
        char ans[210]="";
        for(int i=0;i<n;i++)
        {
            scanf("%s",a[i]);
            int len=strlen(a[i]);
            if(len<minn)
            {
                minn=len;
                strcpy(b,a[i]);
            }
            getchar();
        }
        for(int i=1;i<=minn;i++)//长度
        {
            for(int j=0;j+i<=minn;j++)//起点
            {
                flag=0;
                strncpy(temp,b+j,i);
                temp[i]='';
                
                cal_next(temp);
                for(int k=0;k<n;k++)
                {
                    if(!kmp(temp,a[k]))
                    {
                        flag=1;
                        break;
                    }
                }
                if(flag==0)
                {
                    if(strlen(temp)>strlen(ans)) strcpy(ans,temp);
                    else if(strlen(temp)==strlen(ans)&&strcmp(ans,temp)>0) strcpy(ans,temp);
                }
            }
        }
        if(strlen(ans)==0) printf("IDENTITY LOST
");
        else
        {
            printf("%s
",ans);
        }
    }
}
当初的梦想实现了吗,事到如今只好放弃吗~
原文地址:https://www.cnblogs.com/caijiaming/p/9718603.html