Codeforces 427D Match & Catch 后缀自动机

来源:互联网 发布:ubuntu 14.04与16.04 编辑:程序博客网 时间:2024/05/16 16:14

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, 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.

Examples
input
applepepperoni
output
2
input
loverdriver
output
1
input
bidhanroy
output
-1
input
testsetsesteeptes
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.



后缀自动机水题。

给两个字符串,求在两个串当中都仅出现一次的最短公共子串。


对两个串分别构造后缀自动机,利用dp求出每个状态的right集合大小。预处理后,沿着两个自动机走其中一个串的路径,当停在某个right集合大小都为1的状态时即可更新答案。


#include <cstdio>#include <iostream>#include <string.h>#include <string> #include <map>#include <queue>#include <deque>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#include <stack>#include <iomanip>#define mem0(a) memset(a,0,sizeof(a))#define meminf(a) memset(a,0x3f,sizeof(a))using namespace std;typedef long long ll;typedef long double ld;typedef double db;const int maxn=5005,maxk=26,inf=0x3f3f3f3f;  const ll llinf=0x3f3f3f3f3f3f3f3f;   const ld pi=acos(-1.0L);int dp[maxn*2],dp2[maxn*2],w[maxn*2],r[maxn*2];char a[maxn],b[maxn];class SAM {      public:      void init() {          num=last=0;          a[0].len=0;a[0].fa=-1;          for (int i=0;i<maxk;i++) a[0].son[i]=-1;      }      void update (int c) {          int now=++num,p;          a[now].len=a[last].len+1;          memset(a[now].son,-1,sizeof(a[now].son));          for (p=last;p!=-1&&a[p].son[c]==-1;p=a[p].fa)              a[p].son[c]=now;          if (p==-1) a[now].fa=0; else {              int q=a[p].son[c];              if (a[p].len+1==a[q].len) {                  a[now].fa=q;              } else {                  int ne=++num;                  a[ne].len=a[p].len+1;                  memcpy(a[ne].son,a[q].son,sizeof(a[q].son));                  a[ne].fa=a[q].fa;                  for (;p!=-1&&a[p].son[c]==q;p=a[p].fa)                       a[p].son[c]=ne;                  a[q].fa=a[now].fa=ne;              }          }          last=now;      }      int getnum() {    return num;    }    int getson(int n,int c) {    return a[n].son[c];    }    int getfa(int n) {    return a[n].fa;    }    int getlen(int n) {    return a[n].len;    }    private:      int num,last;      struct node{          int len,fa;          int son[maxk];      } a[maxn*2];  };  SAM sa,sb;  int main() {scanf("%s",a);scanf("%s",b);int lena,lenb,i,j,ans;lena=strlen(a);lenb=strlen(b);sa.init();for (i=0;i<lena;i++) {sa.update(a[i]-'a');}int m1=sa.getnum();    mem0(w);    for(i=0;i<=m1;i++) w[sa.getlen(i)]++;      for(i=0;i<=lena;i++) w[i]+=w[i-1];      for(i=m1;i>=0;i--) r[--w[sa.getlen(i)]]=i;      int now=0;     for (i=0;i<lena;i++) {now=sa.getson(now,a[i]-'a');dp[now]++;    }    for (i=m1;i>0;i--) {    int to=sa.getfa(r[i]);    if (to!=-1) dp[to]+=dp[r[i]];     }        sb.init();for (i=0;i<lenb;i++) {sb.update(b[i]-'a');}int m2=sb.getnum();    mem0(w);    for(i=0;i<=m2;i++) w[sb.getlen(i)]++;      for(i=0;i<=lenb;i++) w[i]+=w[i-1];      for(i=m2;i>=0;i--) r[--w[sb.getlen(i)]]=i;      now=0;     for (i=0;i<lenb;i++) {now=sb.getson(now,b[i]-'a');dp2[now]++;    }    for (i=m2;i>0;i--) {    int to=sb.getfa(r[i]);    if (to!=-1) dp2[to]+=dp2[r[i]];     }        ans=inf;    int u;for (i=0;i<lenb;i++) {now=u=0;for (j=i;j<lenb;j++) {now=sa.getson(now,b[j]-'a');u=sb.getson(u,b[j]-'a');if (now==-1) break;if (dp[now]==1&&dp2[u]==1) ans=min(ans,j-i+1);}}if (ans==inf) ans=-1;printf("%d\n",ans);return 0;}

原创粉丝点击