ACM notes Can work on the problems anytime

ACM notes • Can work on the problems anytime throughout the term • Contest conflict -- The GRE subject test is Nov. 10 th! Digroot “problem” int x; while (cin >> x) { cout << ( (x%9) ? (x%9) : 9 ); }

ACM “style” while (nlines-- > 0) { queue<char> q; string s; getline(cin, s); for (int i = 0; s[i]; i++) { if (!q. empty() && s[i] == q. front()) { // found q. pop(); } else { q. push(s[i]); if (q. size() > 10) { cout << "Not consistent with theoryn"; goto done; } } } if (q. empty()) cout << "An echo string with buffer size tenn"; else cout << "Not an echo string, but still consistent with theoryn"; done: ; Echo problem }

Storing results in tables www. cs. hmc. edu/ACM/ Find the number of prime factors in N! (1 is not prime. ) “dynamic programming” . . . 2 3 4 5 6 7 8 . . .

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 What? 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

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

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. push_front(42); // remove from front

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
- Slides: 19