zoukankan      html  css  js  c++  java
  • poj 3450 Corporate Identity

    Description

    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


    求多个字符串的最长公共子串

    这里是作死写SA
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #define MN 1000003
    using namespace std;
    
    int n,m,nm,nnm;
    char s1[MN];
    int s[MN],a[MN];
    int v[MN],sa[MN],q[MN],rank[MN],h[MN],mmh=0,len,nu[MN];
    bool w[10001];
    inline void gr(int x){
        rank[sa[1]]=1;
        for (int i=2;i<=n;i++) rank[sa[i]]=(s[sa[i]]==s[sa[i-1]]&&s[sa[i]+x]==s[sa[i-1]+x])?rank[sa[i-1]]:rank[sa[i-1]]+1;
        for (int i=1;i<=n;i++) s[i]=rank[i];
    }
    inline void gv(){memset(v,0,sizeof(v));for (int i=1;i<=n;i++) v[s[i]]++;for (int i=1;i<=2e5;i++)v[i]+=v[i-1];}
    inline void gsa(){
        gv();for (int i=n;i>=1;i--) sa[v[s[i]]--]=i;gr(0);
        for (int i=1;i<n;i<<=1){
            gv();for (int j=n;j>=1;j--) if (sa[j]>i) q[v[s[sa[j]-i]]--]=sa[j]-i;
            for (int j=n-i+1;j<=n;j++) q[v[s[j]]--]=j;
            for (int j=1;j<=n;j++) sa[j]=q[j];gr(i);
            if (rank[sa[n]]==n) return;
        }
    }
    inline void gh(){for (int i=1,k=0,j;i<=n;h[rank[i++]]=k) for (k?k--:0,j=sa[rank[i]-1];a[i+k]==a[j+k]&&i+k<=n&&j+k<=n;k++);}
    int main(){
        scanf("%d",&n);
        while(n){
            nm=0;
            for (int i=1;i<=n;i++){
                scanf("%s",s1);
                m=strlen(s1);
                for (int j=0;j<m;j++) a[++nm]=s1[j]-'a',nu[nm]=i;a[++nm]=26+i;
            }
            nnm=n;
            n=nm;
            for (int i=1;i<=nm;i++) s[i]=a[i];
            gsa();gh();
            int l=0,r=n,mid,bo=1,i,j,k,mmh,pos;
            memset(w,0,sizeof(w));
            while(l<r){
                mid=(l+r+1)>>1;
                for (i=1,j,k=2;i<=n;i=k++){
                    mmh=0;
                    while (h[k]>=mid&&k<=n) k++;
                    for (j=i;j<k;j++) if (!w[nu[sa[j]]]&&nu[sa[j]]) mmh++,w[nu[sa[j]]]=1;
                    for (j=i;j<k;j++) w[nu[sa[j]]]=0;
                    if (mmh==nnm) break;
                }
                if (i<=n) l=mid;else r=mid-1;
            }
            if (l==0) printf("IDENTITY LOST
    ");else{
                pos=0;
                for (i=1,j,k=2;i<=n;i=k++){
                    memset(w,0,sizeof(w));mmh=0;
                    while (h[k]>=l&&k<=n) k++;
                    for (j=i;j<k;j++) if (!w[nu[sa[j]]]&&nu[sa[j]]) mmh++,w[nu[sa[j]]]=1;
                    if (mmh==nnm){
                        pos=sa[i];
                        break;
                    }
                }
                for (int j=0;j<l;j++) putchar(a[pos+j]+'a');putchar('
    ');
            }
            scanf("%d",&n);
        }
    }
    7140K 1938MS G++ 2293B

    然后是KMP

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    
    int n,m,ne[201];
    char s[4000][200];
    inline bool kmp(int l,int r,int k){
        int i,j;
        for (i=2,j=0;i<=r;i++){
            while (j&&s[1][j+l]!=s[1][i+l-1]) j=ne[j];
            if (s[1][j+l]==s[1][i+l-1]) j++;
            ne[i]=j;
        }
        int n=strlen(s[k]);
        for (i=0,j=0;i<n&&j<r;)
        if (s[1][j+l]==s[k][i]) i++,j++;else
        if (j==0) i++;else j=ne[j];
        if (j==r) return 0;else return 1;
    }
    int main(){
        int i,j,k,l,r,mid,mmh,an;
        scanf("%d",&n);
        while (n){
            for (i=1;i<=n;i++){
                scanf("%s",s[i]);
                if (strlen(s[i])<strlen(s[1])) swap(s[i],s[1]);
            }
            m=strlen(s[1]);
            l=0;r=m;
            while (l<r){
                mid=l+r+1>>1;
                for (i=0;i<m-mid+1;i++){
                    for (k=2;k<=n;k++) if (kmp(i,mid,k)) break;
                    if (k>n) break;
                }
                if (i==m-mid+1) r=mid-1;else l=mid;
            }
            mmh=-1;
            if (l==0) printf("IDENTITY LOST
    ");else{
                for (i=0;i<m-l+1;i++){
                    for (k=2;k<=n;k++) if (kmp(i,l,k)) break;
                    if (k<=n) continue;
                    if (mmh==-1) mmh=i;else{
                        for (j=0;j<l;j++)
                        if (s[1][i+j]<s[1][mmh+j]){
                            mmh=i;
                            break;
                        }else if (s[1][i+j]>s[1][mmh+j]) break;
                    }
                }
                for (int j=0;j<l;j++) putchar(s[1][mmh+j]);putchar('
    ');
            }
            scanf("%d",&n);
        }
    }
    584K 125MS G++ 1203B
  • 相关阅读:
    keras.Sequential.compile(loss='目标函数 ', optimizer='adam', metrics=['accuracy'])
    Linux下安装tensorflow
    【拿来主义】Shell 中的4种引字符号:单引号、双引号、反引号、反斜杠的用法
    ResourceBundle类:读取配置文件
    Linux三剑客之——awk用法入门
    Javascript学习笔记-基本概念-语句
    Javascript学习笔记-基本概念-操作符
    Javascript学习笔记-基本概念-语法、关键字和保留字、变量
    django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.3 or newer is required; you have 0.7.11
    当在安装包过程中出现python setup.py build_ext --pg-config /path/to/pg_config build ... or with t
  • 原文地址:https://www.cnblogs.com/Enceladus/p/5473900.html
Copyright © 2011-2022 走看看