zoukankan      html  css  js  c++  java
  • POJ2774 Long Long Message

    Long Long Message
    Time Limit: 4000MS   Memory Limit: 131072K
    Total Submissions: 29589   Accepted: 12018
    Case Time Limit: 1000MS

    Description

    The little cat is majoring in physics in the capital of Byterland. A piece of sad news comes to him these days: his mother is getting ill. Being worried about spending so much on railway tickets (Byterland is such a big country, and he has to spend 16 shours on train to his hometown), he decided only to send SMS with his mother. 

    The little cat lives in an unrich family, so he frequently comes to the mobile service center, to check how much money he has spent on SMS. Yesterday, the computer of service center was broken, and printed two very long messages. The brilliant little cat soon found out: 

    1. All characters in messages are lowercase Latin letters, without punctuations and spaces. 
    2. All SMS has been appended to each other – (i+1)-th SMS comes directly after the i-th one – that is why those two messages are quite long. 
    3. His own SMS has been appended together, but possibly a great many redundancy characters appear leftwards and rightwards due to the broken computer. 
    E.g: if his SMS is “motheriloveyou”, either long message printed by that machine, would possibly be one of “hahamotheriloveyou”, “motheriloveyoureally”, “motheriloveyouornot”, “bbbmotheriloveyouaaa”, etc. 
    4. For these broken issues, the little cat has printed his original text twice (so there appears two very long messages). Even though the original text remains the same in two printed messages, the redundancy characters on both sides would be possibly different. 

    You are given those two very long messages, and you have to output the length of the longest possible original text written by the little cat. 

    Background: 
    The SMS in Byterland mobile service are charging in dollars-per-byte. That is why the little cat is worrying about how long could the longest original text be. 

    Why ask you to write a program? There are four resions: 
    1. The little cat is so busy these days with physics lessons; 
    2. The little cat wants to keep what he said to his mother seceret; 
    3. POJ is such a great Online Judge; 
    4. The little cat wants to earn some money from POJ, and try to persuade his mother to see the doctor :( 

    Input

    Two strings with lowercase letters on two of the input lines individually. Number of characters in each one will never exceed 100000.

    Output

    A single line with a single integer number – what is the maximum length of the original text written by the little cat.

    Sample Input

    yeshowmuchiloveyoumydearmotherreallyicannotbelieveit
    yeaphowmuchiloveyoumydearmother
    

    Sample Output

    27

    Source

    POJ Monthly--2006.03.26,Zeyuan Zhu,"Dedicate to my great beloved mother."
    题意:
      给出两个字符串,求最长公共子串的长度
    分析:
      大家都知道要求两个字符串的最长公共子串就是求任意两后缀的最长公共前缀。因此我们可以利用后缀数组的height数组。先用个分隔符$将两个字符串连接起来,再用后缀数组求出height数组的值,找出一个height值最大并且i与i-1的sa值分别在两串字符中就好.....
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    using namespace std;
    const int N=2e5+10;
    int n,len,ans,maxx,sa[N],tsa[N],rank[N],trank[N],h[N],c[N];
    char s[N];
    void DA(){
        int p;
        memset(c,0,sizeof c);maxx=256;
        for(int i=1;i<=len;i++) c[rank[i]=s[i]]++;
        for(int i=2;i<=maxx;i++) c[i]+=c[i-1];
        for(int i=len;i;i--) sa[c[rank[i]]--]=i;
        trank[sa[1]]=p=1;
        for(int i=2;i<=len;i++){
            if(rank[sa[i]]!=rank[sa[i-1]]) p++;
            trank[sa[i]]=p;
        }
        for(int i=1;i<=len;i++) rank[i]=trank[i];
        for(int k=1;p<len;k<<=1,maxx=p){
            p=0;
            for(int i=len-k+1;i<=len;i++) tsa[++p]=i;
            for(int i=1;i<=len;i++) if(sa[i]>k) tsa[++p]=sa[i]-k;
            memset(c,0,sizeof c);
            for(int i=1;i<=len;i++) trank[i]=rank[tsa[i]];
            for(int i=1;i<=len;i++) c[trank[i]]++;
            for(int i=2;i<=maxx;i++) c[i]+=c[i-1];
            for(int i=len;i;i--) sa[c[trank[i]]--]=tsa[i];
            trank[sa[1]]=p=1;
            for(int i=2;i<=len;i++){
                if(rank[sa[i]]!=rank[sa[i-1]]||rank[sa[i]+k]!=rank[sa[i-1]+k]) p++;
                trank[sa[i]]=p;
            }
            for(int i=1;i<=len;i++) rank[i]=trank[i];
        }
        for(int i=1,k=0;i<=len;i++){
            int j=sa[rank[i]-1];
            while(s[i+k]==s[j+k]) k++;
            h[rank[i]]=k;if(k>0) k--;
        }
    }
    int main(){
        scanf("%s",s+1);len=strlen(s+1);s[++len]='$';n=len+1;
        scanf("%s",s+n);len=strlen(s+1);
        DA();
        for(int i=2;i<=len;i++){//0.5h打完 2h调错 QAQ
            if((sa[i]>=n&&sa[i-1]<n)||(sa[i-1]>=n&&sa[i]<n)){
                ans=max(ans,h[i]);
            }
        }
        printf("%d",ans);
        return 0;
    }
  • 相关阅读:
    个人理财小助手 —— 简介
    我的分页控件(未完,待续)——控件件介绍及思路
    静态变量 静态对象 静态函数和非静态函数的区别。(我的理解,大家看看对不对)
    通过“访问多种数据库”的代码来学习多态!(.net2.0版)
    Step By Step 一步一步写网站[1] —— 填加数据
    个人理财小助手 —— 数据库(一)
    几个鸟叫的声音
    Step By Step 一步一步写网站[1] —— 帧间压缩,表单控件
    面向对象相关
    论程序的成长—— 你写的代码有生命力吗?
  • 原文地址:https://www.cnblogs.com/shenben/p/6384006.html
Copyright © 2011-2022 走看看