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;
    }
  • 相关阅读:
    spring boot 在windows下安装为service(转)
    win7安装Anaconda+TensorFlow+配置PyCharm(转)
    IDEA中Spring boot配置热部署无效问题解决方式(转)
    WEB后台--基于Token的WEB后台登录认证机制(转)
    selenium之使用chrome浏览器测试(附chromedriver与chrome的对应关系表)(转)
    开源巨献:Google最热门60款开源项目(转)
    反射获取一个方法中的参数名(不是类型)(转)
    ELK原理与介绍(转)
    Linux下Redis的安装和部署(转)
    webpack+ES6+less开发环境搭建(转)
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5231936.html
Copyright © 2011-2022 走看看