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 ...
随机推荐
- SQL 字符串处理函数大全
select语句中只能使用sql函数对字段进行操作(链接sql server),select 字段1 from 表1 where 字段1.IndexOf("云")=1;这条语句不对 ...
- B1299 [LLH邀请赛]巧克力棒 博弈论
这个题一看就是nim游戏的变形.每次先手取出巧克力就是新建一个nim,但假如先手取一个为0的而且无论后手怎么取剩下的都无法为零就行了.然后用dfs跑. 题干: Description TBL和X用巧克 ...
- MAC地址 初识
MAC地址 即物理地址/硬件地址 地址长度为48位,6字节. 格式为:00-23-5A-15-99-42 一个网卡对应一个MAC地址(比如笔记本,有线网卡有一个MAC地址,无线网卡也有一个MAC地址) ...
- 调取easyui -windows 返回值问题
//-------------------------------------------------------------------------------------------------- ...
- php 静态属性和静态变量
- SQL Sever语言 存储过程及触发器
存储过程:就像函数一样的会保存在数据库中-->可编程性-->存储过程 创建存储过程: 保存在数据库表,可编程性,存储过程create proc jiafa --需要的参数@a int,@b ...
- fcc html5 css 练习2
<form action="/submit-cat-photo" >action属性的值指定了表单提交到服务器的地址 <input type="text ...
- drupal 8——图片组(list)在前台的显示顺序在登录状态和非登录状态不同
问题描述:该页面是通过view来输出的,然而,登录状态下其页面中的图片组输出顺序是乱序的,而非登录状态下则根据id值升序输出. 原因:在原view配置页面中,没有配置默认的排序字段 解决方案:在vie ...
- 【译】x86程序员手册15-5.2页转换
5.2 Page Translation 页转换 In the second phase of address transformation, the 80386 transforms a linea ...
- Linux基础之网络协议
互联网通信原理 从物理层面来说,每台计算机在一开始都是彼此孤立的,为了实现信息的交流与共享,计算机之间必须要建立通信网络.例如人与人之间的交流,他们必须要共用一套语言系统,才能交流成功.计算机之间也是 ...