Question 1223-Editor (suffix array to find the length of the longest substring that appears more than twice)

Source: Internet
Author: User

Question 1223-Editor (suffix array to find the length of the longest substring that appears more than twice)

Mr. kim is a professional programmer. recently he wants to design a new editor which has functions as possible. most editors support a simple search function that finds one occurrence (or all occurrences successively) of a query pattern string in the text.

He observed that the search function in specified cial editors does nothing if no query pattern is given. his idea of a new search function regards each substring of the given text as a query pattern string itself and his new function finds another occurrence in the text. the problem is that there can be occurrences of your substrings in the text. so, Mr. kim decides that the new function finds only occurrences of the longest substring in the text in order to remedy the problem. A formal definition of the search function is as follows:

Given a text stringS, Find the longest substring in text stringSSuch that the substring appears at least twice. The two occurrences are allowed to overlap.

 

Input

Your program is to read from standard input. The input consistsTTest cases. The number of test casesTIs given in the first line of the input. For each test case, a text stringSIs given in one line. For every string, the length is less than or equal to 5,000 and the alphabet is the set of lowercase English characters.

 

Output

Your program is to write to standard output. Print exactly one line for each test case. Print the length of the longest substring in text stringSSuch that the substring appears at least twice.

 

Sample Input

 

3 abcdefghikjlmn abcabcabc abcdabcabb

 

Sample Output

 

0 6 

3

This should be the best since I have done so many suffix array questions.

Ac code

Over 0 ms

 

#include
 
                #include
  
                 #include              #include
   
                 #define min(a,b) (a>b?b:a)          #define max(a,b) (a>b?a:b)       #define N 1000005         using namespace std;            char str[5010];          int sa[5010],Rank[5010],rank2[5010],height[5010],c[5010],*x,*y,s[5010],k;   void cmp(int n,int sz)        {            int i;            memset(c,0,sizeof(c));            for(i=0;i
    
     =0;i--)                sa[--c[x[y[i]]]]=y[i];        }        void build_sa(char *s,int n,int sz)        {            x=Rank,y=rank2;            int i,j;            for(i=0;i
     
      =len)                        y[yid++]=sa[i]-len;                    cmp(n,sz);                swap(x,y);                x[sa[0]]=yid=0;                for(i=1;i
      
       =n) break; } for(i=0;i
       
        

 

 

Related Article

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.