时间限制: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 ;
}

 

Longest Repeated Sequence【微软编程一小时-题目2】的更多相关文章

  1. 微软编程一小时 题目2: Longest Repeated Sequence

    题目2 : Longest Repeated Sequence 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 You are given a sequence of i ...

  2. ACM Longest Repeated Sequence

    Description You are given a sequence of integers, A = a1, a2, ... an. A consecutive subsequence of A ...

  3. 【微软编程一小时】题目1 : Arithmetic Expression

    时间限制:2000ms 单点时限:200ms 内存限制:256MB 描写叙述 Given N arithmetic expressions, can you tell whose result is ...

  4. Parentheses Sequence微软编程笔试

    描述 You are given a sequence S of parentheses. You are asked to insert into S as few parentheses as p ...

  5. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  6. [LeetCode] Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  7. Binary Tree Longest Consecutive Sequence

    Given a binary tree, find the length of the longest consecutive sequence path (连续的路径,不是从小到大). The pa ...

  8. LeetCode Binary Tree Longest Consecutive Sequence

    原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...

  9. 【leetcode】Longest Consecutive Sequence

    Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...

随机推荐

  1. 杭电ACM2085--核反应堆

    http://acm.hdu.edu.cn/showproblem.php?pid=2085 这也是一个简单的的递推. a[i][0] = 3*a[i-1][0]+2*a[i-1][1]; a[i][ ...

  2. C++ Bitsets

    C++ Bitsets给程序员提供一种位集合的数据结构.Bitsets使用许多二元操作符,比如逻辑和,或等. Constructors 创建新bitsets Operators 比较和赋值bitset ...

  3. SQL 面试题(一)

    问题来自于CSDN问答,练练SQL吧. 测试数据SQL代码: if OBJECT_ID('td_ls_2') is not null drop table td_ls_2 go if OBJECT_I ...

  4. JVM学习---JAVA内存

    一.JAVA运行时数据区域:JAVA中的运行时内存区域有的随着虚拟机进程的启动而存在,有的区域则是依赖用户线程的启动和结束而建立和销毁的.包括以下的几个区域. 图. JAVA虚拟机运行时数据区 1.程 ...

  5. JS预览图像将本地图片显示到浏览器上的代码

    js代码实现: 从file域获取本地图片url并将本地图片显示到浏览器上. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitio ...

  6. php获取系统信息的方法

    php获取系统信息的方法. 用 getenv函数进行处理: <?php $root = getenv('DOCUMENT_ROOT'); ////服务器文档根目录 $port = getenv( ...

  7. python_day2_homework_1(简单购物商城)

    '''简单购物商城(要求):1,商品展示,价格2,买,加入购物车3,付款,钱不够''' 1 #_*_ coding: utf-8 _*_ __author__ = 'A-rno' meu_list_1 ...

  8. openerp模块收藏 移除下拉选择列表中的“创建并编辑”链接(转载)

    移除下拉选择列表中的“创建并编辑”链接 原文:http://shine-it.net/index.php/topic,5990.0.html 有时希望下拉列表中列出的项是与主表某个字段关联的,用户只能 ...

  9. openerp学习笔记 调用工作流

    获取工作流服务:wf_service = netsvc.LocalService("workflow")删除对象对应记录的工作流:wf_service.trg_delete(uid ...

  10. 无法访问Fedora的samba共享

    配置好samba服务后,却发现windows无法访问.经过多次试验与fedora的防火墙有关系. 关闭防火墙: #service iptables stop 或清空规则: #iptables -F w ...