当前位置 : 主页 > 编程语言 > python >

POJ 2774 Long Long Message(后缀数组求最长公共子串,4级)

来源:互联网 收集:自由互联 发布时间:2023-03-22
J -Long Long Message Time Limit:4000MSMemory Limit:131072KB64bit IO Format:%I64d %I64u ​​Submit​​​ ​​​Status​​ System Crawler (2013-08-28) Description The little cat is majoring in physics in the capital of Byterland. A

J - Long Long Message

Time Limit:4000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u

​​Submit​​​  ​​​Status​​

System Crawler  (2013-08-28)

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

思路:后缀数组,把两个串合并,求最长前缀。

#include<iostream>#include<cstring>#include<cstdio>#define ll(x) (1<<x)#define FOR(i,a,b) for(int i=a;i<=b;++i)#define clr(f,z) memset(f,z,sizeof(f))using namespace std;const int msize=2e5+9;class SUFFIX_ARRAY{public: int rank[msize],h[msize],c[msize],sa[msize]; bool cmp(int*r,int i,int k) { return r[ sa[i] ]==r[ sa[i-1] ]&&r[ sa[i]+k ]==r[ sa[i-1]+k ]; } void build_SA(char*s,int n,int m) { int*wx=h,*wy=rank; FOR(i,0,m-1)c[i]=0; FOR(i,0,n-1)++c[ wx[i]=s[i] ]; FOR(i,1,m-1)c[i]+=c[i-1]; for(int i=n-1;i>=0;--i)sa[ --c[ wx[i] ] ]=i; for(int k=1;k<=n;k<<=1) { int p=0; FOR(i,n-k,n-1)wy[p++]=i; FOR(i,0,n-1)if(sa[i]>=k)wy[p++]=sa[i]-k; FOR(i,0,m-1)c[i]=0; FOR(i,0,n-1)++c[ wx[ wy[i] ] ]; FOR(i,1,m-1)c[i]+=c[i-1]; for(int i=n-1;i>=0;--i)sa[ --c[ wx[ wy[i] ] ] ]=wy[i]; swap(wx,wy); p=1;wx[ sa[0] ]=0; FOR(i,1,n-1)wx[ sa[i] ]=cmp(wy,i,k)?p-1:p++; if(p>=n)break; m=p; } } void get_H(char*s,int n) { int k=0; FOR(i,0,n)rank[ sa[i] ]=i; FOR(i,0,n-1) { if(k)--k; int j=sa[ rank[i]-1 ]; while(s[i+k]==s[j+k])++k; h[ rank[i] ]=k; } } void debug(int n) { printf("sa="); FOR(i,0,n)printf("%d ",sa[i]);puts(""); printf("rank="); FOR(i,0,n)printf("%d ",rank[i]);puts(""); printf("h="); FOR(i,0,n)printf("%d ",h[i]);puts(""); } int rmq[msize][33],bit[msize]; void initRMQ(int n) { bit[0]=-1; FOR(i,1,n)bit[i]=(i&(i-1))==0?bit[i-1]+1:bit[i-1]; FOR(i,1,n)rmq[i][0]=h[i]; FOR(i,1,bit[n]) for(int j=1;j+ll(i)-1<=n;++j) rmq[j][i]=min(rmq[j][i-1],rmq[j+ll(i-1)][i-1]); } int LCP(int l,int r) { l=rank[l],r=rank[r]; if(l>r)swap(l,r); ++l; int t=bit[r-l+1]; r-=ll(t)-1;///这个居然忘了 return min(rmq[l][t],rmq[r][t]); } int has[msize]; void getstring(char*s,int len,int n) { int ans=0; FOR(i,1,n) if((sa[i]>len&&sa[i-1]<len)||(sa[i]<len&&sa[i-1]>len)) ans=max(ans,h[i]); printf("%d\n",ans); }};SUFFIX_ARRAY ty;char s[2][msize+msize];int r[msize];int main(){ int cas=0; //freopen("data.in","r",stdin); while(~scanf("%s%s",s[0],s[1])) { int len,kn; kn=len=strlen(s[0]); s[0][len]=127; for(int i=0;s[1][i];++i) s[0][++len]=s[1][i]; s[0][++len]=0; ++len; ty.build_SA(s[0],len+1,128); ty.get_H(s[0],len); //ty.initRMQ(len); ty.getstring(s[0],kn,len); }}

上一篇:HDU 3035 War (dijstra+最小割建图,4级)
下一篇:没有了
网友评论