后缀数组....

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

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;
}

转载于:https://www.cnblogs.com/mengfanrong/p/3763720.html

Codeforces 427 D. Match amp; Catch相关推荐

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

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

  2. iOS原生如何加载HTML中img标签的图片

    原文出自:iOS原生如何加载HTML中img标签的图片 前言 最近iOS App项目中使用Webview加载H5页面比较多,也有不少朋友经常问到这个问题,在这里我也学习学习如何通过iOS原生的方式来加 ...

  3. Java 过滤特殊字符的 正则表达式

    Java正则表达式学习: 因为正则表达式是一个很庞杂的体系,此例仅举些入门的概念,更多的请参阅相关书籍及自行摸索. \\ 反斜杠 \t 间隔 ('\u0009') \n 换行 ('\u000A') \ ...

  4. Java正則表達式入门

     众所周知,在程序开发中,难免会遇到须要匹配.查找.替换.推断字符串的情况发生,而这些情况有时又比較复杂,假设用纯编码方式解决,往往会浪费程序猿的时间及精力.因此,学习及使用正則表達式,便成了解决这一 ...

  5. Java的正则表达式使用及案例

    2019独角兽企业重金招聘Python工程师标准>>> 由于项目中使用到了利用正则表达式进行表单的校验,回想一下正则表达式的内容,忘得也差不多了,俗话说:"温故而知新,可以 ...

  6. C# - Get HttpWebResponse Text

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! // - ...

  7. java正则表达式 过滤特殊字符的正则表达式

    在网上找了好久也没找到个合适的正则表达式以过滤特殊字符:自己学习了下,写了两个,实现要求. Java代码 // 过滤特殊字符 public static String StringFilter(Str ...

  8. java 正則_Java正則表達式入门

    u)     在这个模式下,假设你还启用了CASE_INSENSITIVE标志,那么它会对Unicode字符进行大写和小写不明感的匹配.默认情况下,大写和小写不敏感的匹配仅仅适用于US-ASCII字符 ...

  9. Java过滤特殊字符的正则表达式

    在网上找了好久也没找到个合适的正则表达式以过滤特殊字符:自己学习了下,写了两个,实现要求. Java代码   // 过滤特殊字符 public   static   String StringFilt ...

最新文章

  1. luasocket 安装记录 (FS1.6)
  2. Android ViewGroup等容器控件的使用
  3. 深思:如何堂堂正正的做事
  4. clion配置_Ubuntu16.04系统下用CLion编译器调试ROS代码详细教程
  5. CSDN 中秋节日礼品盒 | 中秋节快乐
  6. html百度地图标记图标,百度地图开发之点击地图给该位置添加标注(图标)并弹出对应的位置...
  7. 漫画|解读电气安全“十不准”
  8. Linux Kernel/optee/ATF等操作系统的异常向量表的速查
  9. 低成本运行 Spark 数据计算
  10. java 不可修改的集合对象_[改善Java代码]asList方法产生的List对象不可更改
  11. Phpcms之核心目录phpcms
  12. 什么是URL?协议头、路径和端口是什么意思?
  13. mac 黑窗口连接mysql_mac系统下mysql环境变量设置及远程连接
  14. 乐视盒子u4刷Android,乐视盒子u4安装优酷
  15. java 常量pi_Java-常量
  16. My Seventeenth Page - 赎金信 - By Nicolas
  17. Java实现二手交易系统
  18. java SE部分以及数据库学习笔记
  19. Cell综述:代谢控制中的脑肠轴
  20. 深度神经网络的成功应用,深度神经网络技术赋能

热门文章

  1. diagram怎么记忆_UML20以上14图的解释、分类记忆方法及类之间6种关系
  2. Description Resource Path Location Type Path must include project and resource name: /
  3. Eureka修改实例名称
  4. 分区供水条件口诀_经典口诀2020年一建市政管道篇
  5. u盘启动 联想一体机_关于联想扬天B41-30-NTZ(L)使用U盘重装Win10系统的详细步骤
  6. python2和python3关系_升级python2至python3解决依赖关系
  7. linux实用小工具,Linux系统小工具使用大用途---1
  8. 8000字 | Python数据可视化,完整版实操指南 !
  9. 漫画 | 这样的男朋友,让我分分钟想剖腹自尽!
  10. asp实现注册登录界面_python app (kivy)-与小型数据库连接,实现注册登录操作