made by electron & kk & rabbit125

advertisement
NCKU Programming Contest Training Course
Course 2
2014/12/24
Chia-Ying Lin (a711186)
a711186@gmail.com
http://myweb.ncku.edu.tw/~f74004088/course_2_20141224.ppt
Department of Computer Science and Information Engineering
National Cheng Kung University
Tainan, Taiwan
NCKU CSIE Programming Contest Training Course
byrabbit125
a711186
made by electronmade
& kk &
Outline
• Data Structure
– Basic STL
Vector
Stack
Queue
Heap
Map
Set
– Hash
NCKU CSIE Programming Contest Training Course
a711186
made by electron &made
kk & by
rabbit125
What is data structure?
• Wiki:
data structure is a particular way of organizing data in a
computer so that it can be used efficiently.
• Textbook:
Data structures is concerned with the representation and
manipulation of data.
NCKU CSIE Programming Contest Training Course
a711186
made by electron &made
kk & by
rabbit125
Basic STL
(Standard Template Library)
byrabbit125
a711186
made by electronmade
& kk &
What is STL?

Wiki :
STL is a software library for the C++ programming
language. The STL provides four components called
algorithms, containers, functional, and iterators.
 Algorithms: Include<algorithm>
ex. sort,min,max,lower_bound,upper_bound

Containers: data structure
Functional
Iterators: traverse a container

http://www.cplusplus.com/reference/


NCKU CSIE Programming Contest Training Course
a711186
made by electron &made
kk & by
rabbit125
Outline
• Data Structure
– Basic STL
Vector
Stack
Queue
Heap
Map
Set
– Hash
NCKU CSIE Programming Contest Training Course
a711186
made by electron &made
kk & by
rabbit125
Vector


Like a array, it can resize automatically when insert.
#include<vector>
宣告
新增值
大小
排序
清空
NCKU CSIE Programming Contest Training Course
取值
made
by mike199250
& a711186
made
by electron
& kk & rabbit125
Vector
• vs. Array
– 大小開到題目最大範圍
– 使用memset(快)
或for(慢)初始化
– PS:
memset(matrix, 1, sizeof(matrix)); //會失敗
memset把每個 byte 初始成指定的值,整數由4個byte組成設定的
值是0x01,但存入的值卻是0x0101,而導致錯誤的答案而-1則
是0xFF,存入的值是0xFFFF,不會影響答案。
– 因此memset較適合用來初始成較大的數。
NCKU CSIE Programming Contest Training Course
a711186
made by electron &made
kk & by
rabbit125
Outline
• Data Structure
– Basic STL
Vector
Stack
Queue
Heap
Map
Set
– Hash
NCKU CSIE Programming Contest Training Course
a711186
made by electron &made
kk & by
rabbit125
Stack



A stack is an ordered list in which insertions and
deletions are made at one end called the top.
Last-In-First-Out (LIFO)
top、push、pop、empty
pop
push
NCKU CSIE Programming Contest Training Course
made
&a711186
madebybyelectron
electron&&free999
kk & rabbit125
Stack
宣告
清空
NCKU CSIE Programming Contest Training Course
made
by mike199250
& a711186
made
by electron
& kk & rabbit125
Outline
• Data Structure
– Basic STL
Vector
Stack
Queue
Heap
Map
Set
– Hash
NCKU CSIE Programming Contest Training Course
a711186
made by electron &made
kk & by
rabbit125
Queue



