• poj 2774:Long Long Message


    Time Limit: 4000MS   Memory Limit: 131072K
    Total Submissions: 25018   Accepted: 10237
    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."
     
    题解:
      题意:题目给定两个字符串A,B,求A和B的最长公共子串。
      转化:因为子串一定是某个后缀的前缀,所以问题等价于求A的后缀和B的后缀中的最长公共前缀的最大值。借助于height[]数组,我们让A串和B串连接起来,形成新的字符串T,取其中的height值,但是要保证当前的height[i]中的i满足排名第i的后缀和排名第i-1的后缀分别分布在A,B两个串里。
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<algorithm>
     6 #include<queue>
     7 #include<vector>
     8 using namespace std;
     9 const int maxn=200000;
    10 int r[maxn],sa[maxn],wa[maxn],wb[maxn],wv[maxn],Ws[maxn];
    11 int cmp(int *r,int a,int b,int l){
    12     return r[a]==r[b]&&r[a+l]==r[b+l];
    13 }
    14 void da(int *r,int *sa,int n,int m){
    15     int i,j,p,*x=wa,*y=wb,*t;
    16     for(i=0;i<m;i++) Ws[i]=0;
    17     for(i=0;i<n;i++) Ws[x[i]=r[i]]++;
    18     for(i=1;i<m;i++) Ws[i]+=Ws[i-1];
    19     for(i=n-1;i>=0;i--) sa[--Ws[x[i]]]=i;
    20     
    21     for(j=1,p=1;p<n;j*=2,m=p){
    22         for(p=0,i=n-j;i<n;i++) y[p++]=i;
    23         for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;
    24         
    25         for(i=0;i<n;i++) wv[i]=x[y[i]];
    26         for(i=0;i<m;i++) Ws[i]=0;
    27         for(i=0;i<n;i++) Ws[wv[i]]++;
    28         for(i=1;i<m;i++) Ws[i]+=Ws[i-1];
    29         for(i=n-1;i>=0;i--) sa[--Ws[wv[i]]]=y[i];
    30         
    31         for(t=x,x=y,y=t,p=1,i=1,x[sa[0]]=0;i<n;i++)
    32             x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
    33     }
    34 }
    35 int rank[maxn],height[maxn];
    36 void calheight(int *r,int *sa,int n){
    37     int i,j,k=0;
    38     for(i=0;i<n;i++) rank[sa[i]]=i;
    39     for(i=0;i<n;height[rank[i++]]=k)
    40         for(k?k--:0,j=sa[rank[i]-1];r[i+k]==r[j+k];k++);
    41     return ; 
    42 }
    43 char s1[maxn],s2[maxn];
    44 int len1,len2,now,num[maxn],ans;
    45 int main(){
    46     scanf("%s%s",s1,s2); len1=strlen(s1); len2=strlen(s2);
    47     for(int i=0;i<len1;i++) num[now++]=s1[i]-'a'+2;
    48     num[now++]=1;
    49     for(int i=0;i<len2;i++) num[now++]=s2[i]-'a'+2;
    50     da(num,sa,now,30);
    51     calheight(num,sa,now);
    52     for(int i=1;i<now;i++)
    53         if((sa[i]<len1&&sa[i-1]>len1)||(sa[i-1]<len1&&sa[i]>len1))
    54             ans=max(ans,height[i]);
    55     printf("%d",ans);
    56     return 0;
    57 }
  • 相关阅读:
    drf的模型基表与10个单群操作方法
    drf的序列化组件
    drf之请求模块,渲染模块,解析模块,响应模块,异常模块
    drf之接口与接口规范
    MongoDB C# Demo
    如何用Dome4j(2.2.1)创建Xml
    Map、Set的基本概括
    如何自定义xml文件
    HashMap和HashTable的理解与区别
    装箱 拆箱
  • 原文地址:https://www.cnblogs.com/CXCXCXC/p/5176764.html
Copyright © 2020-2023  润新知