CS 60 Slides - PowerPoint PPT Presentation

About This Presentation
Title:

CS 60 Slides

Description:

Sometimes a problem is much more difficult than expected! strategize as a team to find an algorithm Basic approach: implement (1 or 2 people coding) – PowerPoint PPT presentation

Number of Views:7
Avg rating:3.0/5.0
Slides: 27
Provided by: kell1258
Learn more at: https://www.cs.hmc.edu
Category:

less

Transcript and Presenter's Notes

Title: CS 60 Slides


1
ACM notes
  • Sometimes a problem is much more difficult than
    expected!

strategize as a team to find an algorithm
  • Basic approach

implement (1 or 2 people coding)
debugging
Sigma problem
1 2 3 1 2 4 1 2 4 8 9 1 2 4 6 9 17 34 68 77 1 2 4
8 16 32 64 80 1 2 4 8 16 24 28 29 58 87 1 2 4 8
16 32 33 66 99
1 2 3 1 2 4 1 2 4 8 9 1 2 4 5 9 18 36 72 77 1 2 4
8 16 32 64 80 1 2 3 6 12 24 48 72 84 87 1 2 3 6
12 24 48 96 99
stated answers
better answers
2
Change
const int p1,n5,d10,q25,h50 int
counter0 int pn, nn, dn, qn, hn for
(hn 0 hnh lt num hn) for (qn 0
hnh qnq lt num qn) for (dn 0
hnh qnq dnd lt num dn) for
(nn 0 hnh qnq dnd nnn lt num nn)
for (pn 0 hnh qnq dnd nnn
pnp lt num pn)
if (hnh qnq dnd nnn pnp num)
counter
Brute Force
Dynamic Programming
total
1
2
3
5
6
7
9
10
11
0
4
8
12
using
1
1, 5
1, 5, 10
3
Change
const int p1,n5,d10,q25,h50 int
counter0 int pn, nn, dn, qn, hn for
(hn 0 hnh lt num hn) for (qn 0
hnh qnq lt num qn) for (dn 0
hnh qnq dnd lt num dn) for
(nn 0 hnh qnq dnd nnn lt num nn)
for (pn 0 hnh qnq dnd nnn
pnp lt num pn)
if (hnh qnq dnd nnn pnp num)
counter
Brute Force
Dynamic Programming
total
1
2
3
5
6
7
9
10
11
0
4
8
12
using
1
1
1
1
1
1
1
1
1
1
1
1
1, 5
1
1
1
2
2
2
2
1
1
2
1
1
1
2
2
2
2
1
1, 5, 10
1
2
4
Change
const int p1,n5,d10,q25,h50 int
counter0 int pn, nn, dn, qn, hn for
(hn 0 hnh lt num hn) for (qn 0
hnh qnq lt num qn) for (dn 0
hnh qnq dnd lt num dn) for
(nn 0 hnh qnq dnd nnn lt num nn)
for (pn 0 hnh qnq dnd nnn
pnp lt num pn)
if (hnh qnq dnd nnn pnp num)
counter
Brute Force
Dynamic Programming
total
1
2
3
5
6
7
9
10
11
0
4
8
12
using
1
1
1
1
1
1
1
1
1
1
1
1
1, 5
1
1
1
2
2
2
2
1
1
2
add
1
1
1
2
2
2
2
1
1, 5, 10
1
2
5
Change
const int p1,n5,d10,q25,h50 int
counter0 int pn, nn, dn, qn, hn for
(hn 0 hnh lt num hn) for (qn 0
hnh qnq lt num qn) for (dn 0
hnh qnq dnd lt num dn) for
(nn 0 hnh qnq dnd nnn lt num nn)
for (pn 0 hnh qnq dnd nnn
pnp lt num pn)
if (hnh qnq dnd nnn pnp num)
counter
Brute Force
Dynamic Programming
total
1
2
3
5
6
7
9
10
11
0
4
8
12
using
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1, 5
1
1
1
2
2
2
2
1
1
2
3
3
3
1
1
1
2
2
2
2
1
1, 5, 10
1
2
4
4
4
6
Prime numbers
Check for divisibility by integers up to the
square root of N.
Or, just count factors...
2
3
4
5
6
7
8
9
10
Bug of the week
Prime suspects
define MAXFAC 1000005 int facMAXFAC
int factorTable new int1000000
7
Problems
Entropy
bits used in ASCII
Input
Output
AAAAABCD
64 13 4.9
compression ratio, to 1 place of precision
bits used in an optimal prefix-free encoding
8
Problems
Entropy
bits used in ASCII
Input
Output
AAAAABCD
64 13 4.9
compression ratio, to 1 place of precision
bits used in an optimal prefix-free encoding
A 0
B 10
C 110
D 111
9
Problems
N-Credible Mazes
dimensions
Input
Output
2 0 0 2 2 0 0 0 1 0 1 0 2 0 2 1 2 0
2 0 3 1 2 2 2 -1
start
end
Maze 1 can be travelled
(or not)
edge start
edge end
10
C STL
www.dinkumware.com/htm_cpl/index.html
www.sgi.com/tech/stl/
setltintgt 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 setltintgtiterator i // may
want to typedef // think of an iterator as a
pointer i s.find(42) // return 42s
iterator cout ltlt (i) ltlt endl // prints 42 cout
ltlt (--i)) // prints ... i s.find(43)
// not there ! // at this point ( i
s.end() ) is true s.erase(-9) //
removing elements multisetltintgt m // holds
multiple copies
set
include ltsetgt
multiset
include ltsetgt
11
Breadth-first search
  • algorithm

