Go: package main

import (
	"bufio"
	"fmt"
	"os"
)

var sc = bufio.NewScanner(os.Stdin)

func nextString() string {
	sc.Scan()
	return sc.Text()
}

func main() {
	sc.Buffer(make([]byte, 0), 1000000009)

	for {
		var s string
		s = nextString()
		if s == "." {
			break
		}
		var st []byte
		is_valid := true

		for _, value := range s {
			if value == '(' {
				st = append(st, byte(value))
			}
			if value == '[' {
				st = append(st, byte(value))
			}
			if value == ')' {
				if len(st) == 0 || st[len(st)-1] != '(' {
					is_valid = false
					break
				} else {
					st = st[:len(st)-1]
				}
			}
			if value == ']' {
				if len(st) == 0 || st[len(st)-1] != '[' {
					is_valid = false
					break
				} else {
					st = st[:len(st)-1]
				}
			}
		}
		if len(st) != 0 {
			is_valid = false
		}
		if is_valid {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

C++: #include<bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "../dump.hpp"
#else
#define dump(...)
#endif

#define rep(i,n) for(ll i=0,i##_cond=(n);i<i##_cond;++i)
#define FOR(i,a,b) for(ll i=(a),i##_cond=(b);i<i##_cond;++i)
#define ROF(i,a,b) for(ll i=(a)-1,i##_cond=(b);i>=i##_cond;--i)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend() //sortで大きい順
#define UNIQUE(v) v.erase(unique(all(v)),v.end())
#define SUM(a) accumulate(all(a),0)
#define sz(x) ((ll)(x).size())
#define pb push_back
#define fst first
#define snd second
#define mp make_pair

typedef long long ll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<ll,ll> pii;

const ll inf = 1ll<<62;
const ll mod = 1e9+7;

#define int ll

main(){
  string s;
  while(1){
    string t;
    getline(cin, t);
    if(t == ".") break;
    rep(i,sz(t)){
      if(t[i] == '(') s += "(";
      else if(t[i] == ')') s += ")";
      else if(t[i] == '[') s += "[";
      else if(t[i] == ']') s += "]";
    }

    // 内側から消す
    while(1){
      string tmp = s;
      rep(i,sz(s)-1){
	if(s[i] == '(' and s[i+1] == ')'){
	  s.erase(i,2);
	  break;
	}else if(s[i] == '[' and s[i+1] == ']'){
	  s.erase(i,2);
	  break;
	}
      }  
      if(tmp == s) break;
    }
    if(s == "") cout << "yes" << endl;
    else cout << "no" << endl;
    s.clear();
  }
}

C++: #include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main() {
	string b;
	int i, c=0,d=0;
	while (1) {
		getline(cin,b);
		for (i = 0; i<(int)b.size(); i++) {
				if (b[i] == '(') { c += 1; }
				if (b[i] == ')') {
					c -= 1;
					if (c < 0) { break; }
				}
				if (b[i] == '[') { d += 1; }
				if (b[i] == ']') { d -= 1;
				if (d < 0) { break; }
				}
		}

		if (b[0]=='.'||b[1]=='.') { break; }
		if (c == 0 && d == 0) {cout << "yes"<<endl; }
			else { cout << "no"<<endl; }
			c = 0, d = 0;
	};
	return 0;}

C++: #include <iostream>
#include <map>
#include <stack>
#include <string>
using namespace std;

int main(){
    string line;
    
    map<char, char> bracket;
    bracket[')'] = '(';
    bracket[']'] = '[';
    
    while(getline(cin, line)){
        if(line == ".") break;
        
        bool ans = true;
        stack<char> s;
        for(int i = 0;i < line.size();i++){
            char c = line[i];
            if(c == '(' || c == '[') s.push(c);
            if(c == ')' || c == ']'){
                if(s.empty() || s.top() != bracket[c]){
                    ans = false;
                    break;
                }
                s.pop();
            }
        }
        if(ans) cout << "yes";
        else cout << "no";
        cout << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <tuple>
#include <queue>
#include <stack>

using namespace std;

class Solver {
public:
	static void solve() {
		vector<bool> res;
		string s;
		getline(cin, s);
		while (s[0] != '.') {
			res.emplace_back(balance(s));
			getline(cin, s);
		}

		for (int i = 0; i < res.size(); i++) {
			if (res[i])cout << "yes" << endl;
			else cout << "no" << endl;
		}
	}

	static bool balance(string s) {
		vector<int> index1(s.size(), -1);
		vector<int> index2(s.size(), -1);
		stack<int> stack1;
		stack<int> stack2;
		vector<int> nest1(s.size(), 0);
		vector<int> nest2(s.size(), 0);
		for (int i = 0; i < s.size(); i++) {
			if (i > 0) {
				nest1[i] = nest1[i - 1];
				nest2[i] = nest2[i - 1];
			}

			if (s[i] == '(') {
				nest1[i]++;
				stack1.push(i);
			}
			if (s[i] == ')') {
				nest1[i]--;
				if (stack1.empty())return false;
				index1[i] = stack1.top();
				stack1.pop();
			}
			if (s[i] == '[') {
				nest2[i]++;
				stack2.push(i);
			}
			if (s[i] == ']') {
				nest2[i]--;
				if (stack2.empty())return false;
				index2[i] = stack2.top();
				stack2.pop();
			}
		}
		if (!(stack1.empty() && stack2.empty()))return false;

		for (int i = 0; i < s.size(); i++) {
			if (index1[i] >= 0) {
				if (nest2[index1[i]] != nest2[i])return false;

				for (int j = index1[i] + 1; j < i; j++) {
					if (nest2[j] < nest2[i])return false;
				}
			}
			if (index2[i] >= 0) {
				if (nest1[index2[i]] != nest1[i])return false;

				for (int j = index2[i] + 1; j < i; j++) {
					if (nest1[j] < nest1[i])return false;
				}
			}
		}

		return true;
	}
};

int main() {
	Solver::solve();
}

C++: #include<bits/stdc++.h>
using namespace std;
 
#define REP(i,s,n) for(int i=s;i<n;++i)
#define rep(i,n) REP(i,0,n)
#define SORT(c) sort((c).begin(),(c).end())
#define IINF INT_MAX
#define LLINF LLONG_MAX
#define DEBUG false
 
typedef long long ll;
typedef pair<int, int> ii;

#define MAXI 1000000

int main() {

	string s;
	while(getline(cin, s), s != "."){
		stack<int> st;
		bool flag = true;
		rep(i, s.size()){
			if(s[i] == '(') st.push(1);
			if(s[i] == '[') st.push(2);
			if(s[i] == ')'){
				if(st.empty() || st.top() != 1){
					flag = false;
					break;
				}
				st.pop();
			}
			if(s[i] == ']'){
				if(st.empty() || st.top() != 2){
					flag = false;
					break;
				}
				st.pop();
			}
		}
		if(flag) cout << "yes" << endl;
		else cout << "no" << endl;
	}
	

	return 0;
}

C++: #include<iostream>
#include<string.h>
#include<string>
#include<cstdio>
#include<stdlib.h>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#include<set>
#define ll long long int
#define ld long double
#define INF 1000000000
#define EPS 0.0000000001
#define rep(i,n) for(i=0;i<n;i++)
using namespace std;
typedef pair<int, int> pii;

int main()
{
  while(1){
    stack<int> sta;//[]:1 ():2 
    bool ans=true;
    string c;
    getline(cin,c);
    if(c[0]=='.')break;
    else if(c[0]=='[')sta.push(1);
    else if(c[0]=='(')sta.push(2);
    else if(c[0]==']' || c[0]==')')ans=false;
    int i=0;
    while(ans){
      i++;
      // printf("c='%c'\n",c[i]);
      if(c[i]=='[')sta.push(1);
      else if(c[i]=='(')sta.push(2);
      else if(c[i]==']')
	if(sta.size() && sta.top()==1)sta.pop();
	else{
	  ans=false;
	  break;
	}
      else if(c[i]==')')
	if(sta.size() && sta.top()==2)sta.pop();
	else{
	  ans=false;
	  break;
	}
      else if(c[i]=='.')break;
    }
    if(sta.size()==0 && ans)cout<<"yes"<<endl;
    else{
      cout<<"no"<<endl;
    }
  }


}

C++: #include<iostream>
#include<stack>
#include<string>
using namespace std;
int main()
{
  string s;
  int i;
  bool f;
  while(getline(cin,s),s[0]!='.'){
    stack<char> st;
    f=true;
    for(i=0;i<s.size();i++){
      if(s[i]=='('||s[i]=='['){
	st.push(s[i]);
      }else if(s[i]==')'||s[i]==']'){
	if(!st.empty()&&s[i]-st.top()<3)st.pop();
	else f=false;
      }
    }
    puts(f&&st.empty()?"yes":"no");
  }
}