我是靠谱客的博主 高大信封,这篇文章主要介绍DS--最长重复子串,现在分享给大家,希望可以做个参考。

题目描述

求串的最长重复子串长度(子串不重叠)。例如:abcaefabcabc的最长重复子串是串abca,长度为4。

输入

测试次数t

t个测试串

输出

对每个测试串,输出最长重复子串长度,若没有重复子串,输出-1.

样例输入

复制代码
1
2
3
4
5
3 abcaefabcabc szu0123szu szuabcefg

样例输出

复制代码
1
2
3
4
4 3 -1

思路:

实现代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include<iostream> #include<string> using namespace std; void GetNext(string p, int next[]) { next[0] = -1; int i, j; i = 0; j = -1; while (i<p.length()) { if (j == -1 || p[i] == p[j]) { ++i; ++j; next[i] = j; } else j = next[j]; } } int main() { int n; cin >> n; string s; string re; int i, j = -1,k; int max = 0; while (n--) { int next[100] = { 0 }; max = -1; cin >> s; re=s; for (i = 0, k = s.length() - 1; k >= 0; i++, k--) { re[i] = s[k]; } GetNext(s, next); for (i = 0; i <= s.length(); ++i) { if (next[i]>max && next[i]<=s.length()/2) max = next[i]; } GetNext(re, next); for (i = 0; i <= re.length(); ++i) { if (next[i]>max && next[i]<=re.length()/2) max = next[i]; } if (max == 0) cout << j << endl; else { cout << max << endl; } } return 0; }

最后

以上就是高大信封最近收集整理的关于DS--最长重复子串的全部内容,更多相关DS--最长重复子串内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(49)

评论列表共有 0 条评论

立即
投稿
返回
顶部