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 ...
随机推荐
- php-fpm.conf
[global]pid = /usr/local/php/var/run/php-fpm.piderror_log = /usr/local/php/var/log/php-fpm.loglog_le ...
- 【记录】新建Cordova项目出现ios-deploy找不到的问题
按老流程 Cordova create helloApp Cordova platform add ios 之前一般这种操作之后就能有执行的iOS目录了,像这样 然后 Cordova build ...
- SQLSERVER金额转换成英文大写的函数
CREATE FUNCTION [dbo].[f_num_eng] (@num numeric(15,2)) RETURNS varchar(400) WITH ENCRYPTION AS BEGIN ...
- 金明的预算方案(codevs 1155)
题目描述 Description 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些物品,怎么布置,你说了算,只 ...
- java打开本地应用程序(调用cmd)---Runtime用法详解
有时候我们需要借助java程序打开电脑自带的一些程序,可以直接打开或者借助cmd命令窗口打开一些常用的应用程序或者脚本,在cmd窗口执行的命令都可以通过这种方式运行. 例如: package cn.x ...
- Git学习之常见错误 git push 失败
Git学习之常见错误 git push 失败 问题描述: git push Counting objects: , done. Delta compression using up to thread ...
- Java描述符(修饰符)的类型
以下内容引用自http://wiki.jikexueyuan.com/project/java/modifier-types.html: 描述符(修饰符)是添加到那些定义中来改变他们的意思的关键词.J ...
- firedac数据集的序列和还原
procedure TForm1.Button1Click(Sender: TObject);var stream, stream2: TMemoryStream; buf: TBytes;begin ...
- win7右下角无线网图标显示未连接,但是实际上已连接上,也能上网
首先,要确实是不是服务启动的问题,方法很简单,重新启动电脑就可以. 如果问题依旧,那么按下Win+R快捷键,输入“services.msc”,打开服务界面. 然后会看到右侧窗口出现好多设置项,找到“R ...
- [RxJS] Implement RxJS `concatMap` by Waiting for Inner Subscriptions to Complete
Unlike mergeMap and switchMap, concatMap focuses on when "inner" subscriptions "compl ...