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 ...
随机推荐
- nyoj_t218(Dinner)
描述 Little A is one member of ACM team. He had just won the gold in World Final. To celebrate, he dec ...
- 超越luabind的luaBridge
此编是引用他人的文章,这里记录下,主要为以后自己查找方便,原文地址:http://www.cppblog.com/sunicdavy/archive/2013/12/07/204648.html 最近 ...
- C++ 实现不能被继承的类
方法一: #include <iostream> using namespace std; class A { public: static A* getInstance(); stati ...
- 【风马一族_Android】多选按钮的监控事件
import android.app.Activity; import android.os.Bundle; import android.text.TextUtils; import android ...
- 《mysql数据库备份小脚本》
vim mysql.sh #!/bin/bashDAY=`date +%Y-%m-%d` //日期以年月日显示并赋予DAY变量SIZE=`du -sh /var/lib/mysql //查看mysql ...
- 转:JAVA中this用法小结
转:http://blog.sina.com.cn/s/blog_6a6badc90100t8hm.html#SinaEditor_Temp_FontName Java关键字this只能用于方法方法体 ...
- Markdown 使用说明
使用说明 ========= @[手册|帮助|Markdown] - **马克飞象**是一款专为印象笔记打造的Markdown编辑器. - 特别需要说明的一点是增加了`@(笔记本)[标签]`语法,以此 ...
- GoldenGate单向复制配置示例
一:环境介绍 --source端 ip地址:192.168.123.10 数据库版本:11.2.0.1.0 32 bit 操作系统版本:centos 4.5 32 bit ogg版本:fbo_ggs_ ...
- WPF之UseLayoutRounding和SnapsToDevicePixels
最近在工作中看别的朋友XML代码时,发现SnapsToDevicePixels 属性然后通过查询资料了解其作用 1)UserLayoutRounding为False,导致控件布局相对屏幕若不是整数则不 ...
- ERROR 23 (HY000) at line 29963: Out of resources when opening file
在还原数据库时报错,报错信息如下:(库中的表比较多) ERROR 23 (HY000) at line 29963: Out of resources when opening file 解决方法: ...