看动态规划的题 在topcoder里看的这道题就来练练手
Problem Statement
A sequence of numbers is called a zig-zag sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with fewer than two elements is trivially a zig-zag sequence.
For example, 1,7,4,9,2,5 is a zig-zag sequence because the differences (6,-3,5,-7,3) are alternately positive and negative. In contrast, 1,4,7,2,5 and 1,7,4,5,5 are not zig-zag sequences, the first because its first two differences are positive and the second because its last difference is zero.
Given a sequence of integers, sequence, return the length of the longest subsequence of sequence that is a zig-zag sequence. A subsequence is obtained by deleting some number of elements (possibly zero) from the original sequence, leaving the remaining elements in their original order.
Definition
Class: ZigZag
Method: longestZigZag
Parameters: int[]
Returns: int
Method signature: int longestZigZag(int[] sequence)
(be sure your method is public)
Constraints
- sequence contains between 1 and 50 elements, inclusive.
- Each element of sequence is between 1 and 1000, inclusive.
Examples
0)
{ 1, 7, 4, 9, 2, 5 }
Returns: 6
The entire sequence is a zig-zag sequence.
1)
{ 1, 17, 5, 10, 13, 15, 10, 5, 16, 8 }
Returns: 7
There are several subsequences that achieve this length. One is 1,17,10,13,10,16,8.
2)
{ 44 }
Returns: 1
3)
{ 1, 2, 3, 4, 5, 6, 7, 8, 9 }
Returns: 2
4)
{ 70, 55, 13, 2, 99, 2, 80, 80, 80, 80, 100, 19, 7, 5, 5, 5, 1000, 32, 32 }
Returns: 8
5)
{ 374, 40, 854, 203, 203, 156, 362, 279, 812, 955,
600, 947, 978, 46, 100, 953, 670, 862, 568, 188,
67, 669, 810, 704, 52, 861, 49, 640, 370, 908,
477, 245, 413, 109, 659, 401, 483, 308, 609, 120,
249, 22, 176, 279, 23, 22, 617, 462, 459, 244 }
Returns: 36
题目大意是:一个数字序列如果严格遵守正负之间持续交替的差异称为Zig-Zag序列。第一个差异(如果存在)可以是正的或负的。
例如,1,7,4,9,2,5是Zig-Zag序列因为差异(6,-3,5,-7,3)交替的正面和负面的。相反,1,4,7,2,5和1,7,4,5,5不是Zig-Zag序列,
给定一个整数序列,返回序列的最长Zig-Zag序列。
思路:和求解最长非降子序列的思想一样,a[i]为当前数字,a[j]为前面的数字,down[]为该数下一个数比他小的序列的长度,如:1,17 17的down[1]=2,up[1]=1 up[]为该数下一个数比他大的序列的长度,如:2,1 1的up[1]=2,down[1]=1
若a[j]<a[i]:则a[i]下一个数应该比他小,down[i]=up[j]+1;
若a[j]>a[i]:则a[i]下一个数应该比他大,up[i]=down[j]+1;
len表示当前序列长度最大的.
public class TopCoder__ZigZag {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
String a=s.nextLine();//注意这里是读行 只能读第一行 最后那个试例 将它们弄到同一行
String b[]=a.split(" ");//两个数中间用空格隔开,若想直接复制题里的 将这里的空格改成“,”即可
int arr[]=new int[b.length];
for(int i=0;i<b.length;i++){
arr[i]=Integer.parseInt(b[i]);
}
System.out.println(len(arr));
}
public static int len(int a[]){
if(a.length==1)return 1;
int up[]=new int[a.length];
int down[]=new int[a.length];
int len=1;
up[0]=1;
down[0]=1;
for(int i=1;i<a.length;i++){
for(int j=0;j<i;j++){
if(a[i]<a[j]){
// if(up[i]<down[j]+1)
up[i]=down[j]+1;//其实这里不用判断 因为它和前面的是一个序列 所以长度+1肯定大
down[i]=Math.max(down[i],down[j]);
}
else if(a[i]>a[j]){
// if(down[i]<up[j]+1)
down[i]=up[j]+1;
up[i]=Math.max(up[i],up[j]);
}
else{
down[i]=down[j];
up[i]=up[j];
}
}
len=Math.max(len, Math.max(down[i],up[i]));
}
return len;
}
}