题目大意:求两个字符串的公共子串。

分析:

模板题,将两个字符串接起来用不会出现的字符分割,然后求分属两个字符串的相邻后缀lcp的最大值即可。

代码:

program work;
type
arr=array[..]of longint;
var
sa,rank,b,tmp,lcp:arr;
n,i,m,l,u,ans,t:longint;
s,s1,s2:ansistring;
ch:char;
function compare(i,j,k:longint):longint;
var ri,rj:longint;
begin
if rank[i]<>rank[j] then exit(ord(rank[i]<rank[j]))
else
begin
if i+k<=n then ri:=rank[i+k] else ri:=-;
if j+k<=n then rj:=rank[j+k] else rj:=-;
exit(ord(ri<rj));
end;
end;
procedure qsort(l,h,k:longint; var a:arr);
var i,j,t,m:longint;
begin i:=l; j:=h;
m:=a[(i+j) div ];
repeat
while compare(a[i],m,k)= do inc(i);
while compare(m,a[j],k)= do dec(j);
if i<=j then
begin t:=a[i]; a[i]:=a[j]; a[j]:=t;
inc(i); dec(j); end; until i>j;
if i<h then qsort(i,h,k,a); if j>l then qsort(l,j,k,a); end;
procedure work_sa(s:ansistring; var sa:arr);
var k,i:longint;
begin
for i:= to n do begin sa[i]:=i; rank[i]:=ord(s[i]); end;
k:=;
while k<=n do
begin
qsort(,n,k,sa);
tmp[sa[]]:=;
for i:= to n do
tmp[sa[i]]:=tmp[sa[i-]]+compare(sa[i-],sa[i],k);
rank:=tmp;
k:=k*;
end;
end;
function max(x,y:longint):longint;
begin
if x>y then max:=x else max:=y;
end;
procedure work_lcp(s:ansistring;var sa,lcp:arr);
var i,j,h:longint;
begin
for i:= to n do rank[sa[i]]:=i;
h:=; lcp[]:=; sa[]:=;rank[]:=;
for i:= to n do
begin
j:=sa[rank[i]-];
if h> then dec(h);
while (i+h<=n)and(j+h<=n) do
begin
if s[i+h]<>s[j+h] then break; inc(h);
end;
lcp[rank[i]-]:=h;
end;
end;
begin
readln(u);
for l:= to u do
begin
readln(s1); readln(s2);
s:=s1+'@'+s2;
t:=length(s1);
n:=length(s); work_sa(s,sa);
work_lcp(s,sa,lcp); ans:=;
for i:= to n- do begin
if ((sa[i]<=t)and(sa[i+]>=t+))or((sa[i+]<=t)and(sa[i]>=t+)) then ans:=max(ans,lcp[i]);
end;
writeln('Nejdelsi spolecny retezec ma delku ',ans,'.');
end;
end.

POJ 2217:Secretary(后缀数组)的更多相关文章

  1. POJ 2217 Secretary (后缀数组)

    标题效果: 计算两个公共串串最长的字符串的长度. IDEAS: 这两个组合的字符串. 然后直接确定运行后缀数组height 然后,你可以直接扫描一次height .加个是不是在一个串中的推断就能够了. ...

  2. POJ 2217 LCS(后缀数组)

    Secretary Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1655   Accepted: 671 Descript ...

  3. POJ 2217 Secretary

    Secretary Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID:  ...

  4. POJ 2406 KMP/后缀数组

    题目链接:http://poj.org/problem?id=2406 题意:给定一个字符串,求由一个子串循环n次后可得到原串,输出n[即输出字符串的最大循环次数] 思路一:KMP求最小循环机,然后就 ...

  5. POJ 1743-POJ - 3261~后缀数组关于最长字串问题

    POJ 1743 题意: 有N(1 <= N <=20000)个音符的序列来表示一首乐曲,每个音符都是1~~88范围内的整数,现在要找一个重复的主题.“主题”是整个音符序列的一个子串,它需 ...

  6. POJ - 1226 Substrings (后缀数组)

    传送门:POJ - 1226 这个题跟POJ - 3294  和POJ - 3450 都是一样的思路,一种题型. POJ - 3294的题解可以见:https://www.cnblogs.com/li ...

  7. POJ2217 Secretary 后缀数组&&高度数组

    学后缀数组后的一道裸题.先来讲讲收获,作为字符串初学者,后缀数组也是刚刚在学,所幸的是有一篇好的论文<后缀数组--处理字符串的有力工具>by 罗穗骞,里面非常详尽地介绍了有关后缀数组的概念 ...

  8. Maximum repetition substring(POJ - 3693)(sa(后缀数组)+st表)

    The repetition number of a string is defined as the maximum number \(R\) such that the string can be ...

  9. Milk Patterns POJ - 3261(后缀数组+二分)

    题意: 求可重叠的最长重复子串,但有一个限制条件..要至少重复k次 解析: 二分枚举k,对于连续的height 如果height[i] >= k 说明它们至少有k个元素是重复的,所以判断一下就好 ...

  10. POJ 3581 Sequence(后缀数组)

    Description Given a sequence, {A1, A2, ..., An} which is guaranteed A1 > A2, ..., An,  you are to ...

随机推荐

  1. expect脚本中,变量的写法

    一.expect脚本中,变量的不同写法 shell脚本中定义时间变量的写法:time=`date "+%Y%m%d"` ==>>直接照搬到expect中,设置的变量是不 ...

  2. IPv4与IPv6的校验函数

    1. PHP校验IPv4掩码是否合法,在网上搜了下,排名最高的如下, <?php function is_mask($mask){ $bin_mask =(string)decbin(ip2lo ...

  3. jquery iCheck 插件

    1 官网:http://www.bootcss.com/p/icheck/#download 2 博客:https://www.cnblogs.com/xcsn/p/6307610.html http ...

  4. PAT (Advanced Level) Practise - 1092. To Buy or Not to Buy (20)

    http://www.patest.cn/contests/pat-a-practise/1092 Eva would like to make a string of beads with her ...

  5. 转:Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结

    转自:http://blog.csdn.net/business122/article/details/7536991 创建列表 sample_list = ['a',1,('a','b')] Pyt ...

  6. Scrapy分布式爬虫打造搜索引擎- (二)伯乐在线爬取所有文章

    二.伯乐在线爬取所有文章 1. 初始化文件目录 基础环境 python 3.6.5 JetBrains PyCharm 2018.1 mysql+navicat 为了便于日后的部署:我们开发使用了虚拟 ...

  7. 解决国内网络Python2.X 3.X PIP安装模块连接超时问题

    pip国内的一些镜像   阿里云 http://mirrors.aliyun.com/pypi/simple/   中国科技大学 https://pypi.mirrors.ustc.edu.cn/si ...

  8. ATM-interface-user

    from db import db_handlerfrom lib import common user_logger = common.get_logger('user') def login_in ...

  9. ATMstart

    import os, sys BASE_DIR = os.path.dirname(__file__)sys.path.append(BASE_DIR) from core import src if ...

  10. Thinkphp 支付宝插件的引入 和调用

    本文版权归本宝宝所有 未得允许不得转载 下载地址传送门 https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7629140.0.0.twLYka&am ...