BUPT OJ183 Longest Subsequence

44 篇文章 3 订阅

题目描述

 Recently,Nero is researching a kind of sequence problem related to the sphere.This kind of problem ressembles the Longest Ascending Subsequence.
For instance,Given a sequence,can you find the longest subsequence in it?
Following constraints should be meeted:
1:the elements in the subsequence should be totally equal.
2:the interval between the adjacent elements in the subsequence shouldn't be greater than one(in the hint,we'll show clearly what the interval is)

输入格式

 T indicating the number of test cases
 Each case begins with a interger  n(0<=n<=1000) ,representing the length of sequence
 Following are n integers,Pi stands for the value of each element.

输出格式

 One line,contains a integer indicating the length of the longest subsequence.

输入样例

  2
  6
  1 2 2 1 1 1
  8
  1 2 2 2 1 1 1 1

输出样例

3
4

题干提到的LAS真够坑的... 明明没有关系的, sample给的偏偏就是模糊不清, 理解为LAS也可以得出相同结果

其实简单的很, 没理解题目意思, 结果自然是WA了以后立刻就发现了, 理解了就好简单...

事实上题目意思是要找一个最长的相同元素构成的序列, 使得相邻两项的位置差不超过1...也就是最多可以跳一位取下一项

思路是简单的dp, 毫无技术含量......能不跳就不跳, 其实说是贪心也对? 撒~~



/*
USER_ID: test#birdstorm
PROBLEM: 183
SUBMISSION_TIME: 2014-03-08 01:07:10
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define MAX(x,y) (x)>(y)?(x):(y)
#define For(i,m,n) for(i=m;i<n;i++)
#define MAXN 1005
 
int a[MAXN], dp[MAXN];
 
main()
{
    int i, j, n, t, len, max;
    scanf("%d",&t);
    while(t--){
        len=1; max=0;
        dp[0]=1;
        scanf("%d%d",&n,&a[0]);
        For(i,1,n){
            scanf("%d",&a[i]);
            if(a[i]==a[i-1]) dp[i]=dp[i-1]+1;
            else if(a[i]==a[i-2]) dp[i]=dp[i-2]+1;
            else dp[i]=1;
        }
        For(i,0,n) max=MAX(max,dp[i]);
        printf("%d\n",max);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值