ZOJ 3199 Longest Repeated Substring
Longest Repeated Substring
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
#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的更多相关文章
- LeetCode 1156. Swap For Longest Repeated Character Substring
原题链接在这里:https://leetcode.com/problems/swap-for-longest-repeated-character-substring/ 题目: Given a str ...
- 【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 ...
- 最长回文子串-LeetCode 5 Longest Palindromic Substring
题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- leetcode--5. Longest Palindromic Substring
题目来自 https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the long ...
- [LeetCode] Longest Palindromic Substring 最长回文串
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- No.005:Longest Palindromic Substring
问题: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...
- SPOJ LCS2 - Longest Common Substring II
LCS2 - Longest Common Substring II A string is finite sequence of characters over a non-empty finite ...
- Leetcode Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- 【leedcode】 Longest Palindromic Substring
Given a , and there exists one unique longest palindromic substring. https://leetcode.com/problems/l ...
随机推荐
- 使用自定义的AlertDialog。
使用自定义的AlertDialog.1.核心的方法是setView(View view):把自己的view放置到dialog中填充.2.setCanceledOnTouchOutside(boolea ...
- MSP430 使用时 RST引脚一定拉高
因为这次省去了RESET按键,RST没做任何处理,程序下载完之后插上电池无法运行,最终原因:RST引脚没有拉高.呼呼切记啊
- 【高德地图API】VS2012或者VS2013添加高德地图v2.1.1版本SDK失败
可能由于v2.1.1版本SDK可能是在Win8.1环境下编译[这里有许多的原因,系统升级,安装VS2013等等] 有童鞋在操作正常的情况下添加SDK失败,提示版本不兼容. 如下图: 编辑项目 *.c ...
- Linux特殊符号及基础正则表达式
第1章 特殊符号 1.1 引号系列 1.1.1 单引号 所见即所得 单引号里面的内容会原封不动的输出 [root@oldboyedu50-lnb ~]# echo 'oldboy $LANG $PS ...
- JavaScript Date 日期操作
在前端工程师的道路上,可能有时候需要与时间来打交道,下面我们来看一下日常对日期的操作,今天我们介绍的是Day.js插件 Day.js 是一个轻量的处理时间和日期的 JavaScript 库,和 Mom ...
- mysql 数据去重
update ptop_investrecord set delflag = 1 where cid = 250 and uid = 92569 and delflag = 0 and progr ...
- 引水工程 Kruskal + Prim
Kruskal题解 : 以案例输入为例 有五个缺水地区 , 这个个缺水地区之间建立联系的费用已经给出 并且之间水库的费用也已经给出 , 自己水库也已看为 是另一个 点 , 这样就有了 6 个点 , 这 ...
- $P2121 拆地毯$
\(problem\) \(kruskal\)的模板题. #ifdef Dubug #endif #include <bits/stdc++.h> using namespace std; ...
- Linux 下 Solr的搭建与使用(建议jdk1.8以上)
官方表示solr5之后的版本不再提供对第三方容器的支持(不提供war包了). “旧式”solr.xml格式不再支持,核心必须使用core.properties文件定义. 使用第三方容器的需要自己手动修 ...
- Django总结二
- 创建app应用与ORM操作 - Django项目project - app(应用) -> 不同的功能放在不同的app中 - 命令 : - 创建app python manage.py sta ...