Nyoj 2 bracket matching (stack)

Source: Internet
Author: User

Http://acm.nyist.net/JudgeOnline/problem.php? PID = 2

 

1. MyCode:

 
# Include <iostream> # include <cstring> using namespace STD; # define maxsize 10005int main (void) {int K; CIN> K; while (k --) {char STR [maxsize]; CIN> STR; char stack [maxsize]; int Len = strlen (STR); Int J = 0; bool tag = true; for (INT I = 0; I <Len; I ++) {Switch (STR [I]) {Case '(': Case '[': stack [J ++] = STR [I]; break; Case ')': If (stack [J-1] = '(') {tag = true; J --;} else tag = false; break; Case ']': If (Stack [J-1] = '[') {tag = true; j --;} else tag = false; break; default: cout <"error! "<Endl;} If (TAG = false) // notebreak;} If (TAG = true & J = 0) cout <"yes" <Endl; elsecout <"no" <Endl;} return 0 ;}

 

2. Optimal code for time, memory, and length:

# Include <stdio. h> int main () {int t; char C, S [10001], * P; scanf ("% d \ n", & T); While (t --) {* s = getchar (); P = S + 1; while (C = getchar ())! = '\ N') {If (* (p-1) = C-1 | * (p-1) = C-2) p --; else * P ++ = C ;} if (P = s) printf ("Yes \ n"); elseprintf ("NO \ n ");}}

3. Bidding Process:

 
# Include <iostream> # include <vector> # include <string> using namespace STD; int main () {int N; CIN> N; while (n --) {vector <char> VEC; string ch; Vec. push_back (''); CIN> CH; For (INT I = 0; I <ch. length (); I ++) {Vec. push_back (CH [I]); If (VEC. back ()-1 = * (VEC. end ()-2) | Vec. back ()-2 = * (VEC. end ()-2) {Vec. pop_back (); Vec. pop_back () ;}} if (VEC. size () = 1) cout <"yes" <Endl; elsecout <"no" <Endl;} return 0 ;}

 

 

(I always use the stack code to submit wa. I don't know why, see

Http://blog.csdn.net/justme0/article/details/7415337)

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.