A queue is an ordered list in which insertions and
deletions are made at one end called the front
First-In-First-Out (LIFO)
Push、pop、front、empty
pop
a0
a1
an-1
a2
push
front
NCKU CSIE Programming Contest Training Course
back
made
made
&a711186
by aj211y
madebybyelectron
electron&&free999
kk
& rabbit125
Queue
宣告
NCKU CSIE Programming Contest Training Course
mademade
by electron
& kk & rabbit125
by mike199250
& a711186
Example 1
UVA-673 Parantheses
You are given a string consisting of parentheses () and []. A
string of this type is said to be correct:
(a) if it is the empty string
(b) if A and B are correct, AB is correct,
(c) if A is correct, (A) and [A] is correct. Write a program that
takes a sequence of strings of this type and check their
correctness. Your program can assume that the maximum
string length is 128.
NCKU CSIE Programming Contest Training Course
by electron
& free999
made bymade
electron
& kk & rabbit125
Example 1
• Input
– The file contains a positive integer n and a sequence of n strings of
parentheses () and [], one string a line.
• Output
– A sequence of Yes or No on the output file.
• Sample Input
3
([])
(([()])))
([()[]()])()
• Sample Output
Yes
No
Yes
NCKU CSIE Programming Contest Training Course
by electron
& free999
made bymade
electron
& kk & rabbit125
Outline
• Data Structure
– Basic STL
Vector
Stack
Queue
Heap
Map
Set
– Hash
NCKU CSIE Programming Contest Training Course
a711186
made by electron &made
kk & by
rabbit125
Binary Heap

Binary Heap :
1) Complete binary tree
2) Every element matches heap condition
Min-heap
for all nodes i, excluding the root,
A[PARENT(i)] ≧ A[i].
NCKU CSIE Programming Contest Training Course
Max-heap
for all nodes i, excluding the root,
A[PARENT(i)] ≧ A[i].
a711186
made by electron &made
kk & by
rabbit125
Binary Heap

#include<queue>
NCKU CSIE Programming Contest Training Course
a711186
made by electron &made
kk & by
rabbit125
Outline
• Data Structure
– Basic STL
Vector
Stack
Queue
Heap
Map
Set
– Hash
NCKU CSIE Programming Contest Training Course
a711186
made by electron &made
kk & by
rabbit125
Map

combination of a key value and a mapped value,
following a specific order. Include<map>
NCKU CSIE Programming Contest Training Course
made
by mike199250
& a711186
made
by electron
& kk & rabbit125
Outline
• Data Structure
– Basic STL
Vector
Stack
Queue
Heap
Map
Set
– Hash
NCKU CSIE Programming Contest Training Course
a711186
made by electron &made
kk & by
rabbit125
Set


Sets are containers that store unique elements following
a specific order.
#include<set>
NCKU CSIE Programming Contest Training Course
made
bya711186
aj211y
made by electron &made
kk
& by
rabbit125
Example 2
Uva 10954 Add All
Input
Each test case will start with a positive number, N (2 ≤ N ≤
5000) followed by N positive integers (all are less than 100000). Input is
terminated by a case where the value of N is zero. This case should not be
processed.
Output
For each case print the minimum total cost of addition in a single line.
Sample Input
Output for Sample
3
9
123
19
4
1234
0
NCKU CSIE Programming Contest Training Course
by electron
& free999
made bymade
electron
& kk & rabbit125
Learn more:Iterator

