程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> UVA 題目1223 - Editor(後綴數組求出現次數超過兩次的最長子串的長度)

UVA 題目1223 - Editor(後綴數組求出現次數超過兩次的最長子串的長度)

編輯:C++入門知識

UVA 題目1223 - Editor(後綴數組求出現次數超過兩次的最長子串的長度)


Mr. Kim is a professional programmer. Recently he wants to design a new editor which has as many 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 commercial 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 many 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 string S , find the longest substring in text string S such 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 consists of T test cases. The number of test cases T is given in the first line of the input. For each test case, a text string S is 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 string S such that the substring appears at least twice.

 

Sample Input

 

3 
abcdefghikjlmn 
abcabcabc 
abcdabcabb

 

Sample Output

 

0 
6 

3

做這麼多後綴數組題以來,這個應該是最水的了吧

ac代碼

0ms過

 

#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

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved