• 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;
    }
  • 相关阅读:
    linux下oracle启动关闭
    win10安装JDK详细教程
    Spring MVC中用@ResponseBody转json,对json进行处理方法汇总
    js实现横向跑马灯效果
    Oracle的ORA-02292报错:违反完整性约束,已找到子记录
    echarts中legend如何换行
    java中split特殊符号
    Tomcat开启SSL协议支持
    Oracle获取表字段名,字段类型,字段长度,注释
    Oracle根据符合条件的数据循环批量更新
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5231936.html
Copyright © 2020-2023  润新知