微软编程一小时 题目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)考虑到用户可能只会一种运算,因此可以选择运算.
随机推荐
- C语言调用汇编
程序的入口是main,在main里调用汇编的函数. 首先要解决怎么定义函数的问题 在C语言中,要extern 一个函数声明即可,然后这个函数在汇编里面实现. 在汇编里面,用EXPORT 把C语言定义的 ...
- linux条件变量
条件变量用于线程之间的通信,和互斥锁一起使用.条件变量用于及时通知等待的线程条件的变化,使线程不至于错过变化. 考虑下面的情况,有AB两个线程对index这个全局变量进行++,一个线程C用于判断,in ...
- 2、keys相关命令
redis的官网http://redis.io是学习redis的重要资源库,所有命令都分门别类的罗列在了这里http://redis.io/commands. 1.数据库选择命令: SELECT in ...
- SpringData JPA 接口和方法
1.1 简单查询--接口方法 1.2 五个接口详解 1.2.1 CrudRepository接口 其中T是要操作的实体类,ID是实体类主键的类型.该接口提供了11个常用操作方法. @NoRepo ...
- 前面部分(WCF全面解析1)
WCF全面解析 [同力推荐] 我经历了COM时代,一直把Don BOx的<COM本质论>奉为我的指路明灯.能把SOA机理和WCF这种特定厂商实现的技术讲得如<COM本质论>一样 ...
- k8s secret
https://kubernetes.io/docs/concepts/configuration/secret/ Secret是一个包含少量敏感数据的对象,例如密码,令牌或密钥. 否则,这些信息可能 ...
- Texture Format全解析
[Texture Format全解析] What internal representation is used for the texture. This is a tradeoff between ...
- GLSL in ShaderLab
[Syntax] However, use of raw GLSL is only recommended for testing, or when you know you will only ta ...
- Reactjs+BootStrap开发自制编程语言Monkey的编译器:创建简易的页面IDE
http://localhost:3000/
- 【祥哥带你玩HoloLens开发】了解如何实现远程主机为HoloLens实时渲染
今天有一个兄弟在群里讲到他们的项目模型比较大,单用HoloLens运行设备的性能无法满足需要,问道如何将渲染工作交给服务器来做,讲渲染结果传给HoloLens.正好刚刚看官方github的时候发现一个 ...