BZOJ4566 [Haoi2016] Find the same character "suffix array"

Source: Internet
Author: User

Topic

Given two strings, find the number of scenarios in which a substring is fetched in two strings, making the two substrings the same. Two scenarios are different if and only if the two
There is a different position in the substring.

Input format

Two lines, two string s1,s2, the length is n1,n2. 1 <=n1, n2<= 200000, with only lowercase letters in the string

Output format

Output an integer to indicate the answer

Input sample

Aabb

Bbaa

Output sample

10

Exercises

First consider the violence.
We enumerate the respective one suffixes of the two strings suffix (i) and suffix (j)
Their contribution to the answer is LCP (suffix (i), suffix (j))
So get a \ (O (n^3) \) algorithm

Of course if you know the suffix array, you can \ (o (1) \) request LCP, can be optimized to \ (o (n^2) \)

Of course, if you know the suffix array of routines, with a monotone stack sweep over height[] can do \ (O (NLOGN) \)"The main complexity of the suffix array"
Specifically this:
We know that the LCP between the two suffixes is the minimum value of height between them
If you give a position suffix, want to find this position before all the suffix and the location of the LCP and the sum of things, because the minimum value is a way past, so the front suffix of the LCP will not be larger than the back, so the whole is monotonous does not decline, can be processed with a monotone stack

Finally we only need to divide a, b string each with a monotone stack sweep two times the answer can be counted

#include <iostream>#include <cmath>#include <cstdio>#include <cstring>#include <algorithm>#define LL Long Long int#define REP (i,n) for (int i = 1; I <= (n); i++)#define Redge (U) for (int k = h[u],to; k; k = ed[k].nxt)#define BUG (s,n) for (int i = 1; I <= (n); i++) cout<<s[i]<< '; puts ("");using namespaceStdConst intMAXN =400005, MAXM =100005, INF =1000000000;inline intRead () {intout =0, flag =1;Charc = GetChar (); while(C < -|| C > $) {if(c = ='-') flag =-1; c = GetChar ();} while(c >= -&& C <= $) {out = (out <<3) + (out <<1) + C-' 0 '; c = GetChar ();}returnOut * FLAG;}intSa[maxn],rank[maxn],height[maxn],t1[maxn],t2[maxn],bac[maxn],n,m;CharS[MAXN];intLen LL ans;voidGetsa () {int*x = T1,*y = t2; m = the; for(inti =0; I <= m; i++) Bac[i] =0; for(inti =1; I <= N; i++) Bac[x[i] = s[i]]++; for(inti =1; I <= m; i++) Bac[i] + = bac[i-1]; for(inti = n; I i--) sa[bac[x[i]]--] = i; for(intK =1; K <= N; K <<=1){intp =0; for(inti = N-k +1; I <= N; i++) y[++p] = i; for(inti =1; I <= N; i++)if(Sa[i]-k >0) y[++p] = sa[i]-K; for(inti =0; I <= m; i++) Bac[i] =0; for(inti =1; I <= N; i++) bac[x[y[i]]]++; for(inti =1; I <= m; i++) Bac[i] + = bac[i-1]; for(inti = n; I        i--) sa[bac[x[y[i]]]--] = Y[i];        Swap (x, y); p = x[sa[1]] =1; for(inti =2; I <= N; i++) X[sa[i] = (Y[sa[i]] = = Y[sa[i-1]] && Y[sa[i] + K] = = y[sa[i-1] + K]? P: ++p);if(P >= N) Break;    m = p; } for(inti =1; I <= N; i++) Rank[sa[i]] = i; for(inti =1, k =0; I <= N; i++) {if(k) k--;intj = Sa[rank[i]-1]; while(S[i + K] = = S[j + K]) k++;    Height[rank[i]] = k; }}intSt[maxn],cnt[maxn],top; LL CAL[MAXN];voidSolve () { for(inti =1; I <= N; i++) {if(!height[i]) {top =0;Continue;}inttot = sa[i-1] <= len?1:0; while(Top && St[top] > Height[i]) tot + = cnt[top--];if(tot) st[++top] = height[i],cnt[top] = tot; Cal[top] = cal[top-1] + (LL) st[top] * (LL) cnt[top];if(Sa[i] > len) ans + = cal[top]; } top =0; for(inti =1; I <= N; i++) {if(!height[i]) {top =0;Continue;}inttot = sa[i-1] > Len?1:0; while(Top && St[top] > Height[i]) tot + = cnt[top--];if(tot) st[++top] = height[i],cnt[top] = tot; Cal[top] = cal[top-1] + (LL) st[top] * (LL) cnt[top];if(Sa[i] <= len) ans + = cal[top]; }}intMain () {scanf ("%s", S +1); Len = strlen (S +1); S[len +1] =' # '; scanf"%s", S + len +2); n = strlen (s +1);    Getsa ();    Solve (); cout << ans << endl;return 0;}

BZOJ4566 [Haoi2016] Find the same character "suffix array"

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.