zoukankan      html  css  js  c++  java
  • hdu2328 kmp

    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.

    InputThe 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.OutputFor 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
    题意:和前一题几乎一模一样,只是没有反转的情况(变简单了)
    题解:枚举第一个的子串进行kmp
    #include<map>
    #include<set>
    #include<cmath>
    #include<queue>
    #include<stack>
    #include<vector>
    #include<cstdio>
    #include<iomanip>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define pi acos(-1)
    #define ll long long
    #define mod 10007
    #define ls l,m,rt<<1
    #define rs m+1,r,rt<<1|1
    
    using namespace std;
    
    const double g=10.0,eps=1e-9;
    const int N=4000+5,maxn=(1<<18)-1,inf=0x3f3f3f3f;
    
    int Next[N],slen,plen;
    string a[N],ptr,str;
    
    void getnext()
    {
        int k=-1;
        Next[0]=-1;
        for(int i=1;i<slen;i++)
        {
            while(k>-1&&str[k+1]!=str[i])k=Next[k];
            if(str[k+1]==str[i])k++;
            Next[i]=k;
        }
    }
    bool kmp()
    {
        int k=-1;
        for(int i=0;i<plen;i++)
        {
            while(k>-1&&str[k+1]!=ptr[i])k=Next[k];
            if(str[k+1]==ptr[i])k++;
            if(k==slen-1)return 1;
        }
        return 0;
    }
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
     //   cout<<setiosflags(ios::fixed)<<setprecision(2);
        int n;
        while(cin>>n,n){
            for(int i=0;i<n;i++)cin>>a[i];
            string ans="";
            for(int i=1;i<=a[0].size();i++)
            {
                for(int j=0;j<=a[0].size()-i;j++)
                {
                    str=a[0].substr(j,i);
                    slen=str.size();
                    getnext();
                    bool flag=1;
                    for(int k=1;k<n;k++)
                    {
                        ptr=a[k];
                        plen=a[k].size();
                        if(kmp())continue;
                        flag=0;
                        break;
                    }
                    if(flag)
                    {
                        if(ans.size()<str.size())ans=str;
                        else if(ans.size()==str.size()&&str<ans)ans=str;
                    }
                }
            }
            if(ans!="")cout<<ans<<endl;
            else cout<<"IDENTITY LOST"<<endl;
        }
        return 0;
    }
    View Code
  • 相关阅读:
    博弈论(SG函数):HNOI 2007 分裂游戏
    博弈论(二分图匹配):NOI 2011 兔兔与蛋蛋游戏
    博弈论(男人八题):POJ 1740 A New Stone Game
    动态规划(树形DP):HDU 5834 Magic boy Bi Luo with his excited tree
    杂项(最小表示法):HZOI 2015 Glass Beads
    如何避免死锁
    死锁的四个必要条件
    线程安全和可重入函数之间的区别和联系
    信号量 sem_undo设置
    linux管道的容量和内部组织方式
  • 原文地址:https://www.cnblogs.com/acjiumeng/p/6825013.html
Copyright © 2011-2022 走看看