码迷,mamicode.com
首页 > 其他好文 > 详细

POJ3450题解——暴力orKMP

时间:2019-11-07 09:41:27      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:sign   tac   bec   contains   ges   containe   rpo   most   complete   

题目链接: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]=\0;
            }
            else if(ansl==max)
            {
                string temp;
                temp=s[0].substr(i,max);temp[max]=\0;
                if(temp<ans)
                {
                    ans=temp;ans[ansl]=\0;
                }
            }
        }
        if(ansl)cout<<ans<<endl;
        else cout<<"IDENTITY LOST"<<endl;
    }
    return 0;
}
KMP

 

POJ3450题解——暴力orKMP

标签:sign   tac   bec   contains   ges   containe   rpo   most   complete   

原文地址:https://www.cnblogs.com/Mingusu/p/11809864.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!