Longest Repeated Sequence【微软编程一小时-题目2】

时间:2021-07-05 09:41:20
时间限制: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>

using namespace std;

//判断存不存在长度为k的子串
bool isExist(int a[], int beg, int j, int size, int k)
{
int len = ;
while(beg < size && j < size && a[beg++] == a[j++])
{
++len;
if(len == k)
return true;
}
return false;
} //Longest Repeated Sequence
int LRS(int arr[], int len)
{
int k, maxlen, i, j;
maxlen = ; for(k = len / ; k >= ; k--)
{
for(i = ; i < len - k; ++i)
{
for( j = i + k; j < len; ++j)
{
if(isExist(arr, i, j, len, k) == true)
{
maxlen = k;
return maxlen;
}
}
}
}
return maxlen;
} int main()
{
int s[] = {}, n;
cin >> n;
if(n <= || n > )
exit();
for(int i = ; i < n; i++) cin >> s[i];
cout << LRS(s, n) << endl; return ;
}