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. NB-IOT的键值对

    1. 关于NB-IOT的软件开发,有一个功能,NB收到数据的时候可以唤醒处于低功耗下的MCU. 2. 2个键值对可以配置这个功能.使用键值对的方式. 3. 遇到的第一个问题,<config> ...

  2. 2018春季校园招聘笔经面经合集:Java开发岗

    2018春季校园招聘笔经面经合集:Java开发岗 以下为精选面经: 美团外卖配送部后台开发面经 nowcoder.com/discuss/76 春招总结,干货满满 nowcoder.com/discu ...

  3. DO NOT BELIEVE HIS LIES 游戏随笔

    这游戏是我大学的一个基友推荐的,好吧,感觉被他坑了··· 解谜游戏~慢慢来玩玩··· 恩,就是下面红色圈圈画起来的这个家伙. #1 第一关 好吧,界面上也没啥可聊的,上面写了一行字,THE FIRST ...

  4. [转载]深入理解Batch Normalization批标准化

    文章转载自:http://www.cnblogs.com/guoyaohua/p/8724433.html Batch Normalization作为最近一年来DL的重要成果,已经广泛被证明其有效性和 ...

  5. BZOJ 4276 [ONTAK2015]Bajtman i Okrągły Robin 费用流+线段树优化建图

    Description 有n个强盗,其中第i个强盗会在[a[i],a[i]+1],[a[i]+1,a[i]+2],...,[b[i]-1,b[i]]这么多段长度为1时间中选出一个时间进行抢劫,并计划抢 ...

  6. MySql动态生成SQL并执行

    场景:由于一些表中设计了一些冗余字段,因此在主表修改了该冗余字段的值得时候,需要动态更新在其他表中冗余字段的值 BEGIN #Routine body goes here... /*SQL语句变量*/ ...

  7. 浅谈C#中show和showDialog的区别

    [转载] A.WinForm中窗体显示  显示窗体可以有以下2种方法:  Form.ShowDialog方法 (窗体显示为模式窗体)  Form.Show方法 (窗体显示为无模式窗体) 2者具体区别如 ...

  8. poj1789 Truck History最小生成树

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20768   Accepted: 8045 De ...

  9. Mybatis学习系列(七)缓存机制

    Mybatis缓存介绍 MyBatis提供一级缓存和二级缓存机制. 一级缓存是Sqlsession级别的缓存,Sqlsession类的实例对象中有一个hashmap用于缓存数据.不同的Sqlsessi ...

  10. 利用 spring 的 task:scheduled-tasks 执行定期任务

    ref是工作类 method是工作类中要执行的方法 initial-delay是任务第一次被调用前的延时,单位毫秒 fixed-delay是上一个调用完成后再次调用的延时 fixed-rate是上一个 ...