OpenKattis
Baylor University

# Animal Classification

Alice and Bob are biologists. They like to group animals using classification trees. A classification tree is a binary tree where the leaves are labeled by the animals.

For example, Figure 1 below shows the classification of $5$ animals (labeled as $1, 2, 3, 4, 5$) by Alice and Bob. Alice’s classification has nine groups: $\{ 1\}$, $\{ 2\}$, $\{ 3\}$, $\{ 4\}$, $\{ 5\}$, $\{ 2, 5\}$, $\{ 1, 2, 5\}$, $\{ 1, 2, 3, 5\}$ and $\{ 1, 2, 3, 4, 5\}$. Bob’s classification also has nine groups: $\{ 1\}$, $\{ 2\}$, $\{ 3\}$, $\{ 4\}$, $\{ 5\}$, $\{ 1, 5\}$, $\{ 2, 3\}$, $\{ 1, 2, 3, 5\}$ and $\{ 1, 2, 3, 4, 5\}$.

As you can observe, Alice and Bob classify the animals differently. Carol is interested to know the number of common groups between the classifications of Alice and Bob. For the above example, there are $7$ common groups. They are: $\{ 1\}$, $\{ 2\}$, $\{ 3\}$, $\{ 4\}$, $\{ 5\}$, $\{ 1, 2, 3, 5\}$ and $\{ 1, 2, 3, 4, 5\}$.

Carol usually counts the number of common goups by hand. On one day, an outer space alien came to our earth. He asked Alice and Bob to classify aliens in outer space. The number $N$ of aliens can be as big as $100\, 000$. It is impossible to count the number of common goups by hand now. Can you make a program to help Carol?

## Input

The input consists of three lines:

• one line with one integer $N$ ($1 \leq N \leq 100\, 000$)

• the classification tree of Alice

• the classification tree of Bob

Note that both classification trees are leaf-labeled by $1$ to $N$. The tree is represented by the parathesis encoding. (Note that the encoding only have numbers and $3$ symbols, that is, ‘(’, ‘)’, ‘,’.) For the example in Figure 1, see Sample Input 1.

## Output

Output one line with a single integer that represents the number of common groups between Alice and Bob’s classification trees.

Sample Input 1 Sample Output 1
5
((3,(1,(5,2))),4)
(((5,1),(2,3)),4)

7

Sample Input 2 Sample Output 2
10
(1,(2,(3,(4,(5,(6,(7,(8,(9,10)))))))))
(((((((((1,2),3),4),5),6),7),8),9),10)

11

Sample Input 3 Sample Output 3
10
(10,(9,(8,(7,(6,(5,(4,(3,(2,1)))))))))
(((((((((1,2),3),4),5),6),7),8),9),10)

19
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 1, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.6349886059761047, "perplexity": 869.4497061181231}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2022-05/segments/1642320302622.39/warc/CC-MAIN-20220120190514-20220120220514-00324.warc.gz"}