题目2 : Longest Repeated Sequence
时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
You are given a sequence of integers, A = a1, a2, ... an. A consecutive subsequence of A (say ai, ai+1 ... aj) is called a "repeated sequence" if it appears more than once in A (there exists some positive k that ai+k = ai, ai+k+1 = ai+1, ... aj+k = aj) and its appearances are not intersected (i + k > j).
Can you find the longest repeated sequence in A?
输入
Line 1: n (1 <= n <= 300), the length of A.
Line 2: the sequence, a1 a2 ... an (0 <= ai <= 100).
输出
The length of the longest repeated sequence.
样例输入
5
2 3 2 3 2
样例输出
2
求解
判断时,要注意的是子串不能重叠。
另外,以下代码是暴力求解的,没有在比赛中提交过。
#include <iostream> #include <cstdio> using namespace std; int func(char* str, int len) { int rst = 0; int m, n, length; for(int i = 0; i != len - 1; ++i) for(int span = 1; span != len - i; ++span) { length = 0; for(m = i, n = i + span; length != span && str[m] == str[n]; ++m, ++n, ++length); rst = length > rst ? length : rst; } return rst; } int main(){ int N; char* str = new char[N]; scanf("%d", &N); for(int i = 0; i != N; ++i) cin >> str[i]; cout << func(str, N) << endl; delete[] str; return 0; }