data structures
queue, deque, hashtable (map)
12
C STL
www.dinkumware.com/htm_cpl/index.html
www.sgi.com/tech/stl/
vectorltintgt 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 vi //
ith element sort( v.begin(), v.end() ) //
default sort sort( v.begin(), v.end(), mycompare
) dequeltintgt d // double-ended
queue d.push_front(42) // add to
front d.front(42) // return front
element d.pop_front(42) // remove from front
vector
include ltvectorgt
sort
include ltalgorithmgt
last time
deque
include ltdequegt
13
Other problems
  • Change counting

input
output
1.00 0.06 0
There are 292 ways to make 1.00 There are 2 ways
to make 0.06
  • Sigma series

Shortest sequences from 1 to N such that each
element is the sum of two previous elements.
input
output
3 4 87 99 -1
1 2 3 1 2 4 1 2 4 8 16 24 28 29 58 87 1 2 4 8 16
32 33 66 99
14
Useful C functions
int atoi(char s) double atof(char s) int
strcasecomp(char s1, char s2) long
strtol(char s, NULL, int base)
converts C strings to ints
atoi(100) 100
converts C strings to doubles
atoi(100.0) 100.0
case-insensitive C string comparison
strcasecmp(aCm,ACm) 0
arbitrary conversion from a string in bases
(2-36) to a long int
strtol(Charlie, NULL, 36) 2147483647L
use man for more...
15
sprintf
int sprintf(char str, char format, ...)
prints anything to the string str
char str100 sprintf(str,d,42) // str
is 42 sprintf(str,f,42.0) // str is
42.0 sprintf(str,10d,42) // str is
42 sprintf(str,-10d,42) // str is
42
flexible formatting
right/left justify
16
Two ACM programming skills
A chance to improve your C/C
Preparation for the ACM competition ...
Problem Insight and Execution ...
2
1
Anxiety!
Get into the minds of the judges
17
Key Skill 1 mindreading
100
0
What cases should I handle? spectrum
18
Key Skill 2 anxiety
Anxiety!
19
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(3n1) 1 if n odd
T(n) T(n/2) 1 if n even
20
Dynamic Programming
Keys create a table of partial results,
articulate what each table cell means, then
build it up...
divis.cc
j items considered so far
Table T
0
1
2
3
4
5
6
1 1 6 2 -3
0
the list
1
4
i possible remainder
the divisor
2
3
Tij is 1 if i is a possible remainder using
the first j items in the list.
21
Dynamic programs can be short
include ltcstdiogt include ltiostreamgt include
ltvectorgt vectorltintgt v(10000) vectorltboolgt
m(100) // old mods vectorltboolgt m2(100) //
new mods int n, k bool divisible()
fill(m.begin(),m.end(),false) m0 true
for (int i0 iltn i) / not
giving away all of the code / / here
the table is built (6 lines) /
return m0
int main() cin gtgt n // garbage
while (cin gtgt n) cin gtgt k for
(int i0 iltn i) cin gtgt vi
vi abs(vi) vi
k cout ltlt (divisible() ? "D"
"Not d") ltlt "ivisible\n"
cout ltlt endl
acknowledgment Matt Brubeck
STL http//www.sgi.com/Technology/STL
22
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
filling in the table in the divis problem
getting the input in the pea problem
for (int j1 jltN j) cin gtgt Arrayi
Tablei n k 1 Tablei - n k 1
23
New Problem
Word Chains
doze aplomb ceded dozen envy ballistic yearn
hertz jazz hajj zeroth
Input A list of words
Output yes or no -- can these words be chained
together such that the last letter of one is
the first letter of the next ?
24
Knapsack Problem
object wt. val. 1 3 8
2 2 5 3 1 1 4
2 5
Maximize loot w/ weight limit of 4.
w
Weight available for use
Number of objects considered
0
1
2
3
4
1
V(n,w) max value stealable w/ n objects
w weight
2
3
4
n
V(n,w)
25
C Output
printf, fprintf, sprintf(char s, const char
format, )
the destination
the format string
the values
possible format strings
h
.4
12
-0

d
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 lt -3
or -4 c character s string n outputs of
chars written !! two of these print a
type
size modifier
minimum field width
precision
allowed size modifiers
flags
- left-justify 0 pad w/ zeros
use sign ( or -) (space) use sign ( or -)
deviant operation
h short l long (lowercase L) L long double
start character
26
C Output
value 42
value -42
10.4d
0042
-0042
-12x
0x2a
0xffffffd6
value 42
value -42.419
10.4g
42
-42.42
- 10.4g
42
-42.42
-10.4g
42.00
-42.42
value forty-two
10.5s
forty
Write a Comment
User Comments (0)
About PowerShow.com