Link:
Http://codeforces.com/contest/427/problem/D
Test instructions
Give you two string s1,s2, find the shortest substring appearing in S1 and S2 and only once
Exercises
or the S1 and S2 to connect up, to seek LCP. The first thing to know is that the shortest length must be connected in the SA array,
This will only need to iterate over the LCP array and update ans.
ans = min (ans, max (lcp[i-1], lcp[i + 1]) + 1)
Code:
to intN, K; + intRANK[MAXN], TMP[MAXN]; - intSA[MAXN], LCP[MAXN]; the * BOOLCompare_sa (intIintj) { $ if(Rank[i]! = Rank[j])returnRank[i] <Rank[j];Panax Notoginseng Else { - intRI = i + k <= n? Rank[i + K]:-1; the intRJ = j + k <= n? Rank[j + K]:-1; + returnRI <RJ; A } the } + - voidConstruct_sa (stringSint*sa) { $n =s.length (); $ for(inti =0; I <= N; i++) { -Sa[i] =i; -Rank[i] = i < n? S[i]:-1; the } - for(k =1; K <= N; K *=2) {WuyiSort (SA, SA + n +1, Compare_sa); thetmp[sa[0]] =0; - for(inti =1; I <= N; i++) WuTmp[sa[i]] = tmp[sa[i-1]] + (COMPARE_SA (Sa[i-1], Sa[i])?1:0); - for(inti =0; I <= N; i++) Rank[i] =Tmp[i]; About } $ } - - voidCONSTRUCT_LCP (stringSint*sa,int*LCP) { - intn =s.length (); A for(inti =0; I <= N; i++) Rank[sa[i] =i; + inth =0; thelcp[0] =0; - for(inti =0; I < n; i++) { $ intj = Sa[rank[i]-1]; the if(H >0) h--; the for(; j + H < n && i + H < n; h++) the if(S[j + h]! = s[i + h]) Break; theLcp[rank[i]-1] =h; - } in } the the intMain () { AboutIos::sync_with_stdio (false), Cin.tie (0); the strings1, S2; theCIN >> S1 >>S2; the intN1 =s1.length (); + strings = S1 +"$"+S2; - Construct_sa (S, sa); the CONSTRUCT_LCP (S, SA, LCP);Bayi intAns =INF; theRep (I,1, n +1)if(Lcp[i] > Lcp[i-1] && Lcp[i] > lcp[i +1]) the if(! ((Sa[i] > N1) ^ (Sa[i +1] <N1 ))) -ans = min (ans, max (Lcp[i-1], Lcp[i +1]) +1); - if(ans = = INF) ans =-1; thecout << ans <<Endl; the return 0; the}
Codeforces Round #244 (Div. 2) D. Match & Catch suffix array