ACM online exercise-Parentheses matching

Source: Internet
Author: User

Description of the parentheses matching problem currently, there is a sequence of parentheses. Please check whether this line of parentheses is paired. Enter N (0 <N <= 100) In the first line, indicating that N groups of test data exist. The next N rows Input Multiple groups of input data. Each group of input data is a string of S (the length of S is less than 10000, and S is not an empty string), and the number of test data groups is less than five. Data guarantee S only contains "[", "]", "(", ")" and outputs each group of input data to occupy one row, if the parentheses contained in the string are paired, Yes is output. If not paired, No is output. Input 3 [(]) ([[] ()]) sample output NoNoYes my program, using the language c ++. Idea: Import unmatched characters into the stack. If no matching character exists, the system outputs the stack. Check whether the stack is empty. The source code has been verified by the online system. SyntaxMatch. cpp [cpp] # include <iostream> # include <vector> using namespace std; int main (void) {int nLineNum = 0; cin> nLineNum; if (nLineNum <0 | nLineNum> 100) {cout <"Please input number 0 <n <= 100. "<endl; return 1 ;}vector <string> vInputList; for (int I = 0; I <nLineNum; ++ I) {string sInputStr = ""; cin> sInputStr; if (sInputStr. size () = 0) {cout <"Error: Input string is empty. "<endl; -- I; continue ;} If (sInputStr. size () & gt; = 10000) {cout <"Error: Input string is too long. "<endl; -- I; continue;} vInputList. push_back (sInputStr);} vector <string >:: iterator vIter = vInputList. begin (); for (; vIter! = VInputList. end (); ++ vIter) {string sUnMatch = ""; string sInput = * vIter; string: iterator sIter = sInput. begin (); for (; sIter! = SInput. end (); ++ sIter) {char cCurChar = * sIter; if (sUnMatch. empty () {sUnMatch. push_back (cCurChar);} else {string: iterator sEndIter = sUnMatch. end ()-1; char cEndChar = * sEndIter; if (cEndChar = '[' & cCurChar = ']') | (cEndChar = '(' & cCurChar = ') {sUnMatch. erase (sEndIter);} else {sUnMatch. push_back (cCurChar) ;}} if (sUnMatch. empty () {cout <"Yes" <endl ;}else {cout <"No" <endl ;}} return 1 ;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.