Skip to content

Instantly share code, notes, and snippets.

@msg555
Created January 29, 2013 00:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msg555/4660592 to your computer and use it in GitHub Desktop.
Save msg555/4660592 to your computer and use it in GitHub Desktop.
Balanced Smileys done simply in O(N)
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <map>
#include <set>
#include <queue>
using namespace std;
int main() {
int N; cin >> N;
string ln;
getline(cin, ln);
for(int i = 0; i < N; i++) {
getline(cin, ln);
cout << "Case #" << (i + 1) << ": ";
int lo = 0, hi = 0;
for(int i = 0; lo <= hi && i < ln.size(); i++) {
if(ln[i] == '(') {
lo++; hi++;
} else if(ln[i] == ')') {
lo--; hi--;
lo = max(lo, 0);
} else if(i + 1 < ln.size() && ln[i] == ':' && ln[i + 1] == '(') {
i++;
hi++;
} else if(i + 1 < ln.size() && ln[i] == ':' && ln[i + 1] == ')') {
i++;
lo--;
lo = max(lo, 0);
}
}
cout << (lo == 0 && hi >= 0 ? "YES" : "NO") << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment