http://www.tsinsen.com/A1112
#include "bits/stdc++.h"
using namespace std;
char s[55];
int idx, size, Succ, L;
int GetOption();
int GetVal() {
while (s[idx] == '(' && idx < size) ++idx;
if (idx >= size) return 0;
if (s[idx] == 'a' || s[idx] == 'b' || s[idx] == 'c') {
idx = idx + 1;
while (s[idx] == '(' && idx < size) ++idx;
if (idx >= size) return 1;
else return GetOption();
}
if (s[idx] == '+' || s[idx] == '-' || s[idx] == '*' || s[idx] == '/') {
puts("ERROR 3"); Succ = 0; return 0;
}
puts("ERROR 1"); Succ = 0; return 0;
}
int GetOption() {
while (s[idx] == '(' && idx < size) ++idx;
if (idx >= size) return 0;
if (s[idx] == '+' || s[idx] == '-' || s[idx] == '*' || s[idx] == '/') {
idx = idx + 1; return GetVal();
}
if (s[idx] == 'a' || s[idx] == 'b' || s[idx] == 'c') {
puts("ERROR 3"); Succ = 0; return 0;
}
puts("ERROR 1"); Succ = 0; return 0;
}
int main() {
scanf("%s", s);
size = strlen(s);
s[--size] = '\0';
Succ = 1;
for (int i = 0; i < size; ++i) {
if (s[i] == '(') ++L;
if (s[i] == ')') {
Succ &= (L-- > 0);
s[i] = '(';
}
}
if (L != 0 || !Succ) puts("ERROR 2");
else if (GetVal()) puts("OK");
else if (Succ) puts("ERROR 3");
return 0;
}