UVA673



1
2
3
4
Difficult: Easy
Algorithm: Nil
Space: Best:O(n) Worst:O(n)
Time: O(n)



基本的堆疊模擬題,看了大神的寫法後,才了解自己有多愚鈍 (m )n

寫了一堆判斷式整個程式變得非常不簡潔,實際上只要用一個index一個陣列,就能很簡單的處理好

不過出乎意料之外的快,榜上竟然還有5百多,出乎意料的詭異…



Sample Input

1
2
3
4
5
6
7
8
7
([])
(([()])))
([()[]()])()
)
()
)(
([)]


Sample Output

1
2
3
4
5
6
7
Yes
No
Yes
No
Yes
No
No



Code section

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <vector>
#include <stdio.h>
#include <string.h>
using namespace std;
int main() {
// your code goes here
char data[135];
int n;
vector<char> vc;
scanf("%d", &n);
getchar();
while (n--)
{
memset(data, 0, sizeof(data));
fgets(data, sizeof(data), stdin);
int i, ans = 1;
for (i = 0; i < sizeof(data) && data[i] != ' '; ++i)
{
if (data[i] == '(' || data[i] == '[')
{
vc.push_back(data[i]);
}
else if (data[i] == ')' || data[i] == ']')
{
//ex:)
if (vc.size() == 0)
{
ans = 0;
break;
}
//ex:([)]
else if (data[i] == ')')
{
if (vc.at(vc.size() - 1) != '(')
{
ans = 0;
break;
}
}
else if (data[i] == ']')
{
if (vc.at(vc.size() - 1) != '[')
{
ans = 0;
break;
}
}
vc.pop_back();
}
else if (data[++i] == ' ' && vc.size() == 0)
{
ans = 0;
break;
}
}
if (ans == 0 || vc.size() != 0)
printf("No\n");
else
printf("Yes\n");
vc.clear();
}
return 0;
}