• 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
    ac代码如下:
    //9.32-10.11
    #include<iostream>
    #include<string>
    using namespace std;
    int t;
    string s[4010],ans,p;
    int main()
    {
        while(cin>>t&&t)
        {
            ans="IDENTITY LOST";
            for(int i=0;i<t;i++)cin>>s[i];
            for(int i=1;i<s[0].size();i++)//列举长度 
            {
                for(int j=0;j<=s[0].size()-i;j++)//列举起始地址
                {
                    p=s[0].substr(j,i);
                    bool flag=true;
                    for(int k=1;k<t;k++)//和后面的对比 
                    {
                        if(s[k].find(p)==string::npos)//表示没找到 (不用比了) 
                        {
                            flag=0;break;
                        }
                    }
    //                没找到就换个地址 
                    if(!flag)continue;
                    else  //找到了
                    {
                        if(ans=="IDENTITY LOST")ans=p;
                        else if(ans.size()<p.size())ans=p;
                        else if(ans.size()==p.size())ans=min(p,ans);
                    } 
                } 
            } 
            cout<<ans<<endl;
        }
        return 0;
    }
    暴力枚举法
    #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
  • 相关阅读:
    python爬虫
    绕过CDN查找网站真实IP方法收集
    拖库
    伪静态注入的总结
    国外安全网站信息收集
    python字典去重脚本
    AOP 的利器:ASM 3.0 介绍
    JDK工具
    JVM性能调优监控工具
    DMZ
  • 原文地址:https://www.cnblogs.com/Mingusu/p/11809864.html
Copyright © 2020-2023  润新知