• ACM Longest Repeated Sequence


    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;
    }
  • 相关阅读:
    城市联动
    Js-右键事件
    JS-键盘移动事件
    Js-点名器
    前端学习——HTML
    前端学习——前端基础
    Redis数据库学习
    2020系统综合实践 期末大作业
    #Nginx+Tomcat+Redis session共享
    2020系统综合实践 第7次实践作业
  • 原文地址:https://www.cnblogs.com/xiongqiangcs/p/3647496.html
Copyright © 2020-2023  润新知