# User:Hakerh400/Code golf challenges

Given that User:Hakerh400 will never create an account on the PPCG website (due to disagreement with the StackExchange's privacy policy and terms of service), he decided to write all challenge ideas here. If someone has a PPCG account and finds some of these ideas interesting, feel free to post on the PPCG website. All challenges that are published here are in the public domain.

## Browsing articles

Difficulty: Easy

### Overview

You are reading some articles on a website on the internet. Each article on that website is on a separate page. Pages are numbered starting from ${\displaystyle 1}$. The article on the page with number ${\displaystyle 1}$ is the last article that is published. The article on the page with number ${\displaystyle 2}$ is the previous article, and so on. However, when a new article is published, all previous articles get shifted by a page (so the last published article will be on page ${\displaystyle 1}$, the article which was on page ${\displaystyle 1}$ will be moved to page ${\displaystyle 2}$, and so on).

On each page there is a button next which goes to the next page. If you opened page ${\displaystyle n}$, and while you are reading the article from that page another article is published, when you click next, you will navigate to the ${\displaystyle n+1}$ th page, but it will contain the same article that you have just read. If two articles were published, you would see the article that was ${\displaystyle n-1}$ th while you was reading the ${\displaystyle n}$th article.

You start from the first article (the article on the first page, we call it ${\displaystyle \Omega }$). After you read it, you click next. The second page will be opened. You have no idea how many articles are published in the meantime, so you may be reading the article that is chronologically published before ${\displaystyle \Omega }$, or after ${\displaystyle \Omega }$, or it could even be the same article (in that case you would know that exactly one article is published in the meantime). After you read the article from the second page, you again click next and you go to the third page. Again, any number of articles may be published in the meantime, and so on. The last article that you read is ${\displaystyle \mathrm {A} }$. The only thing you know is that ${\displaystyle \Omega }$ is not chronologically published before ${\displaystyle \mathrm {A} }$ (so either ${\displaystyle \Omega }$ is ${\displaystyle \mathrm {A} }$, or ${\displaystyle \Omega }$ is published after ${\displaystyle \mathrm {A} }$).

### Goal

Your task is to determine how many articles there are between ${\displaystyle \mathrm {A} }$ and ${\displaystyle \Omega }$ (including them).

### Input

The input contains the array of the titles of all articles that you have read so far (each article has a unique title). The first article from the array corresponds to ${\displaystyle \Omega }$ and the last article corresponds to ${\displaystyle \mathrm {A} }$.

### Output

The output is the number of all articles that are published after ${\displaystyle \mathrm {A} }$, but before ${\displaystyle \Omega }$ (plus ${\displaystyle 2}$ because we count ${\displaystyle \mathrm {A} }$ and ${\displaystyle \Omega }$ too, or plus ${\displaystyle 1}$ if ${\displaystyle \mathrm {A} =\Omega }$).

### Examples

Input: A
Output: 1

Input: A A
Output: 1

Input: A B
Output: 2

Input: A A A A B C A D
Output: 2

Input: A B C D E F G A H A H I J K
Output: 5

Input: A B C C C D A A B E E A B B C B C C B C D F F D C C D F G
Output: 6

Input: A A B C C C A B C C D D E F E D G H A A B C I I J B C C C I J I J J K L L
Output: 7

Input: A B A B B C D D D B C D A A E A E A A A A A B B B E F G H F G E F I J K I I H H F H K I I I H F F G E A B C A A B B C C D D A B B G E A B B B B E G E G K L L L L M M N O L L L L J K K I H F F G E E E A B C D
Output: 4

Output: 4


### Scoring

This is code-golf, so the shortest answer in bytes wins.

## Dictionary

Difficulty: Easy

### Overview

A dictionary is defined by an array of key-value pairs. Both keys and values are words consisting of lowercase english letters. Each line in a dictionary contains two words separated by a space (the left one is key and the right on is value). Same key can appear multiple times and in that case the last value for that key is the actual value. Example of a dictionary:

abc xy
defgh pqrst
asdf fd
abc z


As we can see, abc is redefined, so it has value z, because it is the last value that is defined for that key. However, it is important to keep track of two things for each key:

• The first time when the key is added to the dictionary
• The last time when a key is updated

Updating with the same value also counts as updating.

Based on the above metrics, we define two lists of keys for each dictionary (both contain the same number of keys):

• keys1 - The list of keys sorted by the ascending time when the key is added to the dictionary for the first time
• keys2 - The list of keys sorted by the ascending time when the key is last updated

In the example above, keys1 is [abc, defgh, asdf] and keys2 is [defgh, asdf, abc].

### Reduced dictionary

We display a dictionary in the reduced form by first listing all key-value pairs ordered by keys1 and then we list keys2 in a new line (keys separated by spaces). The reduced dictionary from the example above would be:

abc z
defgh pqrst
asdf fd
defgh asdf abc


The first three lines represent key-value pairs (abc is redefined to be z instead of xy) and the last line contains keys2.

### Product of two dictionaries

The syntax ${\displaystyle P[k]}$ represents the value of key ${\displaystyle k}$ from dictionary ${\displaystyle P}$.

For two dictionaries ${\displaystyle A}$ and ${\displaystyle B}$, we define their product ${\displaystyle C=A\times B}$ in the following way:

• ${\displaystyle C}$ is initially empty
• All keys that are in ${\displaystyle A}$, but not in ${\displaystyle B}$, we add them to ${\displaystyle B}$ and for each such key in ${\displaystyle B}$ we set the value to be the key itself
• All values that are in ${\displaystyle A}$, but not as keys in ${\displaystyle B}$, we add them as keys to ${\displaystyle B}$ and for each such key in ${\displaystyle B}$ we set the value to be the key itself
• For each element keys of [A.keys1, A.keys2] (in that order)
• For each key ${\displaystyle K_{1}}$ of keys
• Let ${\displaystyle K_{2}=B[K_{1}]}$
• Let ${\displaystyle S=\left\{x\mid B[x]=K_{2}\right\}}$
• Let ${\displaystyle K_{3}}$ be the last element of A.keys2 that is in ${\displaystyle S}$
• Set ${\displaystyle C[K_{2}]=B\left[A[K_{3}]\right]}$

### Goal

Your task is to calculate the product of two given dictionaries and output it in the reduced form. Bonus points if you can solve it in ${\displaystyle O(n\log n)}$ time complexity.

### Input

Two reduced dictionaries ${\displaystyle A}$ and ${\displaystyle B}$. Use any input format you like.

### Output

Reduced dictionary ${\displaystyle A\times B}$. Use any output format you like.

### Examples

=== Example 1 ===

Input:
p P
x A
q Q
y B
r R
q y r x p

x y
x

Output:
p P
y A
q Q
r R
q r y p

=== Example 2 ===

Input:
p P
x A
q Q
y B
r R
q y r x p

y x
y

Output:
p P
x A
q Q
r R
q r x p

=== Example 3 ===

Input:
p P
x A
q Q
y B
r R
q y r x p

x y
y x
A B
B C
x y A B

Output:
p P
y B
q Q
x C
r R
q x r y p


### Scoring

This is code-golf, so the shortest answer in bytes wins.