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 :( 

 
  一道卖萌的题面。。>_<
  给出我们两个字符串,求其最长的连续子串长度
  用后缀数组解决
  我将一个字符串接到另一个字符串后面,中间留出足够的空格以免影响答案
  求出height数组,二分答案
  然后按照sa数组的顺序验证。  
  因为各种各样的错误也调了很久。。
 
 program poj2774;
const maxn=;
var na,nb,m,n,i:longint;
s1,s2:ansistring;
a,height,sa,rank,tmp,s:array[-..maxn]of longint; function max(a,b:longint):longint;
begin
if a>b then exit(a) else exit(b);
end; function min(a,b:longint):longint;
begin
if a<b then exit(a) else exit(b);
end; procedure Suffix_Array;
var i,j,p,size,v0,v1,v00,v01:longint;
begin
size:=max(,n);
for i:= to size- do s[i]:=;
for i:= to n- do rank[i]:=a[i];
for i:= to n- do inc(s[rank[i]]);
for i:= to size- do inc(s[i],s[i-]);
for i:=n- downto do
begin
dec(s[rank[i]]);
sa[s[rank[i]]]:=i;
end;
j:=;
while j<=n do
begin
p:=;
for i:=n-j to n- do
begin
tmp[p]:=i;
inc(p);
end;
for i:= to n- do if sa[i]-j>= then
begin
tmp[p]:=sa[i]-j;
inc(p);
end;
for i:= to size- do s[i]:=;
for i:= to n- do inc(s[rank[i]]);
for i:= to size- do inc(s[i],s[i-]);
for i:=n- downto do
begin
dec(s[rank[tmp[i]]]);
sa[s[rank[tmp[i]]]]:=tmp[i];
end;
p:=;tmp[sa[]]:=;
for i:= to n- do
begin
v0:=sa[i-];v1:=sa[i];
if v0+j<n then v00:=rank[v0+j] else v00:=-;
if v1+j<n then v01:=rank[v1+j] else v01:=-;
if (rank[v0]=rank[v1])and(v00=v01) then tmp[sa[i]]:=p else
begin
inc(p);tmp[sa[i]]:=p;
end;
end;
for i:= to n- do rank[i]:=tmp[i];
j:=j << ;
end;
end; function compare(i,j,x:longint):longint;
begin
while (a[i+x-]=a[j+x-])and(x<=na)and(x<=nb) do inc(x);
exit(x-);
end; procedure calc_height;
var i:longint;
begin
if rank[]= then height[]:= else height[]:=compare(,sa[rank[]-],);
for i:= to n- do
if rank[i]= then height[i]:= else height[i]:=compare(i,sa[rank[i]-],max(height[i-],));
end; function check(x:longint):boolean;
var i,j:longint;
flag1,flag2:boolean;
begin
i:=;
while i<n do
begin
flag1:=false;flag2:=false;
if sa[i]<=na-x then flag1:=true;
if (sa[i]>m)and(sa[i]<=n-x) then flag2:=true;
//控制找到的是真实存在的子串
j:=i+;
while (j<n)and(height[sa[j]]>=x) do
begin
if sa[j]<=na-x then flag1:=true;
if (sa[j]>m)and(sa[j]<=n-x) then flag2:=true;
inc(j);
end;
if flag1 and flag2 then exit(true);
i:=j;
end;
exit(false);
end; procedure binary;
var L,R,mid,ans:longint;
begin
ans:=;
L:=;R:=min(na,nb);
while L<=R do
begin
mid:=(L+R) >> ;
if check(mid) then
begin
ans:=mid;L:=mid+;
end else R:=mid-;
end;
writeln(ans);
end; begin
readln(s1);
na:=length(s1);
for i:= to na- do a[i]:=ord(s1[i+])-;
m:=;
while m<na do m:=m << ;
m:=na-+m;
readln(s2);
nb:=length(s2);
for i:=m+ to m++nb- do a[i]:=ord(s2[i-m])-;
n:=m+nb+;
Suffix_Array;
calc_height;
binary;
end.

[POJ2774]Long Long Message 解题报告的更多相关文章

  1. URAL 1654 Cipher Message 解题报告

    题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1654 题意:简单的理解就是,把一个序列中相邻的且是偶数个相同的字符删除,奇数个的话就只保 ...

  2. 【NOIP2015】提高组D1 解题报告

    P1978神奇的幻方 Accepted 描述 幻方是一种很神奇的 N ∗ N 矩阵:它由数字 1,2,3, … … , N ∗ N 构成,且每行.每列及两条对角线上的数字之和都相同. 当 N 为奇数时 ...

  3. 2011 ACM-ICPC 成都赛区解题报告(转)

    2011 ACM-ICPC 成都赛区解题报告 首先对F题出了陈题表示万分抱歉,我们都没注意到在2009哈尔滨赛区曾出过一模一样的题.其他的话,这套题还是非常不错的,除C之外的9道题都有队伍AC,最终冠 ...

  4. NOIP2015 提高组(senior) 解题报告

    过了这么久才来发解题报告,蒟蒻实在惭愧 /w\ Day1 T1 [思路] 模拟 [代码] #include<iostream> #include<cstring> #inclu ...

  5. 冲刺Noip2017模拟赛5 解题报告——五十岚芒果酱

    1. 公约数(gcd) [问题描述] 给定一个正整数,在[,n]的范围内,求出有多少个无序数对(a,b)满足 gcd(a,b)=a xor b. [输入格式] 输入共一行,一个正整数n. [输出格式] ...

  6. 【LeetCode】91. Decode Ways 解题报告(Python)

    [LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...

  7. CH Round #56 - 国庆节欢乐赛解题报告

    最近CH上的比赛很多,在此会全部写出解题报告,与大家交流一下解题方法与技巧. T1 魔幻森林 描述 Cortana来到了一片魔幻森林,这片森林可以被视作一个N*M的矩阵,矩阵中的每个位置上都长着一棵树 ...

  8. 二模13day1解题报告

    二模13day1解题报告 T1.发射站(station) N个发射站,每个发射站有高度hi,发射信号强度vi,每个发射站的信号只会被左和右第一个比他高的收到.现在求收到信号最强的发射站. 我用了时间复 ...

  9. BZOJ 1051 最受欢迎的牛 解题报告

    题目直接摆在这里! 1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4438  Solved: 2353[S ...

随机推荐

  1. OrCAD把原理图中的器件添加到原理图库

    1. 在使用OrCAD的时候,有时需要把别人的原理图里面的器件添加到自己的原理图库,方便以后使用,具体操作如下,依次选择Design Cache---元器件--Copy 2. 选中要存放的原理图库,鼠 ...

  2. FJWC 2019 游记

    FJWC 2019 游记 Day 0 春节旅游, 刚从杭州绍兴一带赶回来, 然而并没有直接飞去福州, 去了厦门再去福州, 浪费了好多时间. Day 1 酒店到学校有 \(20\) 分钟的步行路程, 感 ...

  3. formValidator 不支持jquery1.9以上的解决办法

    不支持的原因是因为jquery1.9以上版本剔除了$.browser 在formValidator 源码最顶层.或者jquery源码里加上如下代码 $.browser = {};$.browser.m ...

  4. 对 a = [lambda : x for x in range(3)] 的理解

    上面的语句创建了一个列表 a ,其中有三个元素,每个元素都是一个 lambda 匿名函数. >>> a = [lambda : x for x in range(3)] >&g ...

  5. HDU 4719 Oh My Holy FFF(DP+线段树)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)

    Description N soldiers from the famous "*FFF* army" is standing in a line, from left to ri ...

  6. [译]如何去除Git的unstaged的文件提示“old mode 100755 new mode 100644”?

    原文来源:https://stackoverflow.com/questions/1257592/how-do-i-remove-files-saying-old-mode-100755-new-mo ...

  7. Java 实现一个带提醒的定时器

    定时闹钟预览版EXE下载链接:https://files.cnblogs.com/files/rekent/ReadytoRelax_jar.zip 功能说明: 实现了一个休息提醒器,用户首先设定一个 ...

  8. 算法(1)K-diff Pairs in an Array

    写在前面:研究操作系统,习惯了用C,但是在做算法题甚至构建大型系统时,C真的是噩梦.还是用C++比较好,基本算法很成熟,并可基于此实现更复杂的算法.那就边写算法边捡起来好久不用的C++吧! 题目:数组 ...

  9. CSS继承—深入剖析

    CSS的继承是指被包在内部的标签将拥有外部标签的样式性质.继承特性最典型的应用通常发挥在整个网页的样式预设,即整体布局声明.而需要要指定为其它样式的部份设定在个别元素里即可达到效果.这项特性可以给网页 ...

  10. [bzoj1018] [SHOI2008]堵塞的交通

    题目描述 有一天,由于某种穿越现象作用,你来到了传说中的小人国.小人国的布局非常奇特,整个国家的交通系统可以被看成是一个22行CC列的矩形网格,网格上的每个点代表一个城市,相邻的城市之间有一条道路,所 ...