描述
在某个字符串(长度不超过100)中有左括号、右括号和大小写字母;规定(与常见的算数式子一样)任何一个左括号都从内到外与在它右边且距离最近的右括号匹配。写一个程序,找到无法匹配的左括号和右括号,输出原来字符串,并在下一行标出不能匹配的括号。不能匹配的左括号用"$"标注,不能匹配的右括号用"?"标注.
输入
输入包括多组数据,每组数据一行,包含一个字符串,只包含左右括号和大小写字母,
字符串长度不超过100
注意:cin.getline(str,100)最多只能输入99个字符!
输出
对每组输出数据,输出两行,第一行包含原始输入字符,第二行由"$","?"和空格组成,"$"和"?"表示与之对应的左括号和右括号不能匹配。
样例输入
样例输出
注意:cin.getline(str,100)最多只能输入99个字符!
((ABCD(x) )(rttyy())sss)(
((ABCD(x) $$ )(rttyy())sss)( ? ?$
</pre><pre class="sh_cpp sh_sourceCode" style="margin-top: 0px; margin-bottom: 0px; padding: 11px; border: 1px solid rgb(218, 218, 218); font-size: 14px; line-height: 1.3em; overflow: auto; font-family: 'Courier New'; background: white;"><span class="sh_preproc" style="color: rgb(0, 0, 139); font-weight: bold;">#include</span><span class="sh_string" style="color: red; font-family: monospace;"><stdio.h></span> <span class="sh_preproc" style="color: rgb(0, 0, 139); font-weight: bold;">#include</span><span class="sh_string" style="color: red; font-family: monospace;"><string.h></span> <span class="sh_type" style="color: rgb(0, 100, 0);">int</span> <span class="sh_function" style="font-weight: bold;">main</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">()</span> <span class="sh_cbracket" style="color: red;">{</span> <span class="sh_type" style="color: rgb(0, 100, 0);">char</span> a<span class="sh_symbol" style="color: rgb(139, 0, 0);">[</span><span class="sh_number" style="color: purple;">101</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">];</span> <span class="sh_type" style="color: rgb(0, 100, 0);">int</span> l<span class="sh_symbol" style="color: rgb(139, 0, 0);">,</span>i<span class="sh_symbol" style="color: rgb(139, 0, 0);">,</span>j<span class="sh_symbol" style="color: rgb(139, 0, 0);">,</span>t<span class="sh_symbol" style="color: rgb(139, 0, 0);">,</span>k<span class="sh_symbol" style="color: rgb(139, 0, 0);">;</span> <span class="sh_keyword" style="color: blue; font-weight: bold;">while</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">(</span><span class="sh_function" style="font-weight: bold;">gets</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">(</span>a<span class="sh_symbol" style="color: rgb(139, 0, 0);">))</span> <span class="sh_cbracket" style="color: red;">{</span> t<span class="sh_symbol" style="color: rgb(139, 0, 0);">=</span><span class="sh_number" style="color: purple;">0</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">;</span> k<span class="sh_symbol" style="color: rgb(139, 0, 0);">=</span><span class="sh_number" style="color: purple;">0</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">;</span> l<span class="sh_symbol" style="color: rgb(139, 0, 0);">=</span><span class="sh_function" style="font-weight: bold;">strlen</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">(</span>a<span class="sh_symbol" style="color: rgb(139, 0, 0);">);</span> <span class="sh_function" style="font-weight: bold;">printf</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">(</span><span class="sh_string" style="color: red; font-family: monospace;">"%s</span><span class="sh_specialchar" style="color: rgb(255, 192, 203); font-family: monospace;">\n</span><span class="sh_string" style="color: red; font-family: monospace;">"</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">,</span>a<span class="sh_symbol" style="color: rgb(139, 0, 0);">);</span> <span class="sh_keyword" style="color: blue; font-weight: bold;">for</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">(</span>i<span class="sh_symbol" style="color: rgb(139, 0, 0);">=</span><span class="sh_number" style="color: purple;">0</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">;</span>i<span class="sh_symbol" style="color: rgb(139, 0, 0);"><</span>l<span class="sh_symbol" style="color: rgb(139, 0, 0);">;</span>i<span class="sh_symbol" style="color: rgb(139, 0, 0);">++)</span> <span class="sh_cbracket" style="color: red;">{</span> <span class="sh_keyword" style="color: blue; font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">(</span>a<span class="sh_symbol" style="color: rgb(139, 0, 0);">[</span><span class="sh_number" style="color: purple;">0</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">]==</span><span class="sh_string" style="color: red; font-family: monospace;">')'</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">)</span> <span class="sh_cbracket" style="color: red;">{</span> a<span class="sh_symbol" style="color: rgb(139, 0, 0);">[</span><span class="sh_number" style="color: purple;">0</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">]=</span><span class="sh_string" style="color: red; font-family: monospace;">'?'</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">;</span> <span class="sh_keyword" style="color: blue; font-weight: bold;">continue</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">;</span> <span class="sh_cbracket" style="color: red;">}</span> <span class="sh_keyword" style="color: blue; font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">(</span>a<span class="sh_symbol" style="color: rgb(139, 0, 0);">[</span>i<span class="sh_symbol" style="color: rgb(139, 0, 0);">]==</span><span class="sh_string" style="color: red; font-family: monospace;">')'</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">)</span> <span class="sh_cbracket" style="color: red;">{</span> k<span class="sh_symbol" style="color: rgb(139, 0, 0);">=</span><span class="sh_number" style="color: purple;">0</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">;</span> <span class="sh_keyword" style="color: blue; font-weight: bold;">for</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">(</span>j<span class="sh_symbol" style="color: rgb(139, 0, 0);">=</span>i<span class="sh_number" style="color: purple;">-1</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">;</span>j<span class="sh_symbol" style="color: rgb(139, 0, 0);">>=</span><span class="sh_number" style="color: purple;">0</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">;</span>j<span class="sh_symbol" style="color: rgb(139, 0, 0);">--)</span> <span class="sh_cbracket" style="color: red;">{</span> <span class="sh_keyword" style="color: blue; font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">(</span>a<span class="sh_symbol" style="color: rgb(139, 0, 0);">[</span>j<span class="sh_symbol" style="color: rgb(139, 0, 0);">]==</span><span class="sh_string" style="color: red; font-family: monospace;">'('</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">)</span> <span class="sh_cbracket" style="color: red;">{</span> a<span class="sh_symbol" style="color: rgb(139, 0, 0);">[</span>j<span class="sh_symbol" style="color: rgb(139, 0, 0);">]=</span>a<span class="sh_symbol" style="color: rgb(139, 0, 0);">[</span>i<span class="sh_symbol" style="color: rgb(139, 0, 0);">]=</span><span class="sh_string" style="color: red; font-family: monospace;">'#'</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">;</span> k<span class="sh_symbol" style="color: rgb(139, 0, 0);">=</span><span class="sh_number" style="color: purple;">1</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">;</span> <span class="sh_keyword" style="color: blue; font-weight: bold;">break</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">;</span> <span class="sh_cbracket" style="color: red;">}</span> <span class="sh_keyword" style="color: blue; font-weight: bold;">else</span> <span class="sh_keyword" style="color: blue; font-weight: bold;">continue</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">;</span> <span class="sh_cbracket" style="color: red;">}</span> <span class="sh_keyword" style="color: blue; font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">(</span>k<span class="sh_symbol" style="color: rgb(139, 0, 0);">==</span><span class="sh_number" style="color: purple;">0</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">)</span> a<span class="sh_symbol" style="color: rgb(139, 0, 0);">[</span>i<span class="sh_symbol" style="color: rgb(139, 0, 0);">]=</span><span class="sh_string" style="color: red; font-family: monospace;">'*'</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">;</span> <span class="sh_cbracket" style="color: red;">}</span> <span class="sh_cbracket" style="color: red;">}</span> <span class="sh_keyword" style="color: blue; font-weight: bold;">for</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">(</span>i<span class="sh_symbol" style="color: rgb(139, 0, 0);">=</span><span class="sh_number" style="color: purple;">0</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">;</span>i<span class="sh_symbol" style="color: rgb(139, 0, 0);"><</span>l<span class="sh_symbol" style="color: rgb(139, 0, 0);">;</span>i<span class="sh_symbol" style="color: rgb(139, 0, 0);">++)</span> <span class="sh_cbracket" style="color: red;">{</span> <span class="sh_keyword" style="color: blue; font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">(</span>a<span class="sh_symbol" style="color: rgb(139, 0, 0);">[</span>i<span class="sh_symbol" style="color: rgb(139, 0, 0);">]==</span><span class="sh_string" style="color: red; font-family: monospace;">'('</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">)</span> a<span class="sh_symbol" style="color: rgb(139, 0, 0);">[</span>i<span class="sh_symbol" style="color: rgb(139, 0, 0);">]=</span><span class="sh_string" style="color: red; font-family: monospace;">'$'</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">;</span> <span class="sh_keyword" style="color: blue; font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">(</span>a<span class="sh_symbol" style="color: rgb(139, 0, 0);">[</span>i<span class="sh_symbol" style="color: rgb(139, 0, 0);">]==</span><span class="sh_string" style="color: red; font-family: monospace;">'*'</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">)</span> a<span class="sh_symbol" style="color: rgb(139, 0, 0);">[</span>i<span class="sh_symbol" style="color: rgb(139, 0, 0);">]=</span><span class="sh_string" style="color: red; font-family: monospace;">'?'</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">;</span> <span class="sh_cbracket" style="color: red;">}</span> <span class="sh_keyword" style="color: blue; font-weight: bold;">for</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">(</span>i<span class="sh_symbol" style="color: rgb(139, 0, 0);">=</span><span class="sh_number" style="color: purple;">0</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">;</span>i<span class="sh_symbol" style="color: rgb(139, 0, 0);"><</span>l<span class="sh_symbol" style="color: rgb(139, 0, 0);">;</span>i<span class="sh_symbol" style="color: rgb(139, 0, 0);">++)</span> <span class="sh_cbracket" style="color: red;">{</span> <span class="sh_keyword" style="color: blue; font-weight: bold;">if</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">(</span>a<span class="sh_symbol" style="color: rgb(139, 0, 0);">[</span>i<span class="sh_symbol" style="color: rgb(139, 0, 0);">]!=</span><span class="sh_string" style="color: red; font-family: monospace;">'$'</span> <span class="sh_symbol" style="color: rgb(139, 0, 0);">&&</span> a<span class="sh_symbol" style="color: rgb(139, 0, 0);">[</span>i<span class="sh_symbol" style="color: rgb(139, 0, 0);">]!=</span><span class="sh_string" style="color: red; font-family: monospace;">'?'</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">)</span> <span class="sh_function" style="font-weight: bold;">printf</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">(</span><span class="sh_string" style="color: red; font-family: monospace;">" "</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">);</span> <span class="sh_keyword" style="color: blue; font-weight: bold;">else</span> <span class="sh_function" style="font-weight: bold;">printf</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">(</span><span class="sh_string" style="color: red; font-family: monospace;">"%c"</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">,</span>a<span class="sh_symbol" style="color: rgb(139, 0, 0);">[</span>i<span class="sh_symbol" style="color: rgb(139, 0, 0);">]);</span> <span class="sh_cbracket" style="color: red;">}</span> <span class="sh_function" style="font-weight: bold;">printf</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">(</span><span class="sh_string" style="color: red; font-family: monospace;">"</span><span class="sh_specialchar" style="color: rgb(255, 192, 203); font-family: monospace;">\n</span><span class="sh_string" style="color: red; font-family: monospace;">"</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">);</span> <span class="sh_cbracket" style="color: red;">}</span> <span class="sh_keyword" style="color: blue; font-weight: bold;">return</span> <span class="sh_number" style="color: purple;">0</span><span class="sh_symbol" style="color: rgb(139, 0, 0);">;</span> <span class="sh_cbracket" style="color: red;">}</span>