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;
    }
  • 相关阅读:
    Scott的ASP.net MVC框架系列文章之一
    NHibernate配置的总体流程(转载)
    深入浅出MFC》学习笔记之一
    用javascript实现选择下拉菜单间的数据转移,用javascript实现超强的万年历,javascript弹出窗口后,关闭窗口时不弹出对话框
    深入理解动态库
    C#中读取文件内容
    a id="saveButton" href="#" onclick="javascript:startUpload();"Upload !!/a
    如何调用DLL (基于Visual C++6.0的DLL编程实现)
    MSDN获取网站,www.bd66.com
    应用MFC开发高级应用程序
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5231936.html
Copyright © 2011-2022 走看看