Skip to content

Commit cc6972b

Browse files
committed
tccc 03 semi 3 div 1 250
1 parent 33719e6 commit cc6972b

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

Diff for: ZigZag.cpp

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <iostream>
2+
#include <sstream>
3+
#include <vector>
4+
#include <algorithm>
5+
#include <set>
6+
#include <map>
7+
using namespace std;
8+
int a[55];
9+
int b[55];
10+
11+
class ZigZag {
12+
public:
13+
int longestZigZag(vector <int> t)
14+
{
15+
int n = t.size();
16+
a[n-1] = b[n-1] = 1;
17+
for(int i = n-2; i >= 0; i--){
18+
int maxa = 0;
19+
for(int j = i+1; j < n; j++)
20+
if(t[j] > t[i]) maxa = max(maxa, b[j]);
21+
a[i] = maxa+1;
22+
23+
int maxb = 0;
24+
for(int j = i+1; j < n; j++)
25+
if(t[j] < t[i]) maxb = max(maxb, a[j]);
26+
b[i] = maxb+1;
27+
}
28+
return max(a[0], b[0]);
29+
}
30+
31+
// BEGIN CUT HERE
32+
public:
33+
void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); if ((Case == -1) || (Case == 5)) test_case_5(); }
34+
private:
35+
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
36+
void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
37+
void test_case_0() { int Arr0[] = { 1, 7, 4, 9, 2, 5 }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 6; verify_case(0, Arg1, longestZigZag(Arg0)); }
38+
void test_case_1() { int Arr0[] = { 1, 17, 5, 10, 13, 15, 10, 5, 16, 8 }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 7; verify_case(1, Arg1, longestZigZag(Arg0)); }
39+
void test_case_2() { int Arr0[] = { 44 }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 1; verify_case(2, Arg1, longestZigZag(Arg0)); }
40+
void test_case_3() { int Arr0[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 2; verify_case(3, Arg1, longestZigZag(Arg0)); }
41+
void test_case_4() { int Arr0[] = { 70, 55, 13, 2, 99, 2, 80, 80, 80, 80, 100, 19, 7, 5, 5, 5, 1000, 32, 32 }; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 8; verify_case(4, Arg1, longestZigZag(Arg0)); }
42+
void test_case_5() { int Arr0[] = { 374, 40, 854, 203, 203, 156, 362, 279, 812, 955,
43+
600, 947, 978, 46, 100, 953, 670, 862, 568, 188,
44+
67, 669, 810, 704, 52, 861, 49, 640, 370, 908,
45+
477, 245, 413, 109, 659, 401, 483, 308, 609, 120,
46+
249, 22, 176, 279, 23, 22, 617, 462, 459, 244 }
47+
; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 36; verify_case(5, Arg1, longestZigZag(Arg0)); }
48+
49+
// END CUT HERE
50+
51+
};
52+
53+
// BEGIN CUT HERE
54+
int main()
55+
{
56+
ZigZag ___test;
57+
___test.run_test(-1);
58+
}
59+
// END CUT HERE

Diff for: ZigZag.txt

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
PROBLEM STATEMENT
2+
3+
A sequence of numbers is called a zig-zag sequence if the differences
4+
between successive numbers strictly alternate between positive and negative. The
5+
first difference (if one exists) may be either positive or negative. A sequence with
6+
fewer than two elements is trivially a zig-zag sequence.
7+
8+
9+
10+
For example, 1,7,4,9,2,5 is a zig-zag sequence because the differences
11+
(6,-3,5,-7,3) are alternately positive and negative. In contrast, 1,4,7,2,5
12+
and 1,7,4,5,5 are not zig-zag sequences, the first because its first two differences are positive and
13+
the second because its last difference is zero.
14+
15+
16+
17+
Given a sequence of integers, sequence, return the length of the longest subsequence of sequence that is
18+
a zig-zag sequence. A subsequence is obtained by deleting some number of
19+
elements (possibly zero) from the original sequence, leaving the remaining elements in their original order.
20+
21+
22+
23+
DEFINITION
24+
Class:ZigZag
25+
Method:longestZigZag
26+
Parameters:vector <int>
27+
Returns:int
28+
Method signature:int longestZigZag(vector <int> sequence)
29+
30+
31+
CONSTRAINTS
32+
-sequence contains between 1 and 50 elements, inclusive.
33+
-Each element of sequence is between 1 and 1000, inclusive.
34+
35+
36+
EXAMPLES
37+
38+
0)
39+
{ 1, 7, 4, 9, 2, 5 }
40+
41+
Returns: 6
42+
43+
The entire sequence is a zig-zag sequence.
44+
45+
1)
46+
{ 1, 17, 5, 10, 13, 15, 10, 5, 16, 8 }
47+
48+
Returns: 7
49+
50+
There are several subsequences that achieve this length. One is 1,17,10,13,10,16,8.
51+
52+
2)
53+
{ 44 }
54+
55+
Returns: 1
56+
57+
3)
58+
{ 1, 2, 3, 4, 5, 6, 7, 8, 9 }
59+
60+
Returns: 2
61+
62+
4)
63+
{ 70, 55, 13, 2, 99, 2, 80, 80, 80, 80, 100, 19, 7, 5, 5, 5, 1000, 32, 32 }
64+
65+
Returns: 8
66+
67+
5)
68+
{ 374, 40, 854, 203, 203, 156, 362, 279, 812, 955,
69+
600, 947, 978, 46, 100, 953, 670, 862, 568, 188,
70+
67, 669, 810, 704, 52, 861, 49, 640, 370, 908,
71+
477, 245, 413, 109, 659, 401, 483, 308, 609, 120,
72+
249, 22, 176, 279, 23, 22, 617, 462, 459, 244 }
73+
74+
75+
Returns: 36

0 commit comments

Comments
 (0)