POJ3450题解——暴力orKMP

Posted mingusu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ3450题解——暴力orKMP相关的知识,希望对你有一定的参考价值。

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

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.

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<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define pppp cout<<endl;
#define EPS 1e-8
#define LL long long
#define ULL unsigned long long     //1844674407370955161
#define INT_INF 0x3f3f3f3f      //1061109567
#define LL_INF 0x3f3f3f3f3f3f3f3f //4557430888798830399
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
const int dr[]= {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]= {-1, 1, 0, 0, -1, 1, -1, 1};

int t,ansl,next[210];
string s[4010],p,ans;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    while(cin>>t&&t)
    {
        ansl=0;
        for(int i=0;i<t;i++)cin>>s[i];
        for(int i=0;i<s[0].size();i++)
        {
            int l=s[0].size()-i;
            p=s[0].substr(i,l);
            
            next[0]=-1;
            int slen=-1;
            int plen=0;
            while(plen<l)
            {
                if(slen==-1||p[plen]==p[slen])
                {
                    plen++;slen++;
                    if(p[plen]!=p[slen])
                    next[plen]=slen;
                    else
                    next[plen]=next[slen];
                }
                else slen=next[slen];
            }
            
            int snk,max=210;
            for(int j=1;j<t;j++)
            {
                plen=0;slen=0;snk=0;
                while(plen<l&&slen<s[j].size())
                {
                    if(plen==-1||p[plen]==s[j][slen])
                    {
                        plen++;slen++;
                    }
                    else plen=next[plen];
                    if(plen>snk)
                    {
                        snk=plen;
                    }
                }
                if(snk<max)max=snk;
            }
            
            if(max>ansl)
            {
                ansl=max;
                ans=s[0].substr(i,max);
                ans[ansl]=;
            }
            else if(ansl==max)
            {
                string temp;
                temp=s[0].substr(i,max);temp[max]=;
                if(temp<ans)
                {
                    ans=temp;ans[ansl]=;
                }
            }
        }
        if(ansl)cout<<ans<<endl;
        else cout<<"IDENTITY LOST"<<endl;
    }
    return 0;
}
KMP

 

以上是关于POJ3450题解——暴力orKMP的主要内容,如果未能解决你的问题,请参考以下文章

POJ 题目3450 Corporate Identity(KMP 暴力)

POJ_3450_KMP

POJ 3450 Corporate Identity(KMP)

POJ - 3450

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

POJ3080 POJ3450Corporate Identity(广义后缀自动机||后缀数组||KMP)