CF (427d-match & Catch) suffix Array application

Source: Internet
Author: User

Test instructions: For two strings, find the shortest substring. The number of occurrences of this substring in two strings equals 1. The occurrence is defined as: capable of overlapping occurrences.


Solution: The application of the suffix array. Small enumeration length. Suppose a length len is valid: Then there must be this kind of sa[i] rank. The public prefix length of sa[i] and s[i+1] is greater than or equal to Len, and the public prefix length of sa[i] and [i-1] is less than Len, the same time sa[i+1] and [i+2] The public prefix length is less than Len, at the same time guaranteed sa[i] and sa[i+1] in two strings. The judge function is the skillful implementation of these inferences.


Code:

#include <iostream> #include <string> #include <cstring> #include <string.h> #include < stdio.h>using namespace Std;const int MAX = 200100;int N, num[max];int Sa[max], Rank[max], Height[max];//sa[i] Height[i] (suffix sa[i] and sa[i-1] the longest common prefix int wa[max], Wb[max], Wv[max], wd[max];//name Group Rank[i] Save Suffix (i) Rank from small to large in all suffixes. To put it simply, the suffix Array (SA) is "Who is the first row?" The rank Array (rank) is "What's your rank?" ” 。 Easy to see, the suffix array and the rank array are mutually inverse. int cmp (int *r, int A, int b, int l) {return r[a] = = R[b] && r[a+l] = = R[b+l];} void da (int *r, int n, int m)//multiplication algorithm 0 (NLGN).    {int I, J, p, *x = WA, *y = WB, *t;    for (i = 0; i < m; i + +) wd[i] = 0;    for (i = 0; i < n; i + +) Wd[x[i]=r[i]] + +;    for (i = 1; i < m; i + +) Wd[i] + = wd[i-1];    for (i = n-1; I >= 0; i-) sa[--wd[x[i]] [= i;        for (j = 1, p = 1; p < n; j *= 2, M = p) {for (P = 0, i = n-j; i < n; i + +) Y[p + +] = i;        for (i = 0; i < n; i + +) if (Sa[i] >= j) Y[p + +] = sa[i]-J; For (i = 0; i < n; i + +) wv[i] = x[y[i]];        for (i = 0; i < m; i + +) wd[i] = 0;        for (i = 0; i < n; i + +) Wd[wv[i]] + +;        for (i = 1; i < m; i + +) Wd[i] + = wd[i-1];        for (i = n-1; I >= 0; i-) sa[--wd[wv[i]] [= Y[i];  for (t = x, x = y, y = t, p = 1, x[sa[0]] = 0, i = 1; i < n; i + +) {X[sa[i]] = cmp (y, sa[i-1], Sa[i], j)?        P-1: P + +; }}}void calheight (int *r, int n)//height array.    {int I, j, k = 0;    for (i = 0; i < n; i + +) rank[sa[i]] = i;    for (i = 0; i < n; height[rank[i + +]] = k) {for (k K--: 0, j = sa[rank[i]-1]; r[i+k] = = R[j+k]; k + +);    }}int len1=0;int Len2=0;char s1[10000];char s2[10000];bool Judge (int n,int k) {int a = 0,b = 0;             for (int i = 0;i < n;i++) {if (Height[i] < K) {if (a = = 1 && b = = 1) return 1;        A = b = 0;        } if (Sa[i] >= 0 && sa[i] < len1) a++; if (Sa[i] > Len1 && Sa[i] < n-1) b++; } return 0;}    int main () {scanf ("%s%s", S1,S2);    Len1=strlen (S1);    Len2=strlen (S2);    for (int i=0; i<len1; i++) num[i]=s1[i]-' a ' +1;    num[len1]=28;    for (int i=0; i<len2; i++) num[i+len1+1]=s2[i]-' a ' +1;    Da (num,len1+len2+2,30);    Calheight (num,len1+len2+2);    int len=min (LEN1,LEN2);    int ans=-1;            for (int i=1; i<=len; i++) {if (Judge (len1+len2+2,i)) {ans=i;        Break    }} cout<<ans<<endl; return 0;} 20 5 19 20 19 5 20 19 5 19 28 20 5 5 16 20 5 19 0 0 0

CF (427d-match &amp; Catch) suffix Array application

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.