Iterators: traverse a container
Container
declare
Vector
vector<int> myvector;
vector<int>::iterator it;
// erase the 6th element
myvector.erase (myvector.begin()+5);
// erase the first 3 elements: myvector.erase
(myvector.begin(),myvector.begin()+3);
map
map<int>::iterator it;
map<int>::iterator it;
for (it=mymap.begin(); it!=mymap.end(); ++it)
cout << it->first << " => " << it->second << '\n';
//first:key, second:value
set
set<int> myset;
set<int>::iterator it;
for (it=myset.begin(); it!=myset.end(); ++it)
cout << ' ' << *it;
NCKU CSIE Programming Contest Training Course
a711186
made by electron &made
kk & by
rabbit125
Learn more:
Other STL & comparison
• Other STL: list、pair、next_permutation…etc
made by a711186
• Comparison
Container
新增/去除值
取值
Vector
push_backO(1)
pop_back[去除尾端] (1)
erase[元素] O(n)
stack
清除
include
Useful
function
Var[index] O(1) Clear[全部]
<vecotr>
Lower_bound、
Upper_bound
push O(1)
pop O(1)
top() O(1)
While(!var.empty())
var.pop();
<stack>
queue
push O(1)
pop O(1)
front() O(1)
While(!var.empty())
var.pop();
<queue>
Heap
(priority_que
ue)
push O(logn)
pop O(logn)
top() O(1)
While(!var.empty())
var.pop();
<queue>
clear
<map>
map
var[key]=value O(logn)
Iterator O(1)
Insert withContest
pair Training Course
NCKU CSIE Programming
count
made by electron & kk & rabbit125
set
Insert O(logn)
Iterator O(1)
clear
<set>
count
Outline
• Data Structure
– Basic STL
Vector
Stack
Queue
Heap
Map
Set
– Hash
NCKU CSIE Programming Contest Training Course
a711186
made by electron &made
kk & by
rabbit125
Hash
made by electron & kk & rabbit125
Hash

Why?
– N number with numbered by 1~N
• easy to declare an array
– N number with range 1~2147483647
• uneasy to declare an array

What?
– O(1) access your data
– A Static Table with Fixed Size, using a function f(x) to transform a
key word x into a new address
Hash Function
key word
雜湊函數
NCKU CSIE Programming Contest Training Course
address
f (x)
mademade
by electron
& free999
&kevinx6000
by electron
& kk &
rabbit125
Hash
•
•
•
•
Bucket (桶):The Size of f(x) for the Hash Table
Slot (槽):The Size of each Bucket
Collision (碰撞): x1 != x2, while f(x1)=f(x2)
Overflow (溢位):The Size of Bucket is Full
Bucket
0
6
1
7
2
8
3
9
4
10
5
11
Slot
NCKU CSIE Programming Contest Training Course
made by electron & free999
Hash

