forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbalanced_smileys.cpp
More file actions
71 lines (62 loc) · 1.55 KB
/
balanced_smileys.cpp
File metadata and controls
71 lines (62 loc) · 1.55 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
#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];
int dp(int st, int ed) {
int& ret = mm[st][ed];
if (ret != -1) return ret;
if (st > ed) return ret = 1;
FOR(i,st,ed) {
if (str[i] == '(') {
if (i - 1 >= st && str[i - 1] == ':') {
if (dp(i + 1, ed) == 1) return ret = 1;
}
FOR(j,i+1,ed) {
if (str[j] == ')') {
if (dp(i + 1, j - 1) == 1 && dp(j + 1, ed) == 1) return ret = 1;
}
}
return ret = 0;
} else if (str[i] == ')') {
if (i - 1 >= st && str[i - 1] == ':') {
return ret = dp(i + 1, ed);
}
return ret = 0;
}
}
return ret = 1;
}
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;
}
memset(mm, -1, sizeof(mm));
int ret = dp(0, len - 1);
return ret == 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;
}