"Hdoj" 1403

Source: Internet
Author: User

The suffix array is twice times more solvable.

1#include <cstdio>2#include <cstring>3#include <cstdlib>4 5 #defineMAXM 286 #defineMAXN 1000107 8 intwa[maxn*2];9 intwb[maxn*2];Ten intwv[maxn*2]; One intws[maxn*2]; A CharS[MAXN]; - intstr[maxn*2], sa[maxn*2]; - intheight[maxn*2], rank[maxn*2]; the  - BOOLcmpint*r,intAintBintl) { -     returnR[A]==R[B] && r[a+l]==r[b+l]; - } +  - voidDaint*r,int*sa,intNintm) { +     intI, J, *x = WA, *y = WB, *T; A     intp; at      -      for(i=0; i<m; ++i) Ws[i] =0; -      for(i=0; i<n; ++i) ws[x[i]=r[i]]++; -      for(i=1; i<m; ++i) ws[i] + = ws[i-1]; -      for(i=n-1; i>=0; -i) sa[--ws[x[i]] =i; -      for(j=1, p=1; j<n; j*=2, m=p) { in          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; to          for(i=0; i<n; ++i) Wv[i] =X[y[i]]; +          for(i=0; i<m; ++i) Ws[i] =0; -          for(i=0; i<n; ++i) ws[wv[i]]++; the          for(i=1; i<m; ++i) ws[i] + = ws[i-1]; *          for(i=n-1; i>=0; -i) sa[--ws[wv[i]] =Y[i]; $          for(T=x, X=y, Y=t, p=1, x[sa[0]]=0, i=1; i<n; ++i)Panax NotoginsengX[sa[i]] = cmp (y, sa[i-1], Sa[i], j)? P1:p + +; -     } the } +  A voidCalheight (int*r,int*sa,intN) { the     intI, J, k=0; +      -      for(i=1; 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) -             /* do nothing*/; - } the  - voidPrintrank (intN) {Wuyi     inti; the      -printf"\nprint rank...\n"); Wu      for(i=0; i<n; ++i) -printf"%d", Rank[i]); Aboutprintf"\ n"); $ } -  - voidPrintsa (intN) { -     inti; A      +printf"\nprint sa...\n"); the      for(i=0; i<n; ++i) -printf"%d", Sa[i]); $printf"\ n"); the } the  the voidPrintheight (intN) { the     inti; -      inprintf"\nprint height...\n"); the      for(i=0; i<n; ++i) theprintf"%d", Height[i]); Aboutprintf"\ n"); the } the  the intMain () { +     intL1, L2, L; -     intI, J, K; the     intans;Bayi      the #ifndef Online_judge theFreopen ("data.in","R", stdin); - #endif -  the      while(SCANF ("%s", s)! =EOF) { theL =0; the          for(l1=0; S[L1]; l1++) thestr[l++] = s[l1]-'a'+2; -str[l++] =1;//Split Strings thescanf"%s", s); the          for(l2=0; S[L2]; l2++) thestr[l++] = S[l2]-'a'+2;94STR[L] =0; theDa (str, SA, L +1, MAXM); the calheight (str, SA, l); the #ifndef Online_judge98Printrank (L +1); AboutPrintsa (L +1); - printheight (l);101     #endif102Ans =-1;103          for(i=2; i<=l; ++i) {104             if(Height[i] >ans) { the                 if((sa[i-1]&LT;L1 && sa[i]>l1) | | (sa[i-1]&GT;L1 && sa[i]<L1))106Ans =Height[i];107             }108         }109printf"%d\n", ans); the     }111  the 113     return 0; the}

"Hdoj" 1403

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.