Nanyang OJ-2 Bracket Pairing (data structure-stack application)

Source: Internet
Author: User

Brace pairing problem

Time limit:MS | Memory limit:65535 KB Difficulty:3
Describe
now, with a sequence of parentheses, you should check that the line brackets are paired.
Input
the first line enters a number n (0<n<=100), which indicates that there are n sets of test data. The next n lines enter multiple sets of input data, each set of input data is a string s (S is less than 10000, and S is not an empty string), and the number of test data groups is less than 5 groups. Data guarantee S contains only "[", "]", "(", ")" Four characters
Output
The
output of each set of input data is one row, if the parentheses contained in the string are paired, the output is yes, and if you do not pair the output no
Sample input
3[(]) (]) ([[[] ()])
Sample output
Nonoyes
This test instructions examines the usage of the data structure-stack.
#include <iostream> #include <stack>using namespace Std;int main () {  int n, m;  CIN >> N;  while (n--) {    char str[10005];    stack<char>s;//defines a stack that stores character type data.     cin >> str;    for (int i = 0; Str[i]! = ' + '; i++) {      if (S.empty ()) S.push (Str[i]);//If the stack is empty, the data is directly in the stack.       else {        if (s.top () + 1 = = Str[i] | | s.top () + 2 = = Str[i])//According to the brackets ASC code difference to determine whether to correspond           to S.pop ();//If str[i] corresponds to the bracket at the top of the stack, the top Element out of the stack.         else//otherwise str[i] into the stack.           S.push (Str[i]);      }    }    if (S.empty ())//After the string is processed, determine if the stack is empty.       cout << "Yes" << endl;//stack null means that all parentheses correspond.     else//Otherwise, the parentheses do not correspond.       cout << "No"  << Endl;  }}        

If there is a description of the error, please indicate ha!!! Together to explore progress together!!!

Nanyang OJ-2 Bracket Pairing (data structure-stack application)

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.