Longest Repeated Sequence【微软编程一小时-题目2】
描述
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】的更多相关文章
- 微软编程一小时 题目2: Longest Repeated Sequence
题目2 : Longest Repeated Sequence 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 You are given a sequence of i ...
- ACM Longest Repeated Sequence
Description You are given a sequence of integers, A = a1, a2, ... an. A consecutive subsequence of A ...
- 【微软编程一小时】题目1 : Arithmetic Expression
时间限制:2000ms 单点时限:200ms 内存限制:256MB 描写叙述 Given N arithmetic expressions, can you tell whose result is ...
- Parentheses Sequence微软编程笔试
描述 You are given a sequence S of parentheses. You are asked to insert into S as few parentheses as p ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- Binary Tree Longest Consecutive Sequence
Given a binary tree, find the length of the longest consecutive sequence path (连续的路径,不是从小到大). The pa ...
- LeetCode Binary Tree Longest Consecutive Sequence
原题链接在这里:https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/ 题目: Given a binary t ...
- 【leetcode】Longest Consecutive Sequence
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
随机推荐
- 9个超绚丽的HTML5 3D图片动画特效
在Web 1.0时代,我们的网页中图片数量非常少,而且都是以静态图片为主.HTML5的出现,推动了Web 2.0的发展,同时也催生出了很多绚丽的HTML5图片动画特效,特别是有些还有3D的动画效果.本 ...
- GridView、DataGrid、DataList、Repeater、ListView、DetailsView、FormView
列表类 GridView 控件 GridView 控件以表的形式显示数据,并提供对列进行排序.分页.翻阅数据以及编辑或删除单个记录的功能. 特征:一行一条记录,就像新闻列表一样:带分页功能 ...
- DropDownList另一种写法
2013-09-29 17:04:47 1.性别: <asp:DropDownList ID="DrpSex" runat ="server" Widt ...
- 【转】理解依赖注入(IOC)和学习Unity
IOC:英文全称:Inversion of Control,中文名称:控制反转,它还有个名字叫依赖注入(Dependency Injection).作用:将各层的对象以松耦合的方式组织在一起,解耦,各 ...
- Logstash+kibana+ ElasticSearch+redis
这是之前Logstash+kibana+ ElasticSearch+redis 安装时,自己整理的初学者容易看懂的资料,按照以下的步骤也已经完成了安装. 这里有二台服务器: 192.168.148. ...
- 延迟加载图片的 jQuery 插件:Lazy Load
网站的速度非常重要,现在有很多网站优化的工具,如 Google 的Page Speed,Yahoo 的 YSlow,对于网页图片,Yahoo 还提供 Smush.it这个工具对图片进行批量压缩,但是对 ...
- NSS_05 数据访问选型
在数据访问层上很想用orm框架, 选用Nhibernate或ef, 可以直接操作类对象, 避免转换, 更加的面向对象,更重要的是开发起来就方便多了. 但是从网上了解到这些框架太高级了, 用得不好到时会 ...
- PHP中strtotime函数使用方法分享
在PHP中有个叫做strtotime的函数.strtotime 实现功能:获取某个日期的时间戳,或获取某个时间的时间戳.strtotime 将任何英文文本的日期时间描述解析为Unix时间戳[将系统时间 ...
- json string 与object 之间的转化
1.将json string转化成object 1: public static T GetObjectFromJson<T>(string jsonString) 2: { 3: Dat ...
- 解决This system is not registered with RHN
Redhat之所以会出现这个错误是因为没有注册RHN,我们只需要更新一下yum的源就可以了.使用命令 cd /etc/yum.repos.d/ 进入yum的配置目录. 在终端中输入 wget ht ...