The first lecture of the ACM Aleppo CPC training. The local contest of ICPC. This lecture will help you get started in programming contests word with the lower bound techniques. The lectures focus on the C++ programming language and the STL library to solve programming problems.
1. ACM Aleppo CPC Training
Part 1 C++ Programming Concepts
By Ahmad Bashar Eter
2. Strings:
• We’ve seen that the strings is a type that represent array of
characters.
• We’ve also seen that if we want to input a string we can use the
default cin with >> operator but this method input the first part of
the string until the first white space character.
• If we want to input a full line string we may use getline function.
• Note: sometimes the getline retune empty string because of some
characters in the cin buffer so in order to ensure good usage of the
function put cin.clear(); cin.ignore(); before you use getline.
• Or another method is to put the getline in while loop while the
string is empty.
• We will continue on the C++ string object and its functions.
3. Strings:
DescriptionFunction
Return iterator to beginningbegin
Return iterator to endend
Return length of stringlength, size
Clear stringclear
Test if string is emptyempty
Append to stringappend
Insert into stringinsert
Find content in stringfind
Erase characters from stringerase
Get C string equivalentc_str
For the full reference go to:
4. Sort Algorithm:
• A lot of problems can be solved easily if the input is sorted.
• So in order to sort any sequence of elements we may use the
function sort which exist in the C++ algorithms library.
• this function take 2 parameter pointer or iterator to the first
element and pointer or iterator to the end of the sequence.
• Note the end element is the first element out side the rang or the
sequence.
• Know we will take the rest of the session to solve problems.
5. Example 1: Cakeminator
You are given a rectangular cake, represented as an r × c grid. Each cell either has
an evil strawberry, or is empty. For example, a 3 × 4 cake may look as follows:
The cakeminator is going to eat the cake! Each time he eats, he chooses a row or
a column that does not contain any evil strawberries and contains at least one
cake cell that has not been
eaten before, and eats all the cake cells there.
He may decide to eat any number of times.
Please output the maximum number of cake
cells that the cakeminator can eat.
Solve at:
6. Example 1: Cakeminator
Input:
The first line contains two integers r and c (2 ≤ r, c ≤ 10), denoting the number of
rows and the number of columns of the cake. The next r lines each contains c
characters — the j-th character of the i-th line denotes the content of the cell at
row i and column j, and is either one of these:
'.' character denotes a cake cell with no evil strawberry;
'S' character denotes a cake cell with an evil strawberry.
Output:
Output the maximum number of cake cells that the cakeminator can eat.
Solve at:
7. Example 2: Little Elephant and Chess
The Little Elephant loves chess very much.
One day the Little Elephant and his friend decided to play chess. They've got the chess pieces
but the board is a problem. They've got an 8 × 8 checkered board, each square is painted either
black or white. The Little Elephant and his friend know that a proper chessboard doesn't have
any side-adjacent cells with the same color and the upper left cell is white. To play chess, they
want to make the board they have a proper chessboard. For that the friends can choose any
row of the board and cyclically shift the cells of the chosen row, that is, put the last (rightmost)
square on the first place in the row and shift the others one position to the right. You can run
the described operation multiple times (or not run it at all).
For example, if the first line of the board looks like that "BBBBBBWW" (the white cells of the
line are marked with character "W", the black cells are marked with character "B"), then after
one cyclic shift it will look like that "WBBBBBBW".
Help the Little Elephant and his friend to find out whether they can use any number of the
described operations to turn the board they have into a proper chessboard.
Solve at:
8. Example 2: Little Elephant and Chess
Input
The input consists of exactly eight lines. Each line contains exactly eight
characters "W" or "B" without any spaces: the j-th character in the i-th line
stands for the color of the j-th cell of the i-th row of the elephants' board.
Character "W" stands for the white color, character "B" stands for the black color.
Consider the rows of the board numbered from 1 to 8 from top to bottom, and
the columns — from 1 to 8 from left to right. The given board can initially be a
proper chessboard.
Output
In a single line print "YES" (without the quotes), if we can make the board a
proper chessboard and "NO" (without the quotes) otherwise.
Solve at:
9. Example 3: Checking the Calendar
You are given names of two days of the week.
Please, determine whether it is possible that during some non-leap year the first
day of some month was equal to the first day of the week you are given, while the
first day of the next month was equal to the second day of the week you are
given. Both months should belong to one year.
In this problem, we consider the Gregorian calendar to be used. The number of
months in this calendar is equal to 12. The number of days in months during any
non-leap year is: 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31.
Names of the days of the week are given with lowercase English letters: "monday",
"tuesday", "wednesday", "thursday", "friday", "saturday", "sunday".
Solve at:
10. Example 3: Checking the Calendar
Input
The input consists of two lines, each of them containing the name of exactly one
day of the week. It's guaranteed that each string in the input is from the set
"monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday".
Output
Print "YES" (without quotes) if such situation is possible during some non-leap
year. Otherwise, print "NO" (without quotes).
Solve at:
11. Example 4: Boy or Girl
Those days, many boys use beautiful girls' photos as avatars in forums. So it is
pretty hard to tell the gender of a user at the first glance. Last year, our hero
went to a forum and had a nice chat with a beauty (he thought so). After that
they talked very often and eventually they became a couple in the network.
But yesterday, he came to see "her" in the real world and found out "she" is
actually a very strong man! Our hero is very sad and he is too tired to love again
now. So he came up with a way to recognize users' genders by their user names.
This is his method: if the number of distinct characters in one's user name is odd,
then he is a male, otherwise she is a female. You are given the string that
denotes the user name, please help our hero to determine the gender of this
user by his method.
Solve at:
12. Example 4: Boy or Girl
Input
The first line contains a non-empty string, that contains only lowercase English
letters — the user name. This string contains at most 100 letters.
Output
If it is a female by our hero's method, print "CHAT WITH HER!" (without the
quotes), otherwise, print "IGNORE HIM!" (without the quotes).
Solve at:
13. Example 5: Amusing Joke
So, the New Year holidays are over. Santa Claus and his colleagues can take a rest and
have guests at last. When two "New Year and Christmas Men" meet, thear assistants
cut out of cardboard the letters from the guest's name and the host's name in honor of
this event. Then the hung the letters above the main entrance. One night, when
everyone went to bed, someone took all the letters of our characters' names. Then he
may have shuffled the letters and put them in one pile in front of the door.
The next morning it was impossible to find the culprit who had made the disorder. But
everybody wondered whether it is possible to restore the names of the host and his
guests from the letters lying at the door? That is, we need to verify that there are no
extra letters, and that nobody will need to cut more letters.
Help the "New Year and Christmas Men" and their friends to cope with this problem.
You are given both inscriptions that hung over the front door the previous night, and a
pile of letters that were found at the front door next morning.
Solve at:
14. Example 5: Amusing Joke
Input
The input file consists of three lines: the first line contains the guest's name, the
second line contains the name of the residence host and the third line contains
letters in a pile that were found at the door in the morning. All lines are not empty
and contain only uppercase Latin letters. The length of each line does not
exceed 100.
Output
Print "YES" without the quotes, if the letters in the pile could be permuted to make
the names of the "New Year and Christmas Men". Otherwise, print "NO" without
the quotes.
Solve at:
15. Other Exercises
Try these good exercises and remember the more you exercise, the
better you will become ;).
• Codeforces B. Pashmak and Flowers
• Codeforces A. Laptops
• Codeforces A. Wsay Too Long Words
• Codeforces A. String Task
And don’t forget to join the Facebook group:
Acm_Aleppo_CPC | Discussion
Also follow us on:
ACM ICPC Aleppo University