How to hash?
NCKU CSIE Programming Contest Training Course
by kevinx6000
& a711186
mademade
by electron
& kk & rabbit125
Hash Function 1
• Mid-Square
– String → Integer → Square → Mid number
– EX.
• CFGA
3671(轉成數值)
[Set A=1,B=2…,Z=26]
13476241(取平方值)
762(取中間三位數)
h(CFGA)=762
NCKU CSIE Programming Contest Training Course
made by electron & kk & rabbit125
&kevinx6000
Hash Function 2
• Folding
• Shift folding
• Ex.
key word X
X=16732942159812
pick up for each three characters
h(X)=167+329+421+598+12=1527
NCKU CSIE Programming Contest Training Course
P1
167
P2
P3
P4
P5
329
421
598
12
summation
1527
made by electron & kk & rabbit125
Hash Function 3
• Shift reverse Folding
• Ex.
•
key word X
•
X=16732942159812
reverse
reverse
•
•
X=76192312489521
pick up for each three characters
h(X)=761+923+124+895+21=2724
P1
P2
P3
761
923
124
P4
P5
895
21
summation
NCKU CSIE Programming Contest Training Course
2724
mademade
by electron
& free999
&kevinx6000
by electron
& kk &
rabbit125
Hash Function 4
NCKU CSIE Programming Contest Training Course
mademade
by electron
& free999
&kevinx6000
by electron
& kk &
rabbit125
Hash STL
• Similar with … map
NCKU CSIE Programming Contest Training Course
by kevinx6000
& a711186
mademade
by electron
& kk & rabbit125
Overflow Handling 1
• Chaining(linked list)
– use the link list to store multiple key words (vector is powerful)
0
1
A
A2
null
2
C
5
F
…
A1
C1
null
Z1
null
null
key words:
A, A1, A2, C, C1, F, Z, Z1
…
25
Z
NCKU CSIE Programming Contest Training Course
made by electron & kk & rabbit125
Overflow Handling 2
• Linear Probing(Linear Open Addressing)
– When collision occurs, move to the next slot.
NCKU CSIE Programming Contest Training Course
a711186
made by electron &made
kk & by
rabbit125
http://eportfolio.tsu.edu.tw/blog/attach/646/2646/epform/12/epform_949755_2222257_49744.pdf
Overflow Handling 3
• Quadratic Probing (Quadratic Open Address)
–
NCKU CSIE Programming Contest Training Course
a711186
made by electron &made
kk & by
rabbit125
http://eportfolio.tsu.edu.tw/blog/attach/646/2646/epform/12/epform_949755_2222257_49744.pdf
Overflow Handling 4
• Rehashing
– 設置一系列 Hashing Function f1, f2, …., fm,當
使用 f1 產生 overflow時,則改用 f2,若 f2 又發
生 overflow,則改用 f3,…,直到沒有 overflow
發生為止。
NCKU CSIE Programming Contest Training Course
a711186
made by electron &made
kk & by
rabbit125
http://eportfolio.tsu.edu.tw/blog/attach/646/2646/epform/12/epform_949755_2222257_49744.pdf
Example 3
• POJ-2503 Babelfish
• Description
You have just moved from Waterloo to a big city. The people here speak an
incomprehensible dialect of a foreign language. Fortunately, you have a
dictionary to help you understand them.
• Input
Input consists of up to 100,000 dictionary entries, followed by a blank line,
followed by a message of up to 100,000 words. Each dictionary entry is a line
containing an English word, followed by a space and a foreign language
word. No foreign word appears more than once in the dictionary. The
message is a sequence of words in the foreign language, one word on each
line. Each word in the input is a sequence of at most 10 lowercase letters.
• Output
Output is the message translated to English, one word per line. Foreign words
not in the dictionary should be translated as "eh".
NCKU CSIE Programming Contest Training Course
made
a711186
made by electron &
kk &by
rabbit125
Example 3
• Sample Input
dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay
atcay
ittenkay
oopslay
NCKU CSIE Programming Contest Training Course
• Sample Output
cat
eh
loops
• Hint
Huge input and output,scanf
and printf are recommended.
made
a711186
made by electron &
kk &by
rabbit125
Example 4
• POJ-1200 Crazy Search
• Description
Many people like to solve hard puzzles some of which may lead them to
madness. One such puzzle could be finding a hidden prime number in a given
text. Such number could be the number of different substrings of a given size
that exist in the text. As you soon will discover, you really need the help of a
computer and a good algorithm to solve such a puzzle.
Your task is to write a program that given the size, N, of the substring, the
number of different characters that may occur in the text, NC, and the text
itself, determines the number of different substrings of size N that appear in
the text.
As an example, consider N=3, NC=4 and the text "daababac". The different
substrings of size 3 that can be found in this text are: "daa"; "aab"; "aba";
"bab"; "bac". Therefore, the answer should be 5.
NCKU CSIE Programming Contest Training Course
made
a711186
made by electron &
kk &by
rabbit125
Example 4
• Input
The first line of input consists of two numbers, N and NC, separated by
exactly one space. This is followed by the text where the search takes
place. You may assume that the maximum number of substrings formed
by the possible set of characters does not exceed 16 Millions.
• Output
The program should output just an integer corresponding to the number of
different substrings of size N found in the given text.
• Sample Input
•
•
34
daababac
Sample Output
5
Hint
Huge input,scanf is recommended.
NCKU CSIE Programming Contest Training Course
made
a711186
made by electron &
kk &by
rabbit125
NCKU CSIE Programming Contest Training Course
a711186
made by electron &made
kk & by
rabbit125
Homework
• UVA (total 12 problems)
– 501 、642、673 、755、 10017 、 10098、 10226 、10282、10420、10815 、
10954、11995
• POJ (total 10 problems)
– 1028、1035、1200、1840、2431、2442、2503、3274、3349、3481
NCKU CSIE Programming Contest Training Course
made by electron & kk & rabbit125
Download