17/9/2015Provinci HKU 20071 ACM Programming Contests Website: Mailing list: Coordinator: Chan Ho Leung.

Slides:



Advertisements
Similar presentations
Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.
Advertisements

ACM Programming Team Bill Punch. ACM Programming Contest One of the premier programming competitions. Held every year since 1977 MSU was the winner that.
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
CSE 332: C++ Algorithms I The C++ Algorithm Libraries A standard collection of generic algorithms –Applicable to various types and containers E.g., sorting.
Beginning C++ Through Game Programming, Second Edition
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
. The Standard C++ Library. 2 Main Ideas Purpose Flexibility Efficiency Simple & Uniform Interface.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
CMSC 202 Lesson 24 Iterators and STL Containers. Warmup Write the class definition for the templated Bag class – A bag has: Random insertion Random removal.
Data Structures Using C++1 Chapter 13 Standard Template Library (STL) II.
CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++
Rossella Lau Lecture 12, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 12: An Introduction to the STL  Basic.
Discussion Section: HW1 and Programming Tips GS540.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
Standard Template Library C++ introduced both object-oriented ideas, as well as templates to C Templates are ways to write general code around objects.
ACM Programming Contests
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
How to win in ACM/ICPC? Four levels of programmers 1. Implementation ◦ know the language well, translate idea to programs 2. Algorithms ◦
Lecture 23 Today Standard Template Library Programs in: programs/p19 Bibliography: Textbook p.252,
CS223 Algorithms D-Term 2013 Instructor: Mohamed Eltabakh WPI, CS Introduction Slide 1.
Data Structures Using C++ 2E
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
ACM Programming Contests Coordinator: Dr. Hubert Chan Coach: Luyi Mo Training Holder: Jonathan So Website: i.cs.hku.hk/~provinci.
Chapter 16: Searching, Sorting, and the vector Type.
CSE 425: Data Types II Survey of Common Types I Records –E.g., structs in C++ –If elements are named, a record is projected into its fields (e.g., via.
CS 114 – Class 02 Topics  Computer programs  Using the compiler Assignments  Read pages for Thursday.  We will go to the lab on Thursday.
PRESENTED BY: RAJKRISHNADEEPAK.VUYYURU SWAMYCHANDAN.DONDAPATI VINESHKUMARREDDY.LANKA RAJSEKHARTIRUMALA KANDURI ALAN.
1 Today’s Objectives  Announcements The Final Exam will be on Monday, 31-Jul, at 6 p.m. – There is no alternate time and no makeup!  Intro to the Standard.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Chapter 3 Sec 3.3 With Question/Answer Animations 1.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Generic Programming Using the C++ Standard Template Library.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Standard Template Library (STL)
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
Software Design 1.1 Tapestry classes -> STL l What’s the difference between tvector and vector  Safety and the kitchen sink What happens with t[21] on.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 22: Standard Template Library (STL)
ACM programming contest Introduction + Recursion.
CSE 332: C++ STL containers Review: C++ Standard Template Library (STL) The STL is a collection of related software elements –Containers Data structures:
Data Structure Introduction.
An Introduction to STL. The C++ Standard Template Libraries  In 1990, Alex Stepanov and Meng Lee of Hewlett Packard Laboratories extended C++ with a.
Algorithms and data structures Protected by
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
1 Iterators Good reference site:
 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer Chapter 15 - Class string and String Stream.
CSCI 51 Introduction to Programming March 12, 2009.
Welcoming Lecture Meeting/Discussion/Party By David/Denon.
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
Copyright © 2009 – Curt Hill Standard Template Library An Introduction.
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
CSE 332: C++ STL iterators What is an Iterator? An iterator must be able to do 2 main things –Point to the start of a range of elements (in a container)
Announcements You will receive your scores back for Assignment 2 this week. You will have an opportunity to correct your code and resubmit it for partial.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Unit VI.  C++ templates are a powerful mechanism for code reuse, as they enable the programmer to write code (classes as well as functions) that behaves.
CS212: Object Oriented Analysis and Design
Recursion Version 1.0.
Standard Template Library (STL)
Tuesday, February 20, 2018 Announcements… For Today… 4+ For Next Time…
Recitation Outline C++ STL associative containers Examples
Data structures and algorithms
Standard Template Library
the Standard Template Library
An Introduction to STL.
Presentation transcript:

17/9/2015Provinci HKU ACM Programming Contests Website: Mailing list: Coordinator: Chan Ho Leung

17/9/2015Provinci HKU Introduction ● The ACM collegiate programming contests are the major programming challenge for the university students. ● ● World Finals is held around April every year. ● To earn the free ticket to this glory battlefield, we need to excel in the Regional competitions. – To be held in late October and November.

17/9/2015Provinci HKU Team competition ● Each team, consisting of 3 students, works out a number of questions using a single computer. – Well, this needs some coordination among the members. ● Each university can send at most 3 teams to each regional contest. – Each team can join at most 2 regionals. ● The exact competition format will be introduced in due course.

17/9/2015Provinci HKU Our schedule... ● We’ll have a team formation test by the end of September. ● Depending on the results, we’ll select about 3 to 4 teams to enter the regional contest(s). – Special training for the competition will be held after the team formation test.

17/9/2015Provinci HKU Training sessions before test ● To prepare the team formation test, you can join the training sessions this month. – These training sessions are optional and you can join the test directly. ● Topics to be covered: – STL – Dynamic programming – Graph algorithms – Combinatorics – Problems require specific math concepts

17/9/2015Provinci HKU Practice make perfect ● An archive of problems can be found in the site – ● It also has an on-line judge to test your submitted program. – Simply register and apply for an account. ● This archive site will be a major source of training questions.

17/9/2015Provinci HKU Tips for the competition ● In the competition, you’re asked to write a number of programs. ● Only program correctness counts, subject to some time limits (usually 10 sec). – You can submit as many times as you want to the judge. ● Teams solving more problems win. – Tie break on smaller number of submissions. ● Not our concern at the moment...

17/9/2015Provinci HKU Programming languages ● In the competition, you can write the programs in C,C++, Java ● We’ll focus on C++ for the sake of efficient and convenient. ● One of the goodies of C++ is the Standard Template Library (STL). – Many useful data structures and algorithms have been implemented. – Their implementations are always optimal.

17/9/2015Provinci HKU Efficiency ● The implementation of STL algorithm satisfies not only the requirement, but also efficiency. ● The searching, sorting, etc., algorithm are implemented in optimal time complexity. – O(log n) for binary search, O(n log n) for sorting, etc. ● The set and map are implemented using red- black tree.

17/9/2015Provinci HKU Getting inputs... ● Let’s see problem 483. – ● This is a typical question that requires a program to read a number of lines, and then to process each line. ● A common framework for this in C++: string line; while (getline(cin, line)){ //.. to process a line }

17/9/2015Provinci HKU For each words... ● For each line, we need to reverse every word separated by white space. – Tokenizing the input stream ● If the number of white space between words need not preserved in the output, string stream is a good solution. istringstream ins(line); string word; while (ins >> word){ // reverse word }

17/9/2015Provinci HKU To reverse something... ● The last part is to reverse a word. ● You may instantly thinking about a loop to do this. ● Let’s try the STL reverse() function. for (size_t i = 0; i < words.size(); ++i) cout << words[words.size() - i - 1]; reverse(word.begin(), word.end()); cout << word;

17/9/2015Provinci HKU Algorithm & Iterator ● One of the simple algorithms in STL. – See ● This function, as well as other STL algorithms, takes two iterators as arguments. – Imagine that iterator is class-version pointer. – word.begin() is pointing at the first position; – word.end() points to the “pass-the-end”

17/9/2015Provinci HKU Container & iterator ● In C++, you can consider that any object that stores a sequence of (homogeneous) data is a container. – Array, strings, vector, set,... ● To traverse a container, you can use the subscript operator or iterator. ● Accessing a container in terms of iterator allows the generic design of many STL algorithms.

17/9/2015Provinci HKU Examples ● To reverse the elements of a vector. ● To sort the characters of a string ● To find the maximum... reverse(myvec.begin(), myvec.end()); sort(word.begin(), word.end(); int a[] = { 2, 9, 5, 3, 4, 0 }; sort(a, a + sizeof(a)/sizeof(a[0]); vector ::iterator p; p = max(myvec.begin(), myvec.end());

17/9/2015Provinci HKU About iterators... ● Due to the operator overloading of C++, we can access the object “pointed” by an iterator using the dereference operator. – e.g., cout << *p << endl; ● Note that iterator is usually associated with a particular container type. Its type is closely tied with the container. vector ::iterator p; string::iterator q; map ::iterator r;

17/9/2015Provinci HKU Typedef... ● To save some typing headache, you can use typedef to create a type alias: ● You can also define type alias for iterator type: typedef vector MyCont; MyCont::iterator q; typedef map Dict; Dict::iterator r; typedef vector MyCont; typedef MyCont::iterator MyCont_it; MyCont_it p;

17/9/2015Provinci HKU The set container ● See problem 353. – ● In this question, you’re asked to find, for a given string, the number of unique substrings that is palindrome. e.g., – “boy” has 3 palindrome substring – “adam” has 5... – “tot” has 3...

17/9/2015Provinci HKU The STL way... ● How to test if a string is a palindrome? – Please make good use of STL :-) ● To count the number of UNIQUE palindrome substrings, – Enumerate all possible substring ● For each substring, check if it’s a palindrome. ● If yes, insert into a set. – Print the size of the set. ● Correct?

17/9/2015Provinci HKU map ● See problem 401. ● This question extends the palindrome problem a bit. – Some characters have mirrored version; e.g., ‘A’ and ‘A’, ‘E’ and ‘3’, ‘L’ and ‘J’, etc. – Some characters do not have mirrored image; e.g., ‘B’, ‘C’, ‘P’, ‘Q’, etc. ● Then we can defined mirrored string: – ‘3AIAE’, ‘2A3MEAS’

17/9/2015Provinci HKU ● To check if a string is a mirror, – For each character in the string, replace it with its mirrored version. – Check if the reversed of this string equals the original string. ● In doing the replacement, we need to find the corresponding mirrored version of a character. – This can be done by storing the correspondence in a map container.

17/9/2015Provinci HKU Map usage // declaration map mtbl; // associate key-value pairs mtbl[‘A’] = ‘A’; mtbl[‘B’] = ‘ ‘; // no mirror... mtbl[‘E’] = ‘3’;.... string s = “3AIAE”; string ms; for (size_t i = 0; i < s.length(); ++i) ms += mtbl[s[i]];

17/9/2015Provinci HKU Caveats ● The map is good for “database”-like container such as mapping a string to another string; e.g., – u.no => name – Key => value map db; db[‘one’] = 1; db[‘two’] = 2;...

17/9/2015Provinci HKU Find before use? ● A common problem is to check if a given key is mapped to certain value. – If (db[key] == value){... } // dangerous – The testing work only if the key has been inserted into the map before. ● If you’re not sure, check the existence of the key first; e.g., map ::iterator p = db.find(key); if (p != db.end()){ //... }

17/9/2015Provinci HKU Vector or array? ● Array is good for holding initial values. – The size must be fixed at compile time. ● Vector is more flexible. – New elements can be added (at the end) using push_back(). – Use pop_back() to remove the last one. ● Sometimes we may use both :-) – Initialize an array and then copy into a vector or other container.

17/9/2015Provinci HKU Exercises ● Starters: Problems 642, 10107, ● Main course: For Problems & 291, try the next_permutation() from STL. ● Dessert: Problem 195 – Print all the permutation of a string in alphabetically ascending order. – An upper case letter goes before the corresponding lower case letter.

17/9/2015Provinci HKU Comparator for sort() ● The sort() function by default compares two elements using the operator<(). ● We can change the default comparison by plugging in different comparator. – In the simplest case, a comparator is just a boolean function. bool myless(char a, char b){ char la = tolower(a), lb = tolower(b); return la < lb || la == lb && a < b; } sort(mystr.begin(), mystr.end(), myless);

17/9/2015Provinci HKU Summary ● Get familiar with the STL library. – Data structure: vector, map, set are of particular useful. – Algorithms: sort(), bsearch(), find(), count(), max(), min(), save many coding time. ● Behavior of algorithms can be changed by plugging functional object (e.g., comparator in sort()). – Iterators are the interface between data structures and algorithms.

17/9/2015Provinci HKU References ● “Effective STL” by Scott Meyers, Addison- Wesley. ● “Accelerated C++: practical programming by example” by A. Koenig, B. Moo, Addison Wesley. ● “The C++ programming language” by B. Stroustrup.