Poj 2774 Long Message (suffix array)

Source: Internet
Author: User

Poj 2774 Long Message (suffix array)
??

Given two strings A and B, calculate the longest common substring.

Train of Thought: concatenate two strings and separate them with an unused symbol,

Therefore, the answer is to satisfy the height value of the Suffix in different strings with the highest height value.

 

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        # Include
       # Include
        
          # Include
         
           # Include
          
            # Include
           # Include
            
              # Define eps 1e-6 # define LL long using namespace std; const int maxn = 200000 + 200; // const int INF = 0x3f3f3f; char str1 [100010], str2 [100010]; struct SuffixArray {int s [maxn]; // original character array (the last character must be 0, and the previous character must be not 0) int sa [maxn]; // suffix array. sa [0] Must be n-1, that is, the last character int rank [maxn]; // rank array int height [maxn]; // height array int t [maxn], t2 [maxn], c [maxn]; // auxiliary array int n; // void clear () {n = 0; memset (sa, 0, sizeof (sa);} // m is the maximum character value plus 1 .!!! Set s and n void build_sa (int m) {int I, * x = t, * y = t2; for (I = 0; I <m; I ++) c [I] = 0; for (I = 0; I <n; I ++) c [x [I] = s [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 (int k = 1; k <= n; k <= 1) {int p = 0; for (I = n-k; I <n; I ++) y [p ++] = I; for (I = 0; I <n; I ++) if (sa [I]> = k) y [p ++] = sa [I]-k; for (I = 0; I <m; I ++) c [I] = 0; for (I = 0; I <N; I ++) c [x [y [I] ++; for (I = 0; 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] = y [sa [I-1] = y [sa [I] & y [sa [I-1] + k] = y [sa [I] + k]? P-1: p ++; if (p> = n) break; m = p ;}} void build_height () {int I, j, k = 0; 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 (s [I + k] = s [j + k]) k ++; height [rank [I] = k ;}} sa; void init () {sa. clear (); int len1 = strlen (str1), len2 = strlen (str2); for (int I = 0; I <len1; I ++) sa. s [I] = str1 [I]-'A' + 1; for (int I = 0; I <len2; I ++) sa. s [len1 + I + 1] = str2 [I]-'A' + 1; sa. s [len1] = 27; sa. s [len1 + len2 + 1] = 0; sa. n = len1 + len2 + 2; sa. build_sa (30); sa. build_height ();} void solve () {int ans = 0; int len1 = strlen (str1); for (int I = 1; I <sa. n; I ++) {if (sa. height [I]> ans) if (sa. sa [I-1]
             
               Len1 | sa. sa [I]
              
                Len1) ans = sa. height [I];} cout <ans <endl;} int main () {// freopen(input.txt, r, stdin); while (scanf (% s, str1, str2) = 2) {init (); solve ();} return 0 ;}
              
             
            
          
         
        
      
     
    
   
  
 
 

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.