p538.ans1
======================
3

=================
p538.ans2
======================
2

=================
p538.cpp
======================
#include <bits/stdc++.h>

using namespace std;

template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {
        in >> x;
    }
    return in;
};

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

string s;

void read() { getline(cin, s); }

void solve() {
    // A bracket pair is a "real" pair (not part of an emoticon) when an '(' has
    // its nearest bracket to the right being a ')', that ')' has this '(' as its
    // nearest bracket to the left, and everything between them is only spaces
    // and Latin letters (and the pair is not the empty "()"). All other
    // brackets are emoticon parts, which is what we count.
    //
    // prev_bracket[i] / next_bracket[i] give, for position i, the index of the
    // nearest '(' to the left / ')' to the right (or itself if i is that
    // bracket). cnt_non_letters is a prefix sum of characters that are neither
    // letters, spaces, nor brackets, so get_non_letters(l, r) tests emptiness
    // of the inside. We start by counting every bracket, then for each matched,
    // clean pair we subtract its two brackets back out.

    int n = s.size();
    vector<int> prev_bracket(n, -1);
    vector<int> next_bracket(n, -1);
    vector<int> cnt_non_letters(n, 0);
    for(int i = 0; i < n; i++) {
        if(s[i] == '(') {
            prev_bracket[i] = i;
        } else {
            prev_bracket[i] = i ? prev_bracket[i - 1] : -1;
        }

        if(!isalpha(s[i]) && s[i] != ' ' && s[i] != '(' && s[i] != ')') {
            cnt_non_letters[i]++;
        }
        cnt_non_letters[i] += i ? cnt_non_letters[i - 1] : 0;
    }

    auto get_non_letters = [&](int l, int r) {
        return cnt_non_letters[r] - (l ? cnt_non_letters[l - 1] : 0);
    };

    for(int i = n - 1; i >= 0; i--) {
        if(s[i] == ')') {
            next_bracket[i] = i;
        } else {
            next_bracket[i] = i < n - 1 ? next_bracket[i + 1] : -1;
        }
    }

    int ans = 0;
    for(int i = 0; i < n; i++) {
        if(s[i] == '(' || s[i] == ')') {
            ans++;
        }

        if(s[i] == '(' && next_bracket[i] != -1 &&
           prev_bracket[next_bracket[i]] == i &&
           get_non_letters(i, next_bracket[i]) == 0) {
            ans--;
        }

        if(s[i] == ')' && prev_bracket[i] != -1 &&
           next_bracket[prev_bracket[i]] == i &&
           get_non_letters(prev_bracket[i], i) == 0) {
            ans--;
        }
    }

    cout << ans << '\n';
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int T = 1;
    // cin >> T;
    for(int test = 1; test <= T; test++) {
        read();
        // cout << "Case #" << test << ": ";
        solve();
    }

    return 0;
}

=================
p538.in1
======================
Hi:) (it is me) I have bad news:-((

=================
p538.in2
======================
((two plus two equals four))

=================
statement.txt
======================
538. Emoticons
Time limit per test: 1 second(s)
Memory limit: 262144 kilobytes
input: standard
output: standard



A berland national nanochat Bertalk should always stay up-to-date. That's why emoticons highlighting was decided to be introduced. As making emoticons to be highlighted is not exactly the kind of task one performs everyday but this task had to be done as soon as possible, the following simple rule was decided to be introduced: a round opening or closing bracket  be considered part of an emoticon if:
this is an opening bracket and there exists the nearest bracket following to the right. The nearest round bracket to the right should be a closing bracket and there shouldn't be anything between the brackets but spaces and Latin letters,
or else it can be a closing bracket and there exists the nearest round bracket following to the left. The nearest round bracket to the left should be an opening bracket. Besides, there shouldn't be anything between the brackets but spaces and Latin letters.


If a bracket doesn't satisfy the conditions, it is considered a part of an emoticon. For example, let's consider the string "Hi:) (it is me) I have bad news:-((". In the string only the brackets that outline "it is me" aren't emoticons. Note that an opening bracket immediatelly followed by a closing bracket, i.e. "()", are not parts of emoticons by definition.

Your task is to print the number of brackets that are parts of emoticons in the given string.

Input
The input data consist of a single non-empty string. The length of the string does not exceed 105 characters. The string consists of lowercase and uppercase Latin letters, spaces, round brackets and punctuation marks: "-", ":", ",", ";". The string does not begin with and does not end with a space.

Output
Print a single number — the required number of brackets that are part of emoticons.

Example(s)
sample input
sample output
Hi:) (it is me) I have bad news:-((
3 

sample input
sample output
((two plus two equals four)) 
2 

=================
