微软编程一小时 题目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)考虑到用户可能只会一种运算,因此可以选择运算.
随机推荐
- Android 4 学习(20):ActionBar
参考<Pro Android 4.0> ActionBar 11.0之后,ActionBar在Activity中默认存在,可以在代码中设置其显示与否: ActionBar actionBa ...
- jQuery基本API小结(下)---工具函数-基本插件
一.工具函数 1.获取浏览器的名称与版本信息 在jQuery中,通过$.browser对象可以获取浏览器的名称和版本信息,如$.browser.chrome为true,表示当前为Chrome浏览器,$ ...
- webpy简单使用
#!/usr/bin/env python import web import pymysql.cursors # Connect to the database connection = pymys ...
- 解读linux中用户密码规则及忘记root口令的破解(思路)
linux当中,用户名和密码表对应关系放在/etc/passwd中,如: root:x:0:0:root:/root:/bin/bash 格式代表意义分别为 用户名:密码:用户id:组id:用户描述 ...
- XX公司的CA,与平台融合解决方法。。。。。
1,jsp 的编写要求 <script language="javascript" type="text/javascript"> var clie ...
- debian:jessie 安转percona mysql
dpkg -r percona-server-server-5.6 dpkg -r percona-server-client-5.6 dpkg -r libperconaserverclient18 ...
- string.Empty与null与""
(1)NULLnull 关键字是表示不引用任何对象的空引用的文字值.null 是引用类型变量的默认值.那么也只有引用型的变量可以为NULL,如果int i=null,的话,是不可以的,因为Int是值类 ...
- PHP微信授权登录信息
文件1:index.php //换成自己的接口信息 $appid = 'XXXXX'; header('location:https://open.weixin.qq.com/connect/oaut ...
- Zookeeper使用--开源客户端
一.ZkClient ZkClient是在Zookeeper原生API接口之上进行了包装,是一个更易用的Zookeeper客户端,其内部还实现了诸如Session超时重连.Watcher反复注册等功能 ...
- windows命令行下批量拷贝同一后缀的文件到另外一个目录
一个目录下有很多文件夹,想拷贝每个文件夹下面的wmv文件到另外一个目录,如果鼠标打开一个文件,拷贝一个,再打开其他的,逐一操作,很麻烦的,百度了一下,xcopy命令就可以实现:例如将C盘x1目录下所有 ...