Codeforces 427D Match & Catch 后缀自动机
来源:互联网 发布:ubuntu 14.04与16.04 编辑:程序博客网 时间:2024/05/16 16:14
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.
The first line of input contains s1 and the second line contains s2 (1 ≤ |s1|, |s2| ≤ 5000). Both strings consist of lowercase Latin letters.
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.
applepepperoni
2
loverdriver
1
bidhanroy
-1
testsetsesteeptes
3
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;}
- Codeforces 427D Match & Catch 后缀自动机
- Codeforces 427D Match & Catch 后缀自动机 或 后缀数组
- Codeforces 427D Match & Catch(后缀自动机)
- codeforces 427 D Match & Catch ( 后缀数组 )
- CodeForces 427D Match & Catch 后缀数组
- CodeForces 427 D.Match & Catch(后缀数组)
- Codeforces 427 D. Match & Catch
- CodeForces 427D-Match & Catch
- CodeForces 427D-Match & Catch
- CodeForces 427D Match & Catch
- cf 427D Match & Catch 后缀数组
- CF(427D-Match & Catch)后缀数组应用
- codeforces 123 D String(后缀自动机 SAM)
- codeforces 127D Password (后缀自动机 SAM)
- Codeforces Round #244 (Div. 2) D. Match & Catch
- Codeforces round 244(div.2) D match&catch【字符串hash】
- codeforces Round #92 D——后缀自动机
- Codeforces Beta Round #93 D. Password——后缀自动机
- Codeforces 279B Books(dp大法好)
- 2017不一样的大数据
- Python3与Python2版本的一点区别
- 新闻类缓存的SqlDao
- 关于VS环境下制作和使用静态库和动态库
- Codeforces 427D Match & Catch 后缀自动机
- 属性动画
- leetcode 620. Not Boring Movies(SQL,查询排序)38
- 条件随机场conditional random field
- Linux中线程的函数解析
- HDU-1969 Pie
- 相邻交换
- keras模型保存为tensorflow的二进制模型
- ZOJ1074