zoukankan      html  css  js  c++  java
  • POJ2217(最长公共子串)

    Secretary
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 992   Accepted: 408

    Description

    The basic condition of success of a political party, it is the good Election Programme. PSOS know about it, so they entrust the top secretary Juliet with this task. Because she wanted to make her work easier, she used her charm to talk round her friend Romeo to help her. Romeo is assistent of another political party and he was writing the programme some time ago. While writing the Programme for Juliet, he used some parts of his previous programme. When he gave the finished Programme to Juliet, they recognized that both programmes are too similar and that someone could notice it. They need to determine the longest part of text which is common to both programmes.

    Input

    At the first line there is a positive integer N stating the number of assignments to follow. Each assignment consists of exactly two lines of text, each of them contains at most 10000 characters. The end-of-line character is not considered to be a part of the text.

    Output

    Print a single line of text for each assignment. The line should contain the sentence "Nejdelsi spolecny retezec ma delku X." (The longest common part of text has X characters). Replace X with the length of the longest common substring of both texts.

    Sample Input

    2
    Tady nejsou zadni mimozemstani.
    Lide tady take nejsou.
    Ja do lesa nepojedu.
    V sobotu pojedeme na vylet.

    Sample Output

    Nejdelsi spolecny retezec ma delku 7.
    Nejdelsi spolecny retezec ma delku 5.


    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int MAXN=20005;
    char buf[MAXN];
    int rnk[MAXN];
    int tmp[MAXN];
    int sa[MAXN];
    int lcp[MAXN];
    int len,k;
    
    bool comp(int i,int j)
    {
        if(rnk[i]!=rnk[j])    return rnk[i]<rnk[j];
        else
        {
            int ri=(i+k<=len)?rnk[i+k]:-1;
            int rj=(j+k<=len)?rnk[j+k]:-1;
            return ri<rj;
        }
    }    
    
    void getsa()
    {
        memset(rnk,0,sizeof(rnk));
        memset(tmp,0,sizeof(tmp));
        memset(sa,0,sizeof(sa));
        len=strlen(buf);
        
        for(int i=0;i<len;i++)
        {
            sa[i]=i;
            rnk[i]=buf[i]-'a';
        }
        sa[len]=len;
        rnk[len]=-1;
        
        for(k=1;k<=len;k*=2)
        {
            sort(sa,sa+len+1,comp);
            
            tmp[sa[0]]=0;
            for(int i=1;i<=len;i++)
            {
                tmp[sa[i]]=tmp[sa[i-1]]+(comp(sa[i-1],sa[i])?1:0);
            }
            
            for(int i=0;i<=len;i++)
            {
                rnk[i]=tmp[i];    
            }
        }
    }
    
    void getlcp()
    {
        getsa();
        
        memset(rnk,0,sizeof(rnk));
        memset(lcp,0,sizeof(lcp));
        for(int i=0;i<=len;i++)
        {
            rnk[sa[i]]=i;
        }
        
        int h=0;
        lcp[0]=0;
        for(int i=0;i<len;i++)
        {
            int j=sa[rnk[i]-1];
            if(h>0)    h--;
            for(;j+h<len&&i+h<len;h++)
                if(buf[j+h]!=buf[i+h])    break;
                
            lcp[rnk[i]-1]=h;
        }
    
    }
    
    int T;
    char ss[MAXN];
    int main()
    {
        scanf("%d",&T);
        getchar();
        while(T--)
        {
            memset(buf,0,sizeof(buf));
            memset(ss,0,sizeof(ss));
            gets(buf);
            int l=strlen(buf);
            buf[l]='$';
            gets(ss);
            strcat(buf,ss);
            getlcp();
            
            int ans=0;
            for(int i=0;i<len;i++)
                if((sa[i]<l)!=(sa[i+1]<l))
                    ans=max(ans,lcp[i]);
            printf("Nejdelsi spolecny retezec ma delku %d.
    ",ans);
            
        }
        
        return 0;
    }
  • 相关阅读:
    隐藏TabControl的标签 上海
    最近写的一个存储过程 上海
    DBUS 介绍 上海
    存储过程 几个小例子 上海
    C# BHO 上海
    EXEC和sp_executesql的区别 上海
    office文档转换成mht文档(准备、原理篇) 上海
    Dictionary 排序 上海
    .Net 开源资源 上海
    Geant4.9.5.p01 in ubuntu12.04 OpenGL driver.
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5231936.html
Copyright © 2011-2022 走看看