首页 > ACM题库 > HDU-杭电 > hdu 2328 Corporate Identity-KMP-[解题报告]C++
2014
01-05

hdu 2328 Corporate Identity-KMP-[解题报告]C++

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.

输入:

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.

输出:

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.

样例输入:

3
aabbaabb
abbababb
bbbbbabb
2
xyz
abc
0

样例输出:

abb
IDENTITY LOST

题目地址: http://poj.org/problem?id=3450

这道题和刚刚吐槽的POJ3080一样的题目,这是数据范围大了一些,

开始觉得肯定会超时,但是还是在上面修改了一些然后提交了,

果断1A啊~~~~~~~

思路:见上一篇博客  POJ  3080

AC代码: 写的有点乱,不过思路应该很清晰,呵呵

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
using namespace std;

char s[4002][222];
int n,wlen,next[222];
char word[222];
int be;

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

bool kmp(char *text,char *word)
{
    int i=0,j=0,k=0,tlen=strlen(text);
    while(i<tlen)
    {
        if(j==-1||text[i]==word[j])
        {
            i++;    j++;
        }
        else
            j=next[j];
        if(j==wlen)
            return true;
    }
    return false;
}

int main()
{
    int i,j,k,T,be;
    while(scanf("%d",&n)&&n)
    {
        scanf("%s",s[0]);
        int ll=strlen(s[0]);
        char ss[222];
        strcpy(ss,s[0]);
        int ppp=0;
        for(i=1;i<n;i++)
        {
            scanf("%s",s[i]);
            int k=strlen(s[i]);
            if(k<ll)
                strcpy(ss,s[i]),ll=k,ppp=i;
        }
        strcpy(s[ppp],s[0]);
        strcpy(s[0],ss);
        int be=0,len=0;
        int h=0;
        char pp[222]="";
        for(i=1;i<=ll;i++)
        {
            wlen=i;
            int g=0,kkk=0;
            for(j=0;j+i<=ll;j++)
            {
                strncpy(word,s[0]+j,i);
                word[wlen]='\0';
                getnext(word);
                int f=0;
                for(k=1;k<n;k++)
                    if(!kmp(s[k],word))//匹配不成功
                    {
                        f=1;break;
                    }
                if(f==0)//当从j开始的i个匹配成功
                {
                    be=j;   len=i;
                    if(kkk==0||strcmp(pp,word)>0)
                    {
                        kkk=1;strcpy(pp,word);
                    }
                    g=1;
                }
            }
            if(g==0)//当有i个字符的时候都没有匹配
                break;
        }
        if(len==0)
            printf("IDENTITY LOST\n");
        else
        {
            printf("%s\n",pp);
        }
    }
    return 0;
}

解题转自:http://blog.csdn.net/xh_reventon/article/details/9396563


,