Files
PAT/source/html/a1130.html
T

51 lines
3.6 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
Infix Expression
<div class="ques-view"><p>Given a syntax tree (binary), you are supposed to output the corresponding infix expression, with parentheses reflecting the precedences of the operators.</p>
<h3 id="input-specification-">Input Specification:</h3>
<p>Each input file contains one test case. For each case, the first line gives a positive integer N (<span class="katex"><span class="katex-mathml"><math><mrow><mo>≤</mo></mrow>\le</math></span><span aria-hidden="true" class="katex-html"><span class="strut" style="height:0.63597em;"></span><span class="strut bottom" style="height:0.7719400000000001em;vertical-align:-0.13597em;"></span><span class="base textstyle uncramped"><span class="mrel">≤</span></span></span></span> 20) which is the total number of nodes in the syntax tree. Then N lines follow, each gives the information of a node (the <span class="katex"><span class="katex-mathml"><math><mrow><mi>i</mi></mrow>i</math></span><span aria-hidden="true" class="katex-html"><span class="strut" style="height:0.65952em;"></span><span class="strut bottom" style="height:0.65952em;vertical-align:0em;"></span><span class="base textstyle uncramped"><span class="mord mathit">i</span></span></span></span>-th line corresponds to the <span class="katex"><span class="katex-mathml"><math><mrow><mi>i</mi></mrow>i</math></span><span aria-hidden="true" class="katex-html"><span class="strut" style="height:0.65952em;"></span><span class="strut bottom" style="height:0.65952em;vertical-align:0em;"></span><span class="base textstyle uncramped"><span class="mord mathit">i</span></span></span></span>-th node) in the format:</p>
<pre><code>data left_child right_child
</code></pre><p>where <code>data</code> is a string of no more than 10 characters, <code>left_child</code> and <code>right_child</code> are the indices of this node's left and right children, respectively. The nodes are indexed from 1 to N. The NULL link is represented by <span class="katex"><span class="katex-mathml"><math><mrow><mo>−</mo><mn>1</mn></mrow>-1</math></span><span aria-hidden="true" class="katex-html"><span class="strut" style="height:0.64444em;"></span><span class="strut bottom" style="height:0.72777em;vertical-align:-0.08333em;"></span><span class="base textstyle uncramped"><span class="mord">−</span><span class="mord mathrm">1</span></span></span></span>. The figures 1 and 2 correspond to the samples 1 and 2, respectively.</p>
<table>
<thead>
<tr>
<th style="text-align:center"><img alt="infix1.JPG" src="https://images.ptausercontent.com/4d1c4a98-33cc-45ff-820f-c548845681ba.JPG"/></th>
<th style="text-align:center"><img alt="infix2.JPG" src="https://images.ptausercontent.com/b5a3c36e-91ad-494a-8853-b46e1e8b60cc.JPG"/></th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">Figure 1</td>
<td style="text-align:center">Figure 2</td>
</tr>
</tbody>
</table>
<h3 id="output-specification-">Output Specification:</h3>
<p>For each case, print in a line the infix expression, with parentheses reflecting the precedences of the operators. Note that there must be no extra parentheses for the final expression, as is shown by the samples. There must be no space between any symbols.</p>
<h3 id="sample-input-1-">Sample Input 1:</h3>
<pre><code class="lang-in">8
* 8 7
a -1 -1
* 4 1
+ 2 5
b -1 -1
d -1 -1
- -1 6
c -1 -1
</code></pre>
<h3 id="sample-output-1-">Sample Output 1:</h3>
<pre><code class="lang-out">(a+b)*(c*(-d))
</code></pre>
<h3 id="sample-input-2-">Sample Input 2:</h3>
<pre><code class="lang-in">8
2.35 -1 -1
* 6 1
- -1 4
% 7 8
+ 2 3
a -1 -1
str -1 -1
871 -1 -1
</code></pre>
<h3 id="sample-output-2-">Sample Output 2:</h3>
<pre><code class="lang-out">(a*2.35)+(-(str%871))
</code></pre>
</div>