ZOJ 3199 Longest repeated Substring

Source: Internet
Author: User

Longest repeated substringtime limit:5000msmemory limit:32768kbthis problem would be judged onZju. Original id:3199
64-bit integer IO format: %lld Java class name: Main

Write a program that takes a string and returns length of the longest repeated substring. A repeated substring is a sequence of the characters that's immediately followed by itself.

For example, given "Mississippi", the longest repeated substring are "ISS" or "SSI" (not "ISSI").
Given "Massachusetts", the longest repeated substring would be either "s" or "T".
Given "Maine", the longest repeated substring is "" (The empty string).

Input

The first line of the input contains a single integer T, the number of the test cases.

Each of the following T lines, is exactly one string of lowercase charactors.

The length of each string are at 50000 characters.

Output

For each test case, print the length of the longest repeated Substring.

Sample Input

2aaabcabcab

Sample Output

30
SourceZOJ Monthly, May 2009AuthorPENG, PENG problem solving: suffix array, this problem seems to be a suffix self-motivation is not a direct explosion
1#include <bits/stdc++.h>2 using namespacestd;3 Const intMAXN =100010;4 CharS[MAXN];5 intSA[MAXN],T[MAXN],T2[MAXN];6 intHeight[maxn],rk[maxn],c[maxn],n;7 voidBuild_sa (intm) {8     intI,*x = T,*y =T2;9      for(i =0; I < m; ++i) C[i] =0;Ten      for(i =0; I < n; ++i) C[x[i] = s[i]]++; One      for(i =1; I < m; ++i) C[i] + = c[i-1]; A      for(i = n1; I >=0; -i) sa[--c[x[i]] =i; -  -      for(intK =1; K <= N; K <<=1) { the         intp =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 =1; I < m; ++i) C[i] + = c[i-1]; A          for(i = n1; I >=0; -i) sa[--c[x[y[i] []] =Y[i]; at swap (x, y); -x[sa[0]] =0; -          for(p = i =1; I < n; ++i) -             if(Y[sa[i]] = = y[sa[i-1]] && y[sa[i]+k] = = y[sa[i-1]+K]) -X[sa[i]] = P1; -             ElseX[sa[i]] = p++; in         if(P >= N) Break; -m =p; to     } + } - voidgetheight () { the     intI,j,k =0; *      for(i =0; I < n; ++i) Rk[sa[i] =i; $      for(i =0; I < n; ++i) {Panax Notoginseng         if(k)--K; -j = sa[rk[i]-1]; the          while(i + K < n && J + K < n && s[i+k] = = S[j+k]) + +K; +Height[rk[i]] =K; A     } the } + intMain () { -     intKase; $scanf"%d",&Kase); $      while(kase--){ -scanf"%s", s); -n = strlen (s) +1; theBuild_sa ( -); - getheight ();Wuyi         intRET =0; the          for(inti =2; I < n; ++i) { -             intTMP =Height[i]; Wu             if(ABS (sa[i-1]-sa[i]) = = TMP && tmp > RET) ret =tmp; -              for(intj = i +1; J < N; ++j) { AboutTMP =min (tmp,height[j]); $                 if(TMP <= ret) Break; -                 if(ABS (sa[i-1]-sa[i]) = = TMP && tmp > RET) ret =tmp; -             } -         } Aprintf"%d\n", ret); +     } the     return 0; -}
View Code

ZOJ 3199 Longest repeated Substring

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.