微软编程一小时 题目2: Longest Repeated Sequence
题目2 : Longest Repeated Sequence
描述
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 看到这个题,首先能够想到的思路有很多,KMP,后缀数组等等,然而限于时间限制,我暂时能够想到的方法就是暴搜了(-.-!请见谅): 首先有个关键问题需要注意一下, 本题是不可重叠最长子串的问题,不能直接套用后缀数组(参看《编程珠玑》p156),也不能直接套用kmp. (在后续博文中,我再给出其他的解法)。
由于是不可重叠最长子串的问题,如果原串的长度为L,那么子串长度Ls理论上能够达到的最大值为 floor(L/2). 本人思路如下:(如有问题,请各位指出,交流交流经验吧) 寻找数组中能够出现的首个最大长度重复子串的过程: Input L: 数组的长度
Input A: 待查找数组
Output maxlen: 最长重复子串的长度 for k <- L/2 to 2
for i <- 0 to L - k
for j <- i+ k to L
if comlen(A, i,j,L,k) // 判断长度为k的重复子串是否存在
maxLen <- k
return maxLen
return maxLen 源码如下:
/**********************************************
@author: lsc
@time: 2014-04-05
***********************************************/
#include <iostream>
using namespace std; /***********************************************
* comlen:
* i: 查找串T的起始位置
* j: 模式串P的起始位置
* size: 数组边界
* k: 子串长度
* 返回值:
* true: 存在长度为k的重复子串
* false: 不存在长度为k的重复子串
************************************************/
bool comlen(int a[], int i, int j,int size,int k)
{
int len = ;
while(i<size && j<size && a[i++] == a[j++])
{
++len;
if(len==k)
return true;
}
return false;
} /***********************************************
* LRS_base: 查找主逻辑
* maxlen: 记录最长重复子串长度
* arr: 带查找串
***********************************************/
int LRS_base(int arr[], int size)
{
int k,maxlen,i,j;
maxlen=; for(k=size/;k>=;k--)
{
for(i = ; i < size-k; ++i)
{
for( j = i+ k; j < size; ++j)
{
if(comlen(arr,i,j,size,k)==true)
{
maxlen = k;
return maxlen;
}
}
}
}
return maxlen;
} int main()
{
int a[]={},n;
cin>>n;
if(n<=||n>)
exit();
for(int i=;i<n;i++) cin>>a[i];
cout<<LRS_base(a,n)<<endl; return ;
}
微软编程一小时 题目2: Longest Repeated Sequence的更多相关文章
- Longest Repeated Sequence【微软编程一小时-题目2】
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 You are given a sequence of integers, A = a1, a2, ... an. A c ...
- 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 ...
- longest incresing sequence
动态规划基本题目,longest incresing sequence,找出序列中的最长递增子序列: 例如给出序列{8,3,5,2,4,9,7,11}, 其中最长递增子序列为{3,5,9,11}或{3 ...
- 编程一小时 code.org [六一关注]
编程一小时活动的组织者是Code.org, 它是一个面向公众的公益组织,致力于在更多的学校推广计算机科学教育,并为女性和就业率低的有色人种学生学习计算机的机会.同时,一个空前强大的合作伙伴联盟也在支持 ...
- LeetCode 1156. Swap For Longest Repeated Character Substring
原题链接在这里:https://leetcode.com/problems/swap-for-longest-repeated-character-substring/ 题目: Given a str ...
- 【leetcode】1156. Swap For Longest Repeated Character Substring
题目如下: Given a string text, we are allowed to swap two of the characters in the string. Find the leng ...
- Programming pearls 编程珠玑的题目
Programming pearls 编程珠玑的题目 这段时间有空都在看编程珠玑,很经典的一本书,一边看一边用 python 做上面的题目,我做的都放到 github 上了 https://githu ...
- C++编程显示四则运算题目
题目:C++编程显示四则运算题目 设计思路:(1)让用户自己确定出题的数量,同时显示加减乘除四则运算. (2)考虑到用户可能只会一种运算,因此可以选择运算.
随机推荐
- Python实践练习:电话号码和 E-mail 地址提取程序
题目: 假设你有一个无聊的任务,要在一篇长的网页或文章中,找出所有电话号码和邮件地址.如果手动翻页,可能需要查找很长时间.如果有一个程序,可以在剪贴板的文本中查找电话号码和 E-mail 地址,那你就 ...
- TestNG Hello World入门示例
https://www.yiibai.com/testng/hello-world-example.html https://www.yiibai.com/testng/ 作为一个经典的入门例子,这里 ...
- Spring Boot实践——SpringMVC视图解析
一.注解说明 在spring-boot+spring mvc 的项目中,有些时候我们需要自己配置一些项目的设置,就会涉及到这三个,那么,他们之间有什么关系呢? 首先,@EnableWebMvc=Web ...
- MySQL两个日期字段相减得到秒的方法
一.MySQL中两个DateTime字段相减 假定表名为tblName,两个DateTime字段名分别为beginDateTime,endDateTime,以下是相关两个mysql日期字段相减的SQL ...
- openSUSE Linux 忘记root密码的解决方法
openSUSE Linux 忘记root密码的解决方法 : 对于大部分linux发行版本,忘记root密码的时候,是可以通过单用户模式来重设密码的. 如在redhat/fedora 下,可以通过在启 ...
- a.call(b); call 方法
a.call(b); a.apply(b,[]) function class1() { this.name = function(){ alert("class1的方法name()&quo ...
- 关于StringUtils类isEmpty、isNotEmpty、isBlank、isNotBlank针对null、空字符串和空白字符(如空格、制表符)的区别
isEmpty | null | 空字符串("")|空白字符(空格.制表符)| | isEmpty | true | true | false | | isNotEmpty | f ...
- vue的样式绑定
vue在样式绑定,看这官方的文档,怎么试都不行后来看了一篇文章 <div :class="[rankClass]"></div> <script> ...
- Kafka总结的一张图
- leetcode array解题思路
Array *532. K-diff Pairs in an Array 方案一:暴力搜索, N平方的时间复杂度,空间复杂度N 数组长度为10000,使用O(N平方)的解法担心TLE,不建议使用,尽管 ...