括号匹配问题
1 #include <iostream>
2 #include <string>
3 #include <stack>
4
5 using namespace std;
6
7 int Comp(char a, char b)
8 {
9 if ((a == '[' && b == ']') || (a == '(' && b == ')'))
10 {
11 return 1;
12 }
13 else
14 {
15 return 0;
16 }
17 }
18
19 int main()
20 {
21 string st;
22 stack<char> stac;
23 cin>>st;
24
25 for (int i = 0; i < (int)st.size(); ++i)
26 {
27 if (stac.empty())
28 {
29 stac.push(st[i]);
30 }
31 else if (!Comp(stac.top(),st[i]))
32 {
33 stac.push(st[i]);
34 }
35 else
36 {
37 stac.pop();
38 }
39 }
40
41 if (stac.empty())
42 {
43 cout<<"YES";
44 }
45 else
46 {
47 cout<<"NO";
48 }
49
50 return 0;
51 }