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

时间限制: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 = 0;
    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 = 1;

    for(k = len / 2; k >= 2; k--)
    {
        for(i = 0; 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[301] = {0}, n;
    cin >> n;
    if(n <= 0 || n > 300)
        exit(0);
    for(int i = 0; i < n; i++) cin >> s[i];
    cout << LRS(s, n) << endl;

    return 0;
}
 
 

 

 

转载于:https://www.cnblogs.com/codemylife/p/3653386.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值