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

    Long Long Message
    Time Limit: 4000MS   Memory Limit: 131072K
         
    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."
    本是后缀数组裸题。。我写的是RKHASH。。挺好写的 不过TLE了数次。。原因是用了stl的set。。后来改成快排+lower_bound就没T,什么逻辑= =
    网上介绍rkhash的应该挺多。。简单的说就是将字符串对应转成k进制,这样的好处不仅在于方便好写快捷,更在于对字符串进行修改的时候,这个字符串hash可以通过数据结构动态维护。。
     1 #include<set>
     2 #include<map>
     3 #include<queue>
     4 #include<cstdio>
     5 #include<cstdlib>
     6 #include<cstring>
     7 #include<iostream>
     8 #include<algorithm>
     9 using namespace std;
    10 const int N = 100100;
    11 typedef unsigned long long ull;
    12 const ull K = 10000007;
    13 #define For(i,n) for(int i=1;i<=n;i++)
    14 #define For0(i,n) for(int i=0;i<n;i++)
    15 #define Rep(i,l,r) for(int i=l;i<=r;i++)
    16 #define Down(i,r,l) for(int i=r;i>=l;i--)
    17 //set<ull> q;
    18 ull q[N];
    19 char st1[N],st2[N];
    20 int l,r,len1,len2,tail;
    21 ull f1[N],f2[N],pows[N];
    22 
    23 bool Check(int len){
    24    // q.clear();
    25     //q.insert(f1[len-1]);
    26     q[++tail]=f1[len-1];
    27     Rep(i,len+1,len1) q[++tail]=(f1[i-1]-f1[i-len-1]*pows[len]);
    28     sort(q+1,q+tail+1);
    29     //if(q.count(f2[len-1])) return true;
    30     if(q[lower_bound(q+1,q+tail+1,f2[len-1])-q]==f2[len-1]) return true;
    31     Rep(i,len+1,len2)
    32      // if(q.count(f2[i-1]-f2[i-len-1]*pows[len])) return true;
    33       if(q[lower_bound(q+1,q+tail+1,(f2[i-1]-f2[i-len-1]*pows[len]))-q]
    34           ==(f2[i-1]-f2[i-len-1]*pows[len])) return true;
    35     return false;
    36 }
    37 
    38 void GetHash(char *st,ull *F){
    39     int len=strlen(st);
    40     F[0]=st[0];
    41     For(i,len) F[i]=F[i-1]*K+st[i];
    42     F[len]=F[len-1]*K;
    43 }
    44 
    45 void init(){
    46     pows[0]=1;
    47     For(i,N-1) pows[i]=pows[i-1]*K;
    48     scanf("%s",st1);scanf("%s",st2);
    49     len1=strlen(st1);len2=strlen(st2);
    50 }
    51 
    52 int main(){
    53     init();
    54     GetHash(st1,f1);GetHash(st2,f2);
    55     r=min(len1,len2);
    56     while(r-l>1){
    57         int Mid=(l+r)>>1;tail=0;
    58         if(Check(Mid)) l=Mid;
    59         else           r=Mid;
    60     }tail=0;
    61     if(Check(r)) printf("%d
    ",r);
    62     else         printf("%d
    ",l);
    63     return 0;
    64 }
  • 相关阅读:
    LLVM 0: LLVM和Clang的背景
    semaphore和mutex的区别?
    Note: TWLKH: 浅谈Semaphore与mutex
    Bark:自建 iOS 消息推送服务
    aka.ms打不开
    《UNIX 传奇:历史与回忆》Note
    crawlab 实现批量添加爬虫的功能
    手机浏览器打开PC端网站,背景显示不全怎么办?
    jquery实现平滑的二级下拉菜单效果
    nginx服务器设置301重定向,不带www跳转到带www域名
  • 原文地址:https://www.cnblogs.com/zjdx1998/p/3989182.html
Copyright © 2011-2022 走看看