forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbalanced_smileys_dp.cpp
More file actions
84 lines (78 loc) · 2.09 KB
/
balanced_smileys_dp.cpp
File metadata and controls
84 lines (78 loc) · 2.09 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
string str;
int len;
int mm[105][105];
bool isvalid(char ch) {
if ((ch >= 'a' && ch <= 'z') || ch == ' ' || ch == ':' || ch == '(' || ch == ')') return true;
return false;
}
bool run() {
REP(i,len) {
if (!isvalid(str[i])) return false;
}
REP(i,105) {
REP(j,105) {
mm[i][j] = 1;
}
}
REP(i,len) {
if (str[i] == '(' || str[i] == ')') mm[i][i] = 0;
}
FOR(step,2,len) {
FOR(st,0,len-1) {
int ed = st + step - 1;
if (ed >= len) break;
bool fl = true;
FOR(i,st,ed) {
if (str[i] == '(') {
fl = false;
if (i - 1 >= st && str[i - 1] == ':') {
if (mm[i + 1][ed] == 1) {
fl = true;
break;
}
}
FOR(j,i+1,ed) {
if (str[j] == ')') {
if (mm[i + 1][j - 1] == 1 && mm[j + 1][ed] == 1) {
fl = true;
break;
}
}
}
break;
} else if (str[i] == ')') {
fl = false;
if (i - 1 >= st && str[i - 1] == ':') {
if (mm[i + 1][ed] == 1) fl = true;
}
break;
}
}
if (!fl) mm[st][ed] = 0;
}
}
return mm[0][len - 1] == 1;
}
int main() {
int m;
cin >> m;
getline(cin, str);
FOR(i,1,m) {
cout << "Case #" << i << ": ";
getline(cin, str);
len = str.length();
if (str.empty()) {
cout << "YES" << endl;
continue;
}
if (run()) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
}