ACM Longest Repeated Sequence

OpenSoucre發表於2014-04-05

Description

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?

Input

Line 1: n (1 <= n <= 300), the length of A.
Line 2: the sequence, a1 a2 ... an (0 <= ai <= 100).

Output

The length of the longest repeated sequence.

Sample Input

5
2 3 2 3 2

Sample Output

2
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cstring>
#include <string>
#include <cstdio>
using namespace std;

int main()
{
    int n;
    cin >> n;
    vector<int> arr(n);
    for(int i = 0 ; i < n; ++ i)
        cin >> arr[i];
    int len = 0,size = arr.size();
    for(int i = 0; i < size; ++i)
    {
        for(int j = i+1; j < size ; ++j)
        {
            int k = 0;
            while(i+k < j && j+k < size){
                if(arr[i+k] == arr[j+k]){
                    k++;
                }else{
                    break;
                }
            }
            len = max(k,len);
        }
    }
    cout<< len<<endl;
}

 

相關文章