Longest Repeated Substring

Time Limit: 5000ms
Memory Limit: 32768KB

This problem will be judged on ZJU. 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 characters that is immediately followed by itself.

For example, given "Mississippi", the longest repeated substring is "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 test cases.

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

The length of each string is at most 50000 characters.

Output

For each test case, print the length of the Longest Repeated Substring.

Sample Input

2
aaabcabc
ab

Sample Output

3
0
 

Source

Author

PENG, Peng
 
解题:后缀数组,这个题貌似后缀自动机不好搞
 
直接爆
 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
char s[maxn];
int sa[maxn],t[maxn],t2[maxn];
int height[maxn],rk[maxn],c[maxn],n;
void build_sa(int m) {
int i,*x = t,*y = t2;
for(i = ; i < m; ++i) c[i] = ;
for(i = ; i < n; ++i) c[x[i] = s[i]]++;
for(i = ; i < m; ++i) c[i] += c[i-];
for(i = n-; i >= ; --i) sa[--c[x[i]]] = i; for(int k = ; k <= n; k <<= ) {
int p = ;
for(i = n-k; i < n; ++i) y[p++] = i;
for(i = ; i < n; ++i)
if(sa[i] >= k) y[p++] = sa[i] - k;
for(i = ; i < m; ++i) c[i] = ;
for(i = ; i < n; ++i) c[x[y[i]]]++;
for(i = ; i < m; ++i) c[i] += c[i-];
for(i = n-; i >= ; --i) sa[--c[x[y[i]]]] = y[i];
swap(x,y);
x[sa[]] = ;
for(p = i = ; i < n; ++i)
if(y[sa[i]] == y[sa[i-]] && y[sa[i]+k] == y[sa[i-]+k])
x[sa[i]] = p-;
else x[sa[i]] = p++;
if(p >= n) break;
m = p;
}
}
void getHeight() {
int i,j,k = ;
for(i = ; i < n; ++i) rk[sa[i]] = i;
for(i = ; i < n; ++i) {
if(k) --k;
j = sa[rk[i]-];
while(i + k < n && j + k < n && s[i+k] == s[j+k]) ++k;
height[rk[i]] = k;
}
}
int main(){
int kase;
scanf("%d",&kase);
while(kase--){
scanf("%s",s);
n = strlen(s) + ;
build_sa();
getHeight();
int ret = ;
for(int i = ; i < n; ++i){
int tmp = height[i];
if(abs(sa[i-] - sa[i]) == tmp && tmp > ret) ret = tmp;
for(int j = i + ; j < n; ++j){
tmp = min(tmp,height[j]);
if(tmp <= ret) break;
if(abs(sa[i-] - sa[i]) == tmp && tmp > ret) ret = tmp;
}
}
printf("%d\n",ret);
}
return ;
}

ZOJ 3199 Longest Repeated Substring的更多相关文章

  1. LeetCode 1156. Swap For Longest Repeated Character Substring

    原题链接在这里:https://leetcode.com/problems/swap-for-longest-repeated-character-substring/ 题目: Given a str ...

  2. 【leetcode】1156. Swap For Longest Repeated Character Substring

    题目如下: Given a string text, we are allowed to swap two of the characters in the string. Find the leng ...

  3. 最长回文子串-LeetCode 5 Longest Palindromic Substring

    题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  4. leetcode--5. Longest Palindromic Substring

    题目来自 https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the long ...

  5. [LeetCode] Longest Palindromic Substring 最长回文串

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  6. No.005:Longest Palindromic Substring

    问题: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...

  7. SPOJ LCS2 - Longest Common Substring II

    LCS2 - Longest Common Substring II A string is finite sequence of characters over a non-empty finite ...

  8. Leetcode Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  9. 【leedcode】 Longest Palindromic Substring

    Given a , and there exists one unique longest palindromic substring. https://leetcode.com/problems/l ...

随机推荐

  1. [模板]FWT

    写起来和fft很像,这里放个板子. 代码: #include<iostream> #include<cstdio> #include<cmath> #include ...

  2. kindeditor上传文件的使用

    在线富文本编辑器kindeditor配置(.Net Framework 3.5)   下载地址:http://kindeditor.net/down.php 解压放在项目要目录下, 在Bin目录下添加 ...

  3. Coursera公开课-Machine_learing:编程作业2

    第三周编程作业:Logistic Regression 代码包在gitlab上:https://gitlab.com/luntai/Machine_Learning

  4. [转]linux之pr命令

    转自:http://www.bitscn.com/plus/view.php?aid=6638 本文介绍如何使用Linux的pr命令将大文件分割成多个页面进行打印,并在每个页面上加上标题. Linux ...

  5. 在 kafka 的 broke 和 client 之间加入 SSL 双向认证

    在 kafka 的 broke 和 client 之间加入 SSL 双向认证https://blog.csdn.net/hohoo1990/article/details/79110031 kafka ...

  6. [ BZOJ 3038 & 3211 / SPOJ GSS4 ] 上帝造题七分钟2 / 花神游历各国

    \(\\\) \(Description\) 给出一个长度为\(N\)的数列,共进行\(M\)次操作: \(1\ L\ R\):查询\([L,R]\)区间和. \(2\ L\ R\):对\([L,R] ...

  7. ie9以下的浏览器兼容性问题

    .bind不兼容的问题Function.prototype.bind = function () { var fn = this, args = Array.prototype.slice.call( ...

  8. python 求一个文件中每个字符出现的次数

    import pprint import collections filename = input('Input filename') with open(filename) as info: cou ...

  9. 详解HashMap数据结构实现

    HashMap的设计是由数组加链表的符合数据结构,在这里用自己的语言以及结合源码去总结一下,如果有不对的地方希望评论指正,先拱手谢谢. HashMap是日常中非常常用的一种数据结构,我们要想深入了解学 ...

  10. C# 控件调整

    //最大化 this.WindowState = FormWindowState.Maximized; //去掉标题栏 this.FormBorderStyle = FormBorderStyle.N ...