SZU:A12 Jumping up and down

本文介绍了一种算法问题,即在给定的整数序列中寻找最长的子序列,使得相邻元素不相等,用于模拟青蛙跳跃过程中的完美路径。提供了两种实现方式,一种使用数组存储数据,另一种则无需额外数组。

Judge Info

  • Memory Limit: 32768KB
  • Case Time Limit: 10000MS
  • Time Limit: 10000MS
  • Judger: Number Only Judger

Description

In the kingdom of frog, the most popular sport is jumping up and down. Frog likes to jump up(go higher) or jump down(go lower) but jump at the same height(not go higher,and not go lower). The frog king wants to have a sport path for jumping. The Sport path is made by same size blocks which may have different height,when the frog on the path, he can only jumps from one block to the next adjacent block. For frog, perfect path for jumping are those same-height-jumps can be avoided. The Sport path can be represented by an integer sequence which denotes the height of blocks.The length of the sport path is the numbers of integer in the sequence.

Task

Now, it is your turn. You will have a integer sequence represent a sport path, please output the longest perfect path we can find in the given sport path without cutting off.

Input

The first line of input contains T(1 \leq T \leq 100), the number of test cases. There are two lines for each test case. The first line contains an integer number N(1 \leq N \leq 100)denoting how many blocks in the sport path. The second line contains N integer numbers (the height of blocks in the range [ − 100,100]).

Output

For each test case, print a line contains the solution.

Sample Input

2
8
1 1 3 5 5 7 9 8
5
1 2 3 4 5

Sample Output

4
5


解题思路:刚开始读不懂题意,以为是消除相同数和其本身,经过朋友说下才明白,原来是找最长的子串,但是遇到重复数字就开始新的字符串序列。
刚开始用了两个数组,之后用一个数组,而ACMser都不用开数组。

 1 #include <stdio.h>
 2 #include <string.h>
 3 int A[103];
 4  
 5 int main() {
 6     int t,i,n,pre,max,count,min;
 7     scanf("%d",&t);
 8     while (t--) {
 9         max = 1;
10         scanf("%d",&n);
11         for (i=0;i<n;i++) {
12             scanf("%d", &A[i]);
13         }
14         pre = 0;
15         count =1;
16         for (i=1;i<n;++i) {
17  
18             if(A[i]==A[pre]){
19                 if(count>max)
20                     max = count;
21                 count =1;
22                 continue;
23             }
24             count++;
25             pre=i;
26         }
27         min = count;
28         if (min > max)
29             max = min;
30         printf("%d\n",max);
31     }
32 }

 

朋友的做法(不开数组):

 1 #include <stdio.h>
 2 int process(int n);
 3 int main()
 4 {
 5     int testcase;
 6     scanf("%d", &testcase);
 7     while (testcase--) {
 8         int n;
 9         scanf("%d", &n);
10         printf("%d\n", process(n));
11     }
12 }
13 int process(int n)
14 {
15     int i, pre, cur;;
16     scanf("%d", &pre);
17     int cnt = 1, cur_max = 1;
18     for (i = 1; i != n; i++) {
19         scanf("%d", &cur);
20         if (cur == pre) {
21             if (cur_max < cnt)
22                 cur_max = cnt;
23             cnt = 1;
24         }
25         else {
26             cnt++;
27         }
28         pre = cur;
29     }
30     if (cur_max < cnt)
31         cur_max = cnt;
32     return cur_max;
33 }

 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值