标签:
Long Long Message
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 :(
1 program poj2774; 2 const maxn=400010; 3 var na,nb,m,n,i:longint; 4 s1,s2:ansistring; 5 a,height,sa,rank,tmp,s:array[-1..maxn]of longint; 6 7 function max(a,b:longint):longint; 8 begin 9 if a>b then exit(a) else exit(b); 10 end; 11 12 function min(a,b:longint):longint; 13 begin 14 if a<b then exit(a) else exit(b); 15 end; 16 17 procedure Suffix_Array; 18 var i,j,p,size,v0,v1,v00,v01:longint; 19 begin 20 size:=max(200,n); 21 for i:=0 to size-1 do s[i]:=0; 22 for i:=0 to n-1 do rank[i]:=a[i]; 23 for i:=0 to n-1 do inc(s[rank[i]]); 24 for i:=1 to size-1 do inc(s[i],s[i-1]); 25 for i:=n-1 downto 0 do 26 begin 27 dec(s[rank[i]]); 28 sa[s[rank[i]]]:=i; 29 end; 30 j:=1; 31 while j<=n do 32 begin 33 p:=0; 34 for i:=n-j to n-1 do 35 begin 36 tmp[p]:=i; 37 inc(p); 38 end; 39 for i:=0 to n-1 do if sa[i]-j>=0 then 40 begin 41 tmp[p]:=sa[i]-j; 42 inc(p); 43 end; 44 for i:=0 to size-1 do s[i]:=0; 45 for i:=0 to n-1 do inc(s[rank[i]]); 46 for i:=0 to size-1 do inc(s[i],s[i-1]); 47 for i:=n-1 downto 0 do 48 begin 49 dec(s[rank[tmp[i]]]); 50 sa[s[rank[tmp[i]]]]:=tmp[i]; 51 end; 52 p:=0;tmp[sa[0]]:=0; 53 for i:=1 to n-1 do 54 begin 55 v0:=sa[i-1];v1:=sa[i]; 56 if v0+j<n then v00:=rank[v0+j] else v00:=-1; 57 if v1+j<n then v01:=rank[v1+j] else v01:=-1; 58 if (rank[v0]=rank[v1])and(v00=v01) then tmp[sa[i]]:=p else 59 begin 60 inc(p);tmp[sa[i]]:=p; 61 end; 62 end; 63 for i:=0 to n-1 do rank[i]:=tmp[i]; 64 j:=j << 1; 65 end; 66 end; 67 68 function compare(i,j,x:longint):longint; 69 begin 70 while (a[i+x-1]=a[j+x-1])and(x<=na)and(x<=nb) do inc(x); 71 exit(x-1); 72 end; 73 74 procedure calc_height; 75 var i:longint; 76 begin 77 if rank[0]=0 then height[0]:=0 else height[0]:=compare(0,sa[rank[0]-1],1); 78 for i:=1 to n-1 do 79 if rank[i]=0 then height[i]:=0 else height[i]:=compare(i,sa[rank[i]-1],max(height[i-1],1)); 80 end; 81 82 function check(x:longint):boolean; 83 var i,j:longint; 84 flag1,flag2:boolean; 85 begin 86 i:=0; 87 while i<n do 88 begin 89 flag1:=false;flag2:=false; 90 if sa[i]<=na-x then flag1:=true; 91 if (sa[i]>m)and(sa[i]<=n-x) then flag2:=true; 92 //控制找到的是真实存在的子串 93 j:=i+1; 94 while (j<n)and(height[sa[j]]>=x) do 95 begin 96 if sa[j]<=na-x then flag1:=true; 97 if (sa[j]>m)and(sa[j]<=n-x) then flag2:=true; 98 inc(j); 99 end; 100 if flag1 and flag2 then exit(true); 101 i:=j; 102 end; 103 exit(false); 104 end; 105 106 procedure binary; 107 var L,R,mid,ans:longint; 108 begin 109 ans:=0; 110 L:=1;R:=min(na,nb); 111 while L<=R do 112 begin 113 mid:=(L+R) >> 1; 114 if check(mid) then 115 begin 116 ans:=mid;L:=mid+1; 117 end else R:=mid-1; 118 end; 119 writeln(ans); 120 end; 121 122 begin 123 readln(s1); 124 na:=length(s1); 125 for i:=0 to na-1 do a[i]:=ord(s1[i+1])-48; 126 m:=1; 127 while m<na do m:=m << 1; 128 m:=na-1+m; 129 readln(s2); 130 nb:=length(s2); 131 for i:=m+1 to m+1+nb-1 do a[i]:=ord(s2[i-m])-48; 132 n:=m+nb+1; 133 Suffix_Array; 134 calc_height; 135 binary; 136 end.
[POJ2774]Long Long Message 解题报告
标签:
原文地址:http://www.cnblogs.com/mjy0724/p/4414337.html