Poj 2774 Long long Message (suffix array)

Source: Internet
Author: User

Topic Links:

Poj 2774 Long Long Message

Title Description:

Give a A, a, two string to find the longest common substring?

Problem Solving Ideas:

The longest common substring of a A, B string can be converted to the longest common prefix of an array of string suffixes. Concatenate b strings behind a string, separating the R strings with ' $ '. Find the height of the R string array, the largest height[i] (satisfies sa[i] and sa[i-1] is not in the same text string is the answer. The time complexity is probably O (| a|+| b|) *log (| a|+| b|)). (Parade holiday used to engage in this, feeling still a little strange feeling, but adhering to the previous learning style, first write down the template, later in the water problem slowly realized good)

1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5 using namespacestd;6typedefLong LongLL;7 Const intMAXN =200010;8 9 intSA[MAXN], RANK[MAXN], HEIGHT[MAXN];Ten intT1[MAXN], T2[MAXN], R[MAXN], C[MAXN]; One BOOLCMP (int*STR,intAintBintk) A{//rank adjacent to the two strings, the first second key words are the same, rank -     returnSTR[A]==STR[B] && str[a+k]==str[b+K]; - } the intDa (int*STR,intNintm) - { -     int*x = T1, *y =T2, I, J; -n + +; +     //Cardinal Sort, -      for(i=0; i<m; i++) C[i] =0; +      for(i=0; i<n; i++) C[x[i] = Str[i]] + +; A      for(i=1; i<m; i++) C[i] + = c[i-1]; at      for(i=n-1; i>=0; i--) sa[--C[str[i]] =i; -      for(j=1; j<=n; j*=2) -     { -         //sort the second keyword with an SA array -         intp =0; -          for(i=n-j; i<n; i++) y[p++] = i;//You cannot add a string of length J, and the second keyword is the smallest in          for(i=0; i<n; i++)if(Sa[i] >= j) y[p++] = Sa[i]-J; -         //enumeration rank, a string with a starting point greater than J, to multiply to         //Update SA array +          for(i=0; i<m; i++) C[i] =0; -          for(i=0; i<n; i++) C[x[y[i]] + +; the          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]; $         //Update x ArrayPanax Notoginseng swap (x, y); -p =1, x[sa[0]] =0; the          for(i=1; i<n; i++) +X[sa[i]] = cmp (y, sa[i-1], Sa[i], j)? p1:p + +; A         if(P >=N) the              Break; +m =p; -     } $     //Calculate rank Array $      for(i=0; i<n; i++) -Rank[sa[i]] =i; -     //Calculating the height array theN--; -     intK =0;Wuyi      for(i=0; i<n; i++) the{//a point of lifting, height[rank] -         if(k) K--; Wuj = sa[rank[i]-1]; -          while(Str[i+k]==str[j+k]) k++; AboutHeight[rank[i]] =K; $     } - } - intMain () - { A     Charstr1[maxn/2], str2[maxn/2]; +      while(SCANF ("%s%s", STR1, str2)! =EOF) the     { -         intN1 =strlen (str1); $         intN2 =strlen (str2); the         intn = n1 + N2 +1; the          for(intI=0; i<n; i++) the         { the             if(I <N1) -R[i] =Str1[i]; in             Else if(i = =N1) theR[i] ='$'; the             Else AboutR[i] = str2[i-n1-1]; the         } theR[n] =0; theDa (r, N, -); +         intAns =0; -          for(intI=1; i<n; i++) the{//i is rankBayi             if(sa[i]<n1&&sa[i-1]>n1 | | sa[i]>n1&&sa[i-1]<N1) the                 //rank adjacent suffix string not in the same strings theAns =Max (ans, height[i]); -         } -printf ("%d\n", ans); the     } the     return 0; the}

Poj 2774 Long long Message (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.