后缀数组....

在两个串中唯一出现的最小公共子串

D. Match & Catch
time limit per test

1 second

memory limit per test

512 megabytes

input

standard input

output

standard output

Police headquarter is monitoring signal on different frequency levels. They have got two suspiciously encoded strings s1 and s2 from
two different frequencies as signals. They are suspecting that these two strings are from two different criminals and they are planning to do some evil task.

Now they are trying to find a common substring of minimum length between these two strings. The substring must occur only once in the first string, and also it must occur only once in the second string.

Given two strings s1 and s2 consist
of lowercase Latin letters, find the smallest (by length) common substring p of both s1 and s2,
wherep is a unique substring in s1 and
also in s2. See notes
for formal definition of substring and uniqueness.

Input

The first line of input contains s1 and
the second line contains s2 (1 ≤ |s1|, |s2| ≤ 5000).
Both strings consist of lowercase Latin letters.

Output

Print the length of the smallest common unique substring of s1 and s2.
If there are no common unique substrings of s1 and s2 print
-1.

Sample test(s)
input
apple
pepperoni
output
2
input
lover
driver
output
1
input
bidhan
roy
output
-1
input
testsetses
teeptes
output
3
Note

Imagine we have string a = a1a2a3...a|a|,
where |a| is the length of string a, and ai is
the ith letter of
the string.

We will call string alal + 1al + 2...ar (1 ≤ l ≤ r ≤ |a|) the
substring [l, r] of the string a.

The substring [l, r] is unique in a if and
only if there is no pair l1, r1 such
that l1 ≠ l and
the substring [l1, r1] is
equal to the substring[l, r] in a.


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int maxn=10100,INF=0x3f3f3f3f; int sa[maxn],rank[maxn],rank2[maxn],h[maxn],c[maxn],*x,*y,ans[maxn];
char str[maxn]; bool cmp(int*r,int a,int b,int l,int n)
{
if(r[a]==r[b]&&a+l<n&&b+l<n&&r[a+l]==r[b+l]) return true;
return false;
} bool radix_sort(int n,int sz)
{
for(int i=0;i<sz;i++) c[i]=0;
for(int i=0;i<n;i++) c[x[y[i]]]++;
for(int i=1;i<sz;i++) c[i]+=c[i-1];
for(int i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];
} void get_sa(char c[],int n,int sz=128)
{
x=rank,y=rank2;
for(int i=0;i<n;i++) x[i]=c[i],y[i]=i;
radix_sort(n,sz);
for(int len=1;len<n;len*=2)
{
int yid=0;
for(int i=n-len;i<n;i++) y[yid++]=i;
for(int i=0;i<n;i++) if(sa[i]>=len) y[yid++]=sa[i]-len; radix_sort(n,sz); swap(x,y);
x[sa[0]]=yid=0; for(int i=1;i<n;i++)
{
x[sa[i]]=cmp(y,sa[i],sa[i-1],len,n)?yid:++yid;
} sz=yid+1; if(sz>=n) break;
} for(int i=0;i<n;i++) rank[i]=x[i];
} void get_h(char str[],int n)
{
int k=0; h[0]=0;
for(int i=0;i<n;i++)
{
if(rank[i]==0) continue;
k=max(k-1,0);
int j=sa[rank[i]-1];
while(i+k<n&&j+k<n&&str[i+k]==str[j+k]) k++;
h[rank[i]]=k;
}
} int main()
{
cin>>str;
int sg=strlen(str);
str[sg]=127;
cin>>str+sg+1;
int n=strlen(str);
get_sa(str,n);
get_h(str,n); int ans=INF;
int s1=0,s2=0,last=-1;
for(int i=1;i<n;i++)
{
if(sa[i-1]<sg&&sa[i]<sg) continue;
if(sa[i-1]>sg&&sa[i]>sg) continue; int pre=h[i-1];
int next=h[i+1];
if(h[i]>max(pre,next))
{
ans=min(ans,max(pre,next)+1);
}
}
if(ans==INF) ans=-1;
printf("%d\n",ans);
return 0;
}

