Title: Small trick, more STL, Prime algorithm
1Small trick, more STL,Prime algorithm
- Small trick for your coding
- STL Powerful C tools
- Prime numbers algorithm
2Plan
- Some simple tricks
- More STL vector, set, map, queue,
- Iterator
- Compare function for STL
- Prime numbers algorithm
3Programming style
- Short variable names are usually okay
- Quote from Issacs notes
- With proper indentation for every
- Your partner may read your code when debugging
- For eye-ball debug
4Coding
- Coding speed is very important
- The program only need to be fast enough for
judge to accept it. Dont over-optimize. - The operation of the program should not over 109
5Array
- Always give more space to the array
- If you think the array will store 100 elements,
please set the array size to be 105. - To avoid invalid memory access.
- Use int a100 instead of inta new100
- To avoid memory management
- Unless you are the expert in memory management
- Just for ACM, you may deduct mark if you apply it
in assignment p
6Input
- To detect end of file,
- while (cin gtgt in)
- while (scanf(d,n)!EOF)
- To detect zero terminate,
- while (cin gtgt in, in)
- To read line,
- while (getline(cin,str))
- To ignore white space,
- cin gtgt ws
7Floating point
- Handle precision error
- Assume error not more then 1e-8
- const double e 1e-8
- check a equal to b
- fabs(a-b) lt e
- check a less than or equal to b
- a lt b fabs(a-b) lt e
8Dummy element
- Add one dummy element at the front or back of an
array may help your coding easier - Handle the special case
9Testing you program
- Make use of command promote
- In Windows
- ..gt ExecuteFile lt Input_File gt Output_File
- Eg. C\gt 100.exe lt 100.txt gt output.txt
- In Linux
- ./ExecuteFile lt Input_File gt Output_File
- Eg. ./100 lt 100.txt gt output.txt
10Structure
struct st int i // member variable string
s int arr10 st()s(testing) // if you
have other constructer, // you should
add this line st(int a, string b)i(a),s(b) //
the constructor you want memset(arr,0,sizeof(a
rr)) bool operatorlt(const st ss) //
will be very useful return i lt ss.i i
ss.i s lt ss.s
11Structure
void sortArr() // member function
sort(arr,arrsizeof(arr)/sizeof(int))
int main() st temp1 st
temp2(10,AAA) temp1.sortArr() // can sort
arr cout ltlt temp1.s ltlt ltlt temp2.s ltlt endl
// testing AAA
12STL include
13STL include
If you are not sure which library you should
include, please include all related library.
14STL vector and deque
- Useful member functions
- push_back add an element to the back
- pop_back remove the last element
- clear clear the container (erase all element)
- size return the size of the container
- erase delete one element or a range of element
- Difference between vector and deque
- vector is more efficient
- deque have push_front and pop_front, but vector
doesnt
15STL set and multiset
- All elements inside set and multiset are already
sorted - Useful member function
- insert insert an element to set . O(log n)
- find return the iterator point to element x
- if (s.find(x) ! s.end()) / s has x /
- size return the size of the container
- clear clear the container (delete all element)
- begin, end return the begin/end iterator
16STL set and multiset
- Difference between set and multiset
- set will eliminate element that already inside
that set - multiset will not
- The element must be comparable
- bool operatorlt(const type t) const
- Usage
- Find all of distinct elements
- Check the element is appeared before
- Sort all elements in every step (every insert
operation)
17STL queue and priority_queue
- queue first in first out
- priority_queue the largest element always on
top - Useful member function
- push insert an element to queue
- front() / top() return the first element of the
queue / priority_queue - pop remove the first element
- size return the size of the queue
- empty return (size() 0)
- Reset the queue
- q queuelttypegt() // assume we want to clear
q
18STL queue and priority_queue
- Difference between queue and priority_queue
19STL map
- Map one type to another type
- Useful member functions
- find return the iterator point to index x
- if (m.find(x)m.end()) mx y // not found x
- clear clear the container (delete all
index/element) - size return the number of index of the map
- The indexs type must be comparable
- bool operatorlt(const type t) const
20STL map
- Usage
- Mapping between two types using O(log n)
- Just like a hash table
- Sometimes you need mapltint,intgt
- If the problem given a graph have at most 100
node, - the id of the every node is lt 231 1
- Use map to help you give the new id (1n) to
every node . - Limit the memory size
- Just like the above example, we use O(n) memory
to store the node instead of 231-1
21STL iterator
- Just like a pointer
- Basically, nearly (every?) iterator has following
operator - , , --
- Every STLs .begin() and .end() are iterators
- Which are constant iterators
- Dont apply or -- to it
- Random access iterator vector and deques
iterator - Very similar to pointer
- Can use , , iter5 // iter is a random access
iterator - V.erase(V.begin() 3) // assume V is a vector
22STL Compare function
- Why we need compare function
- For sorting using STL sort
- For using some STLs structure map,
priority_queue, set - For binary search using STL lower_bound,
upper_bound - For your own convenient P
23STL Compare function
- We only need less than compare function in most
cases - There are few way to write it
- See compare_ex.cpp
24Prime numbers algorithm
- Basic algorithm good for individual query
- Testing if a number n is prime or not
- Assuming that you already have a array of primes
- int upper (int)sqrt((double)n)
- for (i 0 primeiltupper i)
- if (nprimei0) return false
- return true
25Prime numbers algorithm
- How about asking a range of prime number?
- Lets say all prime numbers from 1 to 1000000???
- We can use more efficient algorithm
26Prime numbers algorithm
- Generate 1100 prime number
27Prime numbers algorithm
- First step assume all element multiply by 2 is
not prime
28Prime numbers algorithm
- Second step
- For every odd number gt 3 and lt sqrt(n)
- If P is prime // assume current number is P
- Eliminate all element which is P(2k1) where k
1,2, -
-
29Prime numbers algorithm
- P 3, Since 3 is not eliminated, so 3 is prime
30Prime numbers algorithm
- P 5, Since 5 is not eliminated, so 5 is prime
31Prime numbers algorithm
- P 7, Since 7 is not eliminated, so 7 is prime
32Prime numbers algorithm
- P 9, Since 9 is eliminated, so 9 is not a prime.
33Prime numbers algorithm
- bool nonprime1000006 // in global, all
element already set to 0 (false) - int main()
- int n 1000000
- int upper (int)sqrt((double)n)
- for (int i 3 i lt upper i2)
- if (!nonprimei)
- int offset i2
- for (int j i3 j lt n joffset)
- nonprimej true
-
-
-
34Prime numbers algorithm
- How about finding all prime factors for
11000000?? - In previous slides, we use bool to mark the
primes - Actually we can use int instead of bool
- Initial all value to 0
- When eliminate, set that element to the current
prime number
35Prime numbers algorithm
36Prime numbers algorithm
- Since we assume 2 is eliminated, we should have
special handle for 2 - void print_factor(int n)
- if (n lt 1) return
- while (n 2 0)
- cout ltlt "2 "
- n/2
-
- if (n 1) return
- while (nonprimen)
- cout ltlt nonprimen ltlt ' '
- n/nonprimen
-
- cout ltlt n
37Prime numbers algorithm
- int nonprime1000006 // in global, all element
already set to 0 - int main()
- int n 1000000
- int upper (int)sqrt((double)n)
- for (int i 3 i lt upper i2)
- if (!nonprimei)
- int offset i2
- for (int j i3 j lt n joffset)
- nonprimej i
-
-
38Practice
- STL
- 136,146,156,482,484,10098,10107,10282,10896,10954
- Prime
- 884,897,10006,10015(Joseph Number),
10235,10699,10738,10789,10856