括号配对问题
Time Limit:3000MS Memory Limit:65536K
Total Submit:90 Accepted:57
Description
现在,有一行括号序列,请你检查这行括号是否配对。
Input
第一行输入一个数N(0
Output
每组输入数据的输出占一行,如果该字符串中所含的括号是配对的,则输出Yes,如果不配对则输出No
Sample Input
3
[(])
(])
([[]()])
Sample Output
No
No
Yes
Hint
数据结构经典
Source
数据结构经典
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AK1029 {
class Program {
static void Main(string[] args) {
int n = int.Parse(Console.ReadLine());
char[] stack = new char[10005];
while (n-- > 0) {
string s = Console.ReadLine();
int i, l = s.Length, j = 0, flag = 1;
for (i = 0; i < l; i++) {
if (s[i] == '(' || s[i] == '[')
stack[j++] = s[i];
if (s[i] == ')') {
if (stack[j - 1] == '(') {
j--;
flag = 1;
} else
flag = 0;
}
if (s[i] == ']') {
if (stack[j - 1] == '[') {
j--;
flag = 1;
} else
flag = 0;
}
if (flag == 0) break;
}
if (flag == 1 && j == 0)
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
}
}