Codeforces 427 D. Match &amp; Catch的更多相关文章

  1. Codeforces 427D Match &amp; Catch(后缀自动机)

    [题目链接] http://codeforces.com/problemset/problem/427/D [题目大意] 给出一个两个字符串,求出最短且在两个字符串中唯一的公共子串. [题解] 以原字 ...

  2. cf244D. Match &amp; Catch 字符串hash (模板)或 后缀数组。。。

    D. Match & Catch 能够用各种方法做.字符串hash.后缀数组,dp.拓展kmp,字典树.. . 字符串hash(模板) http://blog.csdn.net/gdujian ...

  3. CF 427D Match &amp; Catch 求最短唯一连续LCS

    题目来源:CF 427D Match & Catch 题意:给出2个字符串 求最短的连续的公共字符串 而且该字符串在原串中仅仅出现一次 思路:把2个字符串合并起来求height 后缀数组hei ...

  4. Codeforces #427 Div2 D

    #427 Div2 D 题意 给出一个字符串,求它的子串中为 \(k-palindrome\) 的个数. \(1-palindrome\) 要求是一个回文串. \(k-palindrome (k &g ...

  5. codeforces 427 div.2 F. Roads in the Kingdom

    F. Roads in the Kingdom time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  6. Rhino+envjs-1.2.js 在java运行网站js 工具类

    java爬虫遇到个页面加密的东西,找了些资料学习学习 做了个java运行js的工具类,希望对大家有用,其中用到client(获取js)可以自行换成自己的client.主要是用了 Rhino就是Java ...

  7. C#的变迁史 - C# 5.0 之其他增强篇

    1. 内置zip压缩与解压 Zip是最为常用的文件压缩格式之一,也被几乎所有操作系统支持.在之前,使用程序去进行zip压缩和解压要靠第三方组件去支持,这一点在.NET4.5中已有所改观,Zip压缩和解 ...

  8. jquery-jsrender使用

      JsRender是一款基于jQuery的JavaScript模版引擎 特点: · 简单直观 · 功能强大 · 可扩展的 · 快如闪电 jsrender使用比较简单,本文简单结束一些常用的 使用过程 ...

  9. yii php 图片上传与生成缩略图

    今天需要做图片上传与生成缩略图的功能,把代码进行记录如下: html 视图              ($pic_action_url = $this->createAbsoluteUrl('h ...

随机推荐

  1. Learning Vector

    题意: 给出n组x,y增量,从(0,0)开始以x,y坐标增加后等到的终点坐标,可以构成一个面积,再以这个终点为起点再增加,以此类推,使用增量顺序不同,得到的面积不,求用k组增量能得到的最大的面积. 分 ...

  2. 性能测试之系统监控工具nmon

    一.概述 本篇文章主要讲解nmon,以下为目录 1.nmon介绍 2.nmon下载.安装及使用 3.nmon analysis 分析及使用,各个项的含义 二.详细信息: 1.nmon介绍: nmon( ...

  3. Fisher information matrix笔记

    在看FK论文时,fisher information matrix是必须理解的. 从维基百科查阅到,Fisher information matrix是用利用最大似然函数估计来计算方差矩阵. 来源于: ...

  4. R 安装与环境配置

    R语言是一种很方便的应用于科学计算的语言,推荐给大家学习. 由于R的版本对程序包的兼容有些问题,推荐使用2.15.3. 下面给出下载链接,安装一路默认即可,可以自己更存放目录,其他的都没有影响.下面是 ...

  5. CAD操作

    1.建立构造线 说签名和图签不在同一条直线上,如何判断两个对向到底是不是在一条线上呢?通过构造线( Construction Line)可以进行判断,CAD中打入: xl 命令,再键入h(horizo ...

  6. C++二叉树的实现

    C++实现二叉查找树 啥是二叉查找树 在数据结构中,有一个奇葩的东西,说它奇葩,那是因为它重要,这就是树.而在树中,二叉树又是当中的贵族.二叉树的一个重要应用是它们在查找中的应用,于是就有了二叉查找树 ...

  7. 一起刷LeetCode5-Longest Palindromic Substring

    发现自己原来掌握的一下算法,都忘掉了,啊啊啊 ----------------------------------------------------------------------------- ...

  8. Flex里的命名空间,fx、mx、s【转】

    Flex 4带给我们的,是全新的命名空间.了解这些命名空间必定是一件好事情.Flex 4有三个非常重要的命名空间,分别是: xmlns:fx=”http://ns.adobe.com/mxml/200 ...

  9. 手势冲突UIPanGestureRecognizer 和UIPinchGestureRecognizer

    当同时使用pan和pin手势时假如冲突,需要加入下面方法 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shou ...

  10. 第二百三十天 how can I 坚持

    上周日去蟒山摘的松子吗?应该是松子吧,裂开了呢.为啥呢.原来博客园可以上传图片,只是上传起来好费劲啊. 今天程哥问给我分的活多不多,我竟然说了句好多,哎.其实很多问题可以用还好来回答,还好,还行,哈哈 ...