Problems:
- Chihiro Fujisaki and Caesar Cipher
- Chihiro Fujisaki and Keyworded Caesar Cipher
- Nagito Komaeda and lucky days
- Chiaki Nanami Wants to Play Games
- Gundham Tanaka and the Dark Devas of Destruction
CS100 Homework 3 (Spring 2023)
Deadline: 2023-03-15 (Wed) 23:59:59
Late submission will open for 24 hours after the deadline, with 50% point deduction.
If you get full marks in this assignment by no more than 40 submission attempts and also solve the challenge in problem 4, you can earn special OJ displays and a "1-case protection" that can be used in further assignments to cancel one testcase failure. See Piazza or OJ dashboard for more information.
Chihiro Fujisaki and Caesar Cipher
"If I don't do something, nothing's ever going to change."
Description:
Chihiro Fujisaki, the Ultimate Programmer, is developing a new kind of artificial intelligence called Alter Ego. Now he is training the artificial intelligence secretly in order to prevent Monokuma spotting it.
Fujisaki is training Alter Ego with Caesar cipher, an ancient encryption algorithm. With a fixed number as key
, for example, , Caesar cipher replaces each letter in the plaintext a letter positions down the alphabet. In this case, the message box
becomes era
, because going down letters turns b
into e
and turns x
, going through the alphabet, into a
.
Fujisaki's messages contain uppercase letters, lowercase letters, digits, and other characters like spaces and punctuations. He doesn't want to scramble his messages, so he keeps the cases and encrypts uppercase letters, lowercase letters and digits respectively (so that Z
encodes to C
and 9
to 2
). All other characters, including spaces and punctuations, are not encoded.
Monokuma has discovered this secret, and has obtained the secret key
. Please help Monokuma to decrypt Fujisaki's messages.
Input Format:
The input contains two lines:
The first line contains a number , the key
for Caesar cipher. shifts the message right (going down the alphabet), and shifts it left (going up the alphabet).
The second line contains a non-empty string ciphertext
, the encoded text, ending with a line break character ('\n'
). Only uppercase letters, lowercase letters, and digits have been encoded.
It is guaranteed that .
Output Format:
You need to print the decoded message in one line.
Sample Input 1
1 | 8 |
Sample Input 2
1 | -30 |
Sample Outputs...?
Whoops, ciphers are no more secret if we tell you the answers! Why not figure out yourself? Hints: the above samples all decode to proper English sentences.
Test cases
Chihiro Fujisaki and Keyworded Caesar Cipher
"I'm scared, but...I can handle it. I...don't really understand why, but...
When I think about everyone else, my courage starts to grow...!"
Description:
Monokuma has cracked Chihiro Fujisaki's Caesar Cipher. The Ultimate Programmer Fujisaki is working on a more secure code called "Keyworded Caesar Cipher". Let Fujisaki explain his genius ideas to you:
"My keyworded Caesar cipher no longer has a numerical key
, but a keyword
, for example, "Wednesday"
. Just as the original Caesar cipher creates a shifted alphabet for encoding letters, so too does my keyworded Caesar cipher have one, and this encoding alphabet is created by the keyword
"Wednesday"
.
"I first turn all letters into lowercase, and remove duplicate letters in the keyword, keeping only their first occurences. Now "Wednesday"
has become "wednsay"
.
"Then I write the alphabet a
through z
in one line, and my new keyword "wednsay"
beneath it.
a b c d e f g h i j k l m n o p q r s t u v w x y z
w e d n s a y
"And finally, I fill the rest of my encoding alphabet starting from the last letter y
. However, letters in the keyword are already used. I should skip these letters when filling the rest, because I cannot let two letters encode to a same value.
a b c d e f g h i j k l m n o p q r s t u v w x y z
w e d n s a y z b c f g h i j k l m o p q r t u v x
"Alright. I can use it to encode a
to w
, z
to x
, and my name Chihiro
to Dzbzbmj
! For uppercase letters I can do the same, but not for digits any more. It's no big deal. I know in homework 1 you turned numbers into English words, right? (laughs)"
How nice would it be, if only you and he know the secret! Monokuma has heared all your conversation and learned his new method. More importantly, Monokuma has obtained the keyword
! Please help Monokuma again to decrypt Fujisaki's messages.
Input Format:
The input contains two lines:
The first line contains a non-empty string keyword
, containing only uppercase and lowercase letters.
The second line contains a non-empty string ciphertext
, the encoded text, ending with a line break character ('\n'
). Only uppercase and lowercase letters have been encoded.
It is guaranteed that .
Output Format:
You need to print the decoded message in one line.
Sample Input 1
1 | CPPistheBESTlanguage |
Sample Input 2
1 | CPPistheBESTlanguage |
Sample Input 3
1 | ProgrammingIsFun |
Sample Outputs...?
Secret, of course! Again, they all decode to proper English sentences.
Hints
You may find some functions in <string.h>
useful, for example, strlen
, strchr
, strcmp
. Here is a full list of standard library support for string and char manipulations.
Test cases
Nagito Komaeda and lucky days
"I believe my actions will become the foundation of this world's hope. And...if that really happens... Praise me."
Description
Some people believe numbers with certain properties, like palindromes, are special, so does Nagito Komaeda. Komaeda wants to choose a date for himself to do some important things. As the Ultimate Lucky Student, he finds the dates he chooses are all palindrome dates. He is very interested in this kind of special dates.
We define a date YYYYMMDD
(Y
, M
, D
are digits for year, month, and date) to be a palindrome date if it is palindrome, that is, its digits remain the same after being reversed. For example, we can write Apr. 4th, 2022 as 20220404
and write Dec. 30th, 321 as 03211230
(We also zero-fill the year to make it exactly digits). The date 03211230
is a palindrome date.
However, Komaeda has a special calendar, in which there are a total of months in a year, and the -th month will have days. Luckily, there is no leap year(闰年) in his calendar, so there is no need to calculate whether a year is a leap year.
Nagito Komaeda wants to know the total number of palindrome dates in a certain time interval. He is so lucky that he can randomly guess out the correct answer, but you cannot. You want to write a program to calculate the number of palindrome dates.
Input Format:
The input contains 4 lines.
In the first line is a number , indicating the number of months each year in the special calendar.
In the second line are numbers separated by spaces. The -th number indicates that the -th month has days.
In the next two lines, you will get two dates and respectively, representing the start date and the end date, in the form of YYYYMMDD
.
It is guaranteed that:
- .
- .
Output Format:
You only need to output one integer, the number of palindrome dates in the range (both ends inclusive).
Sample Input:
1 | 12 |
Sample Output:
1 | 2 |
Sample Explanation:
There are two palindrome dates between the two dates: and .
(In this sample, the calendar is exactly "our" calendar!)
Test cases
Chiaki Nanami Wants to Play Games
"If you want to believe in someone...you need to overcome doubt first. Belief without doubt...is simply a lie."
Description:
Chiaki Nanami, the Ultimate Gamer, now wants to play games. She has a lot of games in her game console and wants to play some of them. Nanami has marked every game with its game type (like FPS or Rogue-like), represented by an integer. If two game types are marked by the same integer, the two games have the same type and vice versa. Nanami also records the price of each game.
The Ultimate Game Player, Nanami Chiaki, also has her favorite type of games. However, she forgot which type of games is her favorite. She only knows that She loves those games so much that the money she spent on her favorite type of games is over half of the total cost of all games.
Please help Nanami to find her favorite game type.
Input Format:
You will first recieve a number , which indicates the number of games Nanami has. Then in each of the next lines you will recieve a tuple. The tuple in -th line represents the name, price and type of the -th game.
It's guaranteed that there will be no comma in , the game names. The game types may not be consecutive (i.e. the s may not simply be ).
For input,
Output Format:
You only need to print one integer, Nanami's favorite type of games.
Sample Input:
1 | 3 |
Sample Output:
1 | 1 |
Sample Explanation:
In the sample given, there are games in Nanami's game console. The cost of game type is and the cost of game type is . The cost of game type is over the half of the total cost , so her favorite type is .
Challenge (Golden Strawberry):
This problem can be solved in a clever way! You don't need to record all games' types and costs into arrays, and in fact you only need very few variables! Can you solve the problem by declaring at most 10 variables (without using arrays or pointers)? Submit your solution to the additional problem 6 to win the golden strawberry award!
Hint: Use the property of the favorite games.
Test cases
Gundham Tanaka and the Dark Devas of Destruction
"Pitiful humans...they refuse to lift their heads up for fear of doubting the authenticity of the blue sky..."
Description:
Gundham Tanaka, the Ultimate Breeder, kept a lot of animals, such as hamsters and eagles. He named all his animals respectively (like his four favourite hamsters, "the Four Dark Devas of Destruction"). One day, the "Dark Devas of Destruction" (his hamsters, actually) wanted to play a game with him.
These hamsters form a matrix of rows and columns, but facing to different directions L
(left), R
(right), U
(up), or D
(down). Among them are some special hamsters which Tanaka needs to find out.
At first, a leading hamster will hint where the first special hamster is. Whenever he finds a special hamster, it will also hint the position of the next special hamster to him. All hints are in forms like "The next special hamster is the -th one in front/back/left/right of me".
The hamsters make mistake sometimes. When a hamster gives a position, if it is out of bound , or if the hamster at that position has already been identified as a special hamster previously, Tanaka will stop the game and tell his hamsters that they have made a mistake.
Now Tanaka is playing the game, please help him find all special hamsters.
Input Format:
In the first line, you will receive numbers , separated by spaces, indicating the number of rows and columns of the hamster matrix, and the number of hints given by hamsters.
In the next lines, you will receive a -row -column word matrix L
, R
, U
, D
. represents the direction of the hamster in the -th row and -th column, facing L
(left), R
(right), U
(up), or D
(down).
In the next line, you will receive numbers , indicating the row and column of the leading hamster.
In the next lines, each line you will receive a word Left, Right, Front, Back and an integer , meaning that the hint is "The next hamster is the -th in of me".
Another thing you need to know: Hamsters are not computers, they count from rather than .
It is guaranteed that:
Output Format:
In each line, you should print (r, c)
, the row and column of a special hamster, in their order of being found, until the game ends or the last one is found. If the hamsters made mistake in one hint, you should print Mistake!
in the corresponding line and end the game.
Sample Input:
1 | 2 2 4 |
Sample Output:
1 | (2, 1) |
Sample Explanation:
Their directions:
↑ ←
↓ ↓
The leading hamster is at . It says that the first special hamster is the st hamster in its back, at .
Notice that the hamster at is facing down, and it says the next special hamster is in its left, which actually means to the right (back's left) of the matrix, which is .
The next special hamster is obviously unable to find, so there is a mistake. The game ends, ignoring any more hints from hamsters.
Test cases
Reference solutions
Remember, you should always solve all the problems yourself and passed all the testcases first.