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;
    }
  • 相关阅读:
    java生成pdf文字水印和图片水印
    el-date-picker设置可选范围picker-options需要注意的事项,要不然可能会报undefined的错误
    Invalid prop: type check failed for prop "value". Expected String, Number, got Boolean with value false.
    el-table去掉最外层的边框线
    工业物联网之设备云控3 QuartzNet任务调度程序
    工业物联网之设备云控4 管理平台
    工业物联网之设备云控1 技术方案
    C# NModbus4实现PLC数据获取(参考HslCommunication)
    工业物联网之设备云控5 对接流程
    Mongdb数据备份和还原
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5231936.html
Copyright © 2011-2022 走看看