POJ 3415 Common substrings (number of common substrings with a length not less than K--suffix array + monotonic stack optimization)

Source: Internet
Author: User

Test instructions: given two strings A and B, the number of common substrings with a length of not less than K (which can be the same).
Example 1:
A= "xx", b= "xx", K=1, the number of common substrings with a length of not less than K is 5.
Example 2:

A = "Aababaa", B = "Abaabaa", k=2, the number of common substrings with a length of not less than K is 22.

Ideas:

If I suffix with the J suffix LCP length is l, in the case of L is not less than K, its contribution to the answer is L-K + 1. So we can concatenate two, add a wonderful separator in the middle, do the suffix array, and the value of the array of height to the suffix group, to ensure that the same group within the suffix LCP is not less than K. Obviously the answers between the different groups are independent, and we can deal with each group individually. So the question becomes: in each group, for each a suffix, calculate it before the suffix of B and its LCP and (in fact, Lcp-k + 1, which is said to be LCP), and then to each B suffix, calculate it before the suffix of a and the LCP and
Statistical time with the general violence to the complexity of O (n^2), here can be optimized with a monotonic stack: for example, A1,A2,A3,BX is four suffixes, they rank continuous, and the height of the monotonically increasing, then A1 and A2 LCP can be A1 and BX LCP, So monotonous time can side into the stack side statistical answers, but the details are more, such as in the stack is a suffix b and a suffix to be dealt with separately, not monotonous when, out of the stack until again monotonous.


Monotonic stack is also the first encounter, in fact, is to maintain a stack inside the elements are always monotonous ... http://blog.csdn.net/hopeztm/article/details/7868581 here is a monotone stack used in a classic DP


Oneself too weak, this problem with why can use the monotone stack optimization O (n) solve oneself at the beginning also do not understand, see Cxlove code looked for 2.5 hours to thoroughly understand.

. http://blog.csdn.net/acm_cxlove/article/details/7946967


6612k860msc++3114b#include<cstdio> #include <iostream> #include <cstring> #include <algorithm >using namespace Std;typedef long long ll;const int maxn = 2e5+100;int t1[maxn],t2[maxn],c[maxn];bool cmp (int *r,int A, int B,int l) {return r[a]==r[b]&&r[a+l]==r[b+l];}    void da (int str[],int sa[],int rank[],int height[],int n,int m) {n++;    int i,j,p,*x=t1,*y=t2;    for (i=0;i<m;i++) c[i]=0;    for (i=0;i<n;i++) c[x[i]=str[i]]++;    for (i=1;i<m;i++) c[i]+=c[i-1];    for (i=n-1;i>=0;i--) sa[--c[x[i]]]=i;;        for (j=1;j<=n;j<<=1) {p=0;        for (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<m;i++) c[i]=0;        for (i=0;i<n;i++) c[x[y[i]]]++;        for (i=1;i<m;i++) c[i]+=c[i-1];;        for (i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];        Swap (x, y);        P=1;        x[sa[0]]=0; for (i=1;i<n;i++) x[sa[i]]=cmp (y,sa[i-1],sa[i],j)?        p-1:p++; if (p>=n) BreAk    M=p;    } int k=0;    n--;    for (i=0;i<=n;i++) rank[sa[i]]=i;        for (i=0;i<n;i++) {if (k) k--;        J=SA[RANK[I]-1];        while (Str[i+k]==str[j+k]) k++;    Height[rank[i]]=k;    }}int Rank[maxn],height[maxn];char s1[maxn],s2[maxn];int r[maxn];int sa[maxn];int g;struct node{int cnt; int Val;};        Node S[maxn];int main () {while (scanf ("%d", &g), g) {scanf ("%s%s", S1,S2);        int L1=strlen (S1), L2=strlen (S2);        int n=l1+l2+1;        for (int i=0;i<l1;i++) r[i]=s1[i];        R[l1]= ' $ ';        for (int i=0;i<=l2;i++) r[i+l1+1]=s2[i];        Da (r,sa,rank,height,n,130);        ll Ans=0;        ll top=0,cnt,tot=0;            Within each group, the suffix for each B is calculated with the suffix of a before it lcp-k + 1 and for (int i=1;i<=n;i++) {cnt=0; if (height[i]<g) top=tot=0;                    The end of this group, the next set starts else {if (SA[I-1]&LT;L1)//If the previous one is a prefix, then there is a contribution to the answer lcp-g+1 { cnt++;                   tot+=height[i]-g+1; } while (Top!=0&&s[top-1].val>=height[i])//not monotonous so out of stack until monotonous {top--                    ;                    tot-=s[top].cnt* (S[top].val-height[i]);                cnt+=s[top].cnt;                } s[top].cnt=cnt;                S[top++].val=height[i]; if (SA[I]&GT;L1) Ans+=tot; Currently the suffix of B adds the preceding Tot to ans}}//In each group, for each a suffix, calculate the suffix of B before it lcp-k + 1 and Tot=0,top        = 0;            for (int i=1;i<=n;i++) {cnt=0;            if (height[i]<g) top=tot=0;                    else {if (SA[I-1]&GT;L1) {cnt++;                tot+=height[i]-g+1;                    } while (Top!=0&&s[top-1].val>=height[i]) {top--;                    tot-=s[top].cnt* (S[top].val-height[i]); Cnt+=s[top].cnt;                } S[top].val=height[i];                s[top++].cnt=cnt;            if (SA[I]&LT;L1) Ans+=tot;    }} printf ("%i64d\n", ans); } return 0;}





POJ 3415 Common substrings (number of common substrings with a length not less than K--suffix array + monotonic stack optimization)

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.