CodeForces-427D:Match & Catch (后缀自动机)
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, where p 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.
Example
apple
pepperoni
2
lover
driver
1
bidhan
roy
-1
testsetses
teeptes
3
题意:给定字符串S,T。求最短的公共字串s的长度|s|,s满足在各自的母串里只出现了一次。
思路:建立S的后缀自动机,拓扑得到每个字串出现的次数num1。 然后T在后缀自动机上面走,也得到字串T的次数num2。 然后每个集合如果在各自串都只出现一次,就可以用当前集合的最小长度比较答案了。
经验:对建立后缀自动机的S串,在设置num1的时候,可以在np处设置num1=1(也就是所有Blue节点),或者建立完自动机后跑一遍,跑到的地方num1=1。
具体的可以参考代码。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int inf=;
const int maxn=;
int ans=inf,L; char c[maxn];
struct Sam
{
int fa[maxn],ch[maxn][],maxlen[maxn],Last,cnt;
int a[maxn],b[maxn],num1[maxn],num2[maxn],np,nq,p,q;
Sam(){
Last=cnt=;
}
void add(int x)
{
np=++cnt; p=Last; Last=np;
maxlen[np]=maxlen[p]+; num1[np]++;//方法1
while(p&&!ch[p][x]) ch[p][x]=np,p=fa[p];
if(!p) fa[np]=;
else {
q=ch[p][x];
if(maxlen[p]+==maxlen[q]) fa[np]=q;
else {
nq=++cnt; maxlen[nq]=maxlen[p]+;
fa[nq]=fa[q]; fa[q]=fa[np]=nq;
memcpy(ch[nq],ch[q],sizeof(ch[nq]));
while(p&&ch[p][x]==q) ch[p][x]=nq,p=fa[p];
}
}
}
void sort()
{
for(int i=;i<=cnt;i++) a[maxlen[i]]++;
for(int i=;i<=cnt;i++) a[i]+=a[i-];
for(int i=;i<=cnt;i++) b[a[maxlen[i]]--]=i;
//for(int i=1,tp=1;i<=cnt;i++){
// tp=ch[tp][c[i]-'a']; num1[tp]++;
//}
//方法2
for(int i=cnt;i>=;i--) num1[fa[b[i]]]+=num1[b[i]];
}
void solve()
{
scanf("%s",c+); L=strlen(c+); p=;
for(int i=;i<=L;i++){
int x=c[i]-'a';
if(ch[p][x]) p=ch[p][x];
else {
while(p&&!ch[p][x]) p=fa[p];
if(!p) p=;
else p=ch[p][x];
}
num2[p]++;
}
for(int i=cnt;i>=;i--) num2[fa[b[i]]]+=num2[b[i]];
for(int i=;i<=cnt;i++)
if(num1[i]==&&num2[i]==)
ans=min(ans,maxlen[fa[i]]+);
if(ans==inf) ans=-;
}
}sam;
int main()
{
scanf("%s",c+); L=strlen(c+);
for(int i=;i<=L;i++) sam.add(c[i]-'a');
sam.sort(); sam.solve();
printf("%d\n",ans);
return ;
}
CodeForces-427D:Match & Catch (后缀自动机)的更多相关文章
- D. Match & Catch 后缀自动机 || 广义后缀自动机
http://codeforces.com/contest/427/problem/D 题目是找出两个串的最短公共子串,并且在两个串中出现的次数只能是1次. 正解好像是dp啥的,但是用sam可以方便很 ...
- codeforces 427D Match & Catch(后缀数组,字符串)
题目 参考:http://blog.csdn.net/xiefubao/article/details/24934617 题意:给两个字符串,求一个最短的子串.使得这个子串在两个字符串中出现的次数都等 ...
- CodeForces 427D Match & Catch
洛谷题目页面传送门 & CodeForces题目页面传送门 给定\(2\)个字符串\(a,b,|a|=n,|b|=m\),求最长的既在\(a\)中出现恰好\(1\)次又在\(b\)中出现恰好\ ...
- Codeforces 235C Cyclical Quest - 后缀自动机
Some days ago, WJMZBMR learned how to answer the query "how many times does a string x occur in ...
- Codeforces 452E Three Strings(后缀自动机)
上学期很认真地学了一些字符串的常用工具,各种 suffix structre,但是其实对后缀自动机这个部分是理解地不太透彻的,以致于看了师兄A这题的代码后,我完全看不懂,于是乎重新看回一些学习后缀自动 ...
- Codeforces 427D Match & Catch(后缀自动机)
[题目链接] http://codeforces.com/problemset/problem/427/D [题目大意] 给出一个两个字符串,求出最短且在两个字符串中唯一的公共子串. [题解] 以原字 ...
- CF #244 D. Match & Catch 后缀数组
题目链接:http://codeforces.com/problemset/problem/427/D 大意是寻找两个字符串中最短的公共子串,要求子串在两个串中都是唯一的. 造一个S#T的串,做后缀数 ...
- Codeforces.700E.Cool Slogans(后缀自动机 线段树合并 DP)
题目链接 \(Description\) 给定一个字符串\(s[1]\).一个字符串序列\(s[\ ]\)满足\(s[i]\)至少在\(s[i-1]\)中出现过两次(\(i\geq 2\)).求最大的 ...
- 后缀自动机(SAM)
*在学习后缀自动机之前需要熟练掌握WA自动机.RE自动机与TLE自动机* 什么是后缀自动机 后缀自动机 Suffix Automaton (SAM) 是一个用 O(n) 的复杂度构造,能够接受一个字符 ...
- CF 427D Match & Catch 求最短唯一连续LCS
题目来源:CF 427D Match & Catch 题意:给出2个字符串 求最短的连续的公共字符串 而且该字符串在原串中仅仅出现一次 思路:把2个字符串合并起来求height 后缀数组hei ...
随机推荐
- OO第三次作业总结(JML)
第三单元的课题是JML, 即java建模语言.JML是一种描述接的语言.通过前置条件和后置条件,描述一个模块的行为.本单元我们扮演一个项目中的一员,完成自己的一小部分工作,最终实现整个项目.而限制我们 ...
- Bone Collector II(01背包kth)
The title of this problem is familiar,isn't it?yeah,if you had took part in the "Rookie Cup&quo ...
- PTA 03-树2 List Leaves (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/666 5-4 List Leaves (25分) Given a tree, you ...
- [Go]指针操作
指针类型比较常见 type Dog struct { name string } func (dog *Dog) SetName (name string){ dog.name = name } 对于 ...
- PMU 简介
目录 1:PMIC2:Battery管理3:功耗4:常见问题5:参考文献 PMIC[MT6322] Source code structure Build option Battery char ...
- IIS文件存在但报404问题解决
遇到一个奇怪的问题,在IIS7.5中,一些样式和JS文件存在,但访问就是报404. 根据网上搜索到的解决方法,发现解决不了,不同同样的问题引起的. 网上解决: 1.没有配置合适的MIME信息,通过添加 ...
- 《effective C++》:条款37——绝不重新定义继承而来的缺省参数值
引子: 阿里的一道题: #include <IOSTREAM> using namespace std; class A{ public: ) { cout<<"a~ ...
- 从零开始写STL-二叉搜索树
二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若它的 ...
- System表空间大小有10Gb,使用率达到95%,
System表空间大小有10Gb,使用率达到95%,很好奇, 随后执行如下SQL,查看system表空间中使用空间最多的对象 SQL>SELECT * FROM DBA_SEGMENTS T W ...
- 【c++】面向对象程序设计之访问控制与继承
受保护的成员(protected): 1.和私有成员类似,受保护的成员对于类的用户来说是不可访问的 2.和共有成员类似,受保护的成员对于派生类的成员和友元来说是可访问的 3.派生类的友元只能通过派生类 ...