ACM notes Why Not always an easy question









![Graphs [A, E], [A, B], [A, C], [B, D], [B, E], [C, B], [D, Graphs [A, E], [A, B], [A, C], [B, D], [B, E], [C, B], [D,](https://slidetodoc.com/presentation_image/10a85ba6e3ee3831aeee689bb00bc848/image-10.jpg)
![Graphs [A, E], [A, B], [A, C], [B, D], [B, E], [C, B], [D, Graphs [A, E], [A, B], [A, C], [B, D], [B, E], [C, B], [D,](https://slidetodoc.com/presentation_image/10a85ba6e3ee3831aeee689bb00bc848/image-11.jpg)




![Geometric Problems [A, E], [A, B], [A, C], [B, D], [B, E], [C, B], Geometric Problems [A, E], [A, B], [A, C], [B, D], [B, E], [C, B],](https://slidetodoc.com/presentation_image/10a85ba6e3ee3831aeee689bb00bc848/image-16.jpg)























- Slides: 39
ACM notes • Why… ? Not always an easy question! Entropy problem -- Huffman Codes bits used in ASCII Input Output A. . AB. . BC. . CD. . DE. . EF 40 20 16 12 9 784 224 3. 5 1 bits used in an optimal “prefix-free” encoding 98 chars compression ratio, to 1 place of precision
Prefix-free Codes via binary trees A. . AB. . BC. . CD. . DE. . EF 40 20 16 12 1 9 98 ABCDEF 49 40 A AE 9 49 21 E 20 49*2 + 49*3 = 245 B BF 1 BCDF 28 F CD C 16 12 D
Prefix-free Codes via binary trees A. . AB. . BC. . CD. . DE. . EF 40 20 16 1 9 12 98 ABCDEF 0 49 AE A 00 49 1 0 40 1 9 1 0 E 21 01 0 20 B 100 49*2 + 49*3 = 245 BCDF BF 0 1 1 CD 28 F 101 C 16 110 1 12 D 111 codewords are read down the paths
Huffman Codes Building the tree from the bottom up… A. . AB. . BC. . CD. . DE. . EF 40 20 16 12 9 1 10 20 16 12 10 1 0 A. . AB. . BC. . CD. . DE. . F 40 EF 9 E 1 F
Huffman Codes Building the tree from the bottom up… A. . AB. . BC. . CD. . DE. . F 40 20 16 12 10 DEF 22 0 D 10 12 A. . AD. . E. . FB. . BC. . C 40 22 20 16 EF 1 0 9 E 1 F
Huffman Codes Building the tree from the bottom up… A. . AD. . E. . FB. . BC. . C 40 22 20 16 36 BC A. . AB. . CD. . E. . F 40 36 22 20 B 16 1 0 DEF 22 C D 10 12 EF 1 0 9 E 1 F
Huffman Codes Building the tree from the bottom up… A. . AB. . CD. . E. . F 40 36 22 40 A 58 BCDEF 1 0 36 BC 20 B 16 1 0 DEF 22 C D 10 12 58 1 0 A. . AB. . C. . D. . E. . F 40 EF 9 E 1 F
Huffman Codes Building the tree from the bottom up… A. . AB. . C. . D. . E. . F 40 58 98 ABCDEF 1 0 40 A 58 BCDEF 1 0 36 BC 20 B 16 1 0 DEF 22 C D 10 12 1 0 A. . B. . C. . D. . E. . F 58 EF 9 E 1 F
Huffman Codes Total number of bits needed. . . 40*1 + 36*3 + 12*3 + 10*4 = 224 98 ABCDEF 1 0 40 A 58 BCDEF 1 0 36 BC 20 B 16 1 0 DEF 22 C D 10 12 EF 1 0 9 E 1 F
Graphs [A, E], [A, B], [A, C], [B, D], [B, E], [C, B], [D, A], [D, C], [E, D] B Adjacency List Representation A C D E (vector or array of pairs)
Graphs [A, E], [A, B], [A, C], [B, D], [B, E], [C, B], [D, A], [D, C], [E, D] B 8 Adjacency List Representation 9 A 13 C (vector or array of pairs ? ) 6 1 0 7 12 TO D E 11 FROM Matrix representation (2 d array) A B C D E 0 - 8 0 9 13 0 6 - 1 12 - 7 - - 0 11 0
Problems Roman Forts (forts. cc) J C A D I G F B H K E fortify the most vulnerable. . .
Problems Single Points of Failure (spf. cc) 1 5 3 3 0 2 4 1 2 4 5 Network #1 SPF node 3 leaves 2 subnets Output 1 Input 1 Graph 2 Output 2 Network #2 No SPF nodes
All-pairs shortest paths. . . “Floyd-Warshall algorithm” A B C D E 0 - 8 0 9 13 0 6 - 1 12 - 7 - - 0 11 0 k dij = D 0 = (dij 0 ) A B C D E 0 - 8 0 9 13 0 6 - 1 12 - 7 - - 0 11 0 D 1 = (dij 1 ) k A B C D E 0 7 - 8 13 - 1 0 - 6 12 9 0 15 0 0 8 - 11 0 dij = shortest distance from i to j through {1, …, k}
All-pairs shortest paths. . . “Floyd-Warshall algorithm” A B C D E 0 - 8 0 9 13 0 6 - 1 12 - 7 - - 0 11 0 k dij = D 0 = (dij 0 ) A B C D E 0 - 8 0 9 13 0 6 - 1 12 - 7 - - 0 11 0 D 1 = (dij 1 ) k A B C D E 0 7 - 8 13 - 1 0 - 6 12 9 0 15 0 0 8 - 11 0 dij = shortest distance from i to j through {1, …, k}
Geometric Problems [A, E], [A, B], [A, C], [B, D], [B, E], [C, B], [D, A], [D, C], [E, D] B Adjacency List Representation A C D E (vector or array of pairs) code resources to keep in mind
All-pairs shortest paths. . . A B C D E 0 7 - A B C D E 8 13 14 1 0 - 6 12 9 0 15 21 15 0 0 8 - 11 0 D 2 = A B C D E 0 7 - 8 0 9 9 - 0 13 22 7 18 8 13 14 1 0 6 6 12 9 0 15 21 9 0 0 8 20 11 11 0 D 4 = (dij 4 ) 2 (dij ) A B C D E 13 14 1 - 6 12 0 15 21 0 0 8 - 11 0 D 3 = (dij 3 ) 0 13 22 7 18 8 12 12 1 0 6 6 12 9 0 15 21 9 0 0 8 20 11 11 0 D 5 = (dij 5 ) to store the path, another matrix can track the last intermediate vertex
Graphs Representations bits used in ASCII Input Output AAAAABCD 64 13 4. 9 bits used in an optimal “prefix-free” encoding compression ratio, to 1 place of precision
Graphs Representations bits used in ASCII Input Output AAAAABCD 64 13 4. 9 bits used in an optimal “prefix-free” encoding compression ratio, to 1 place of precision
Problems Entropy bits used in ASCII Input Output AAAAABCD 64 13 4. 9 bits used in an optimal “prefix-free” encoding A B C D 0 10 111 compression ratio, to 1 place of precision
Problems N-Credible Mazes dimensions 2 Input start Output end Maze #1 can be travelled 0 0 2 2 edge start (or not…) 0 0 0 1 0 2 1 2 0 3 1 2 2 2 -1 edge end
www. dinkumware. com/htm_cpl/index. html set #include <set> C++ STL www. sgi. com/tech/stl/ set<int> s; // basically a bin. tree s. size(); // returns an int s. insert(14); // adds 14 s. insert(-9); // adds -9 s. insert(42); // adds 42 set<int>: : iterator i; // may want to typedef // think of an iterator as a pointer i = s. find(42); multiset #include <set> // return 42’s iterator cout << (*i) << endl; // prints 42 cout << (*--i)); // prints. . . i = s. find(43); // not there ! // at this point ( i == s. end() ) is true s. erase(-9); // removing elements multiset<int> m; // holds multiple copies
Breadth-first search • algorithm data structures queue, deque, hashtable (map)
www. dinkumware. com/htm_cpl/index. html vector #include <vector> sort #include <algorithm> C++ STL www. sgi. com/tech/stl/ vector<int> v; // basically an int array v. reserve(10); // assure 10 spots v. push_back(42); // adds 42 to the end v. back(); // returns 42 v. pop_back(); // removes 42 v. size(); // # of elements v[i]; // ith element sort( v. begin(), v. end() ); // default sort( v. begin(), v. end(), mycompare ); last time deque #include <deque> deque<int> d; // double-ended queue d. push_front(42); // add to front d. front(42); // return front element d. pop_front(42); // remove from front
Other problems • Change counting input: 1. 00 output: 0. 06 There are 292 ways to make $1. 00 There are 2 ways to make $0. 06 0 • Sigma series input: 3 output: 1 2 3 Shortest sequences from 1 to N such that each element is the sum of two previous elements. 4 1 2 4 87 1 2 4 8 16 24 28 29 58 87 99 1 2 4 8 16 32 33 66 99 -1
Useful C functions int atoi(char* s); converts C strings to ints atoi(“ 100”) == 100 double atof(char* s); converts C strings to doubles atoi(“ 100. 0”) == 100. 0 int strcasecomp(char* s 1, char* s 2); case-insensitive C string comparison strcasecmp(“a. Cm”, “ACm”) == 0 long strtol(char* s, NULL, int base) arbitrary conversion from a string in bases (2 -36) to a long int strtol(“Charlie”, NULL, 36) == 2147483647 L use man for more. . .
sprintf int sprintf(char* str, char* format, . . . ); prints anything to the string str char str[100]; sprintf(str, “%d”, 42); // str is “ 42” sprintf(str, “%f”, 42. 0); // str is “ 42. 0” flexible formatting: sprintf(str, “%10 d”, 42); // str is “ 42” right/left justify: sprintf(str, “%-10 d”, 42); // str is “ 42 ”
Two ACM programming skills A chance to “improve” your C/C++ … Preparation for the ACM competition. . . Problem Insight and Execution. . . 1 Get into the minds of the judges 2 Anxiety!
Key Skill #1: mindreading Get into the minds of the judges 100% 0% “What cases should I handle? ” spectrum
Key Skill #2: Anxiety! anxiety
Dynamic Programming Strategy: create a table of partial results & build on it. divis. cc T(n) = number of steps yet to go T(n) = T(3 n+1) + 1 if n odd T(n) = T(n/2) + 1 if n even
Dynamic Programming Keys: create a table of partial results, articulate what each table cell means, then build it up. . . i = possible remainder Table T 0 divis. cc j = items considered so far 0 1 2 3 4 5 6 1 1 6 2 -3 the list 1 2 4 the divisor 3 T[i][j] is 1 if i is a possible remainder using the first j items in the list.
Dynamic programs can be short #include <cstdio> #include <iostream> #include <vector> vector<int> v(10000); vector<bool> m(100); // old mods vector<bool> m 2(100); // new mods int n, k; int main() { cin >> n; while (cin >> n) { cin >> k; bool divisible() { fill(m. begin(), m. end(), false); m[0] = true; for (int i=0; i<n; i++) { cin >> v[i]; v[i] = abs(v[i]); v[i] %= k; } for (int i=0; i<n; i++) { cout << (divisible() ? "D" : "Not d") << "ivisiblen"; /* not giving away all of the code */ /* here the table is built (6 lines) */ } return m[0]; } // garbage } cout << endl; } acknowledgment: Matt Brubeck STL: http: //www. sgi. com/Technology/STL
General ACM Programming Try brute force first (or at least consider it) -- sometimes it will work fine… -- sometimes it will take a _bit_ too long -- sometimes it will take _way_ too long Best bugs from last week: getting the input in the “pea” problem: for (int j=1 ; j<N ; ++j) { cin >> Array[i]; } filling in the table in the “divis” problem: Table[i + n % k] = 1; Table[i - n % k] = 1;
New Problem Word Chains Input A list of words hertz doze jazz aplomb hajj ceded zeroth dozen envy ballistic yearn Output yes or no -- can these words be chained together such that the last letter of one is the first letter of the next… ?
Knapsack Problem Maximize loot w/ weight limit of 4. Number of objects considered 1 2 3 n 4 V(n, w) = 1 2 wt. val. 3 8 2 5 1 1 2 5 w Weight available for use 0 object 1 2 3 4 V(n, w) = max value stealable w/ ‘n’ objects & ‘w’ weight
C Output printf, fprintf, sprintf(char* s, const char* format, …) the destination the format string the values possible format strings % -#0 12 h d . 4 minimum field width type size modifier precision flags start character left-justify 0 pad w/ zeros + use sign (+ or -) (space) use sign ( or -) # deviant operation d decimal integers u unsigned (decimal) ints o octal integers x hexadecimal integers f doubles (floats are cast) e doubles (exp. notation) g f or e, if exp < -3 or -4 c character s string n outputs # of chars written !! % two of these print a ‘%’ allowed size modifiers h short l long (lowercase L) L long double
C Output value = 42 %10. 4 d %-#12 x value = -42 0042 -0042 0 x 2 a 0 xffffffd 6 value = 42 %+10. 4 g value = -42. 419 +42 -42. 42 %- 10. 4 g 42 -42. 42 %-#10. 4 g 42. 00 -42. 42 value = “forty-two” %10. 5 s forty
Change const int p=1, n=5, d=10, q=25, h=50; int counter=0; int pn, nn, dn, qn, hn; for (hn = 0; hn*h <= num; hn++) for (qn = 0; hn*h + qn*q <= num; qn++) for (dn = 0; hn*h + qn*q + dn*d <= num; dn++) for (nn = 0; hn*h + qn*q + dn*d + nn*n <= num; nn++) for (pn = 0; hn*h + qn*q + dn*d + nn*n + pn*p <= num; pn++) { if (hn*h + qn*q + dn*d + nn*n + pn*p == num) counter++; } Brute Force Dynamic Programming total 0¢ 1¢ 2¢ 3¢ 4¢ 5¢ 6¢ 7¢ 8¢ 9¢ 10¢ 1 1 1 1¢, 5¢ 1 1 1 2 2 2 3 3 3 1¢, 5¢, 10¢ 1 1 1 2 2 2 4 4 4 using 1¢ 12¢







































