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 <vector>
#include <stack>
#include <iostream>
#include <string>

using namespace std;
int main(int argc,char **argv){
	vector<string> inputs;

	while(true){
		string line;
		getline(cin,line);
		if(line == "."){ break; }
		inputs.push_back( line );
	}

	for(auto it=inputs.begin();it!=inputs.end();++it){
		stack<char> st;
		bool isok=true;
		for(auto cit=it->begin();cit!=it->end();++cit){
			char c = *cit;
			if(c=='('||c=='['){ st.push(c); }
			else if(c==')'){
				if(st.size()==0){isok=false;break;}
				if(st.top()=='('){ st.pop(); }
				else{ isok=false; break; }
			}
			else if(c==']'){
				if(st.size()==0){isok=false;break;}
				if(st.top()=='['){ st.pop(); }
				else{ isok=false; break; }
			}
		}
		isok = isok && st.empty();
		cout << (isok?"yes":"no") << endl;
	}

}

C++: #include <iostream>
#include <string>
using namespace std;
#define rep(i,n) for(int i = 0 ; i < n ; i++)

char str[101];

int main()
{
  cin >> str;
  while(true){
    if(str[0] == '.') break;

    int cnt_1 = 0, cnt_2 = 0, cnt_3 = 0, cnt_4 = 0;
    for(int i = 0 ; str[i] != '.' ; i++){
      if(str[i] == '('){
	cnt_1++;
      }
      if(str[i] == ')'){
	cnt_2++;
      }
      if(str[i] == '['){
	cnt_3++;
      }
      if(str[i] == ']'){
	cnt_4++;
      }
    }
    if(cnt_1 == cnt_2 && cnt_3 == cnt_4){
      cout << "yes" << '\n';
      break;
    }else{
      cout << "no" << '\n';
      break;
    }
  }
}

C++: #include<iostream>
#include<string>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;

bool check(string s){
	string temp;
	bool flg=false;
	rep(i,s.size()){
		if(s[i]=='('){
			flg=true;
			bool hata=true;
			for(int j=s.size()-1;j>=0;j--){
				if(s[j]==')'){
					hata=false;
					if(i<j){
						temp=s.substr(i+1,j-i-1);
						return check(temp);
					}else return false;
				}
			}
			if(hata)return false;
		}
		if(s[i]=='['){
			flg=true;
			bool hata=true;
			for(int j=s.size()-1;j>=0;j--){
				if(s[j]==']'){
					hata=false;
					if(i<j){
						temp=s.substr(i+1,j-i-1);
						return check(temp);
					}else return false;
				}
			}
			if(hata)return false;
		}
		if(s[i]==')'){
			flg=true;
			bool hata=true;
			rep(j,s.size()){
				if(s[j]=='('){
					hata=false;
					if(j<i){
						temp=s.substr(j+1,i-j-1);
						return check(temp);
					}else return false;
				}
			}
			if(hata)return false;
		}
		if(s[i]==']'){
			flg=true;
			bool hata=true;
			rep(j,s.size()){
				if(s[j]=='['){
					hata=false;
					if(j<i){
						temp=s.substr(j+1,i-j-1);
						return check(temp);
					}else return false;
				}
			}
			if(hata)return false;
		}
	}
	if(!flg)return true;
}

int main(){
	string s;
	while(1){
		getline(cin,s);
		if(s==".")break;
		if(check(s)){
			cout<<"yes"<<endl;
		}else cout<<"no"<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <cstring>
#include <cctype>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <complex>
using namespace std;
#define REP(i,n) for(int i = 0; i < (int)n; i++)
#define FOR(i,a,b) for(int i = a; i < (int)b; i++)
const int INF = 1<<28;

string input;

int find(int a, int p) {
	int btn[2] = {};
	FOR(i, p, input.size()) {
		if(a) {
			if(input[i] == '[')
				btn[0]++;
			if(input[i] == ']')
				btn[0]--;
			if(input[i] == '(')
				btn[1]++;
			if(input[i] == ')') {
				btn[1]--;
				if(!btn[0] && !btn[1])
					return 0;
			}
		}
		else {
			if(input[i] == '(')
				btn[0]++;
			if(input[i] == ')')
				btn[0]--;
			if(input[i] == '[')
				btn[1]++;
			if(input[i] == ']') {
				btn[1]--;
				if(!btn[0] && !btn[1])
					return 0;
			}
		}
	}
	return 1;
}


int main() {
	while(getline(cin, input)) {
		if(input == ".") break;
		int cnt = 0;
		int chk[2] = {};
		REP(i, input.size()) {
			if(input[i] == '(') {
				cnt += find(1, i);
				chk[1]++;
			}
			if(input[i] == '[') {
				cnt += find(0, i);
				chk[0]++;
			}
			if(input[i] == ')') {
				chk[1]--;
				if(chk[1] < 0) cnt++;
			}
			if(input[i] == ']') {
				chk[0]--;
				if(chk[0] < 0) cnt++;
			}
		}
		cout << (cnt ? "no" : "yes") << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <stack>

using namespace std;

int main(){
	string s;
	
	while(getline(cin,s)){
		
		stack<char> st;
		bool flag=true;

		if(s==".")break;

		for(int i=0;i<s.size();i++){
			char ch=s[i];

			if(ch=='('||ch=='['){
				st.push(ch);
			}
			else if(ch==')'||ch==']'){
				if(st.empty()){
					flag=false;
					break;
				}else if(ch==')'&&st.top()=='('){
					st.pop();
				}else if(ch==']'&&st.top()=='['){
					st.pop();
				}
				else{
					flag=false;
					break;
				}
			}
		}
		if(st.empty()&&flag){
			cout<<"yes"<<endl;
		}else{
			cout<<"no"<<endl;
		}
	}
	return 0;
}

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

bool solve(string str) {
  stack<char> stk;
  for (char c : str) {
    if (c == '[') {
      stk.push(c);
    } else if (c == '(') {
      stk.push(c);
    } else if (c == ']') {
      if (stk.empty() or stk.top() != '[') {
        return false;
      }
      stk.pop();
    } else if (c == ')') {
      if (stk.empty() or stk.top() != '(') {
        return false;
      }
      stk.pop();
    }
  }
  return stk.empty();
}

int main() {
  string query;
  while (getline(cin, query) and query != ".") {
    cout << (solve(query) ? "yes" : "no" ) << endl;
  }
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <cstring>
#include <numeric>
#include <algorithm>
#include <functional>
#include <array>
#include <map>
#include <queue>
#include <limits.h>
#include <set>
#include <bitset>
#include <stack>
#include <cstdlib>
#define REP(i,n) for(int i = 0; n > i; i++)
#define MOD 1000000007
using namespace std;
typedef vector<int> Ivec;
typedef pair<int, int> pii;
typedef long long int ll;
char str[101];
map<char, char> kako = { {'(',')'},{'[',']'} };
int is_balanced(int s) {
	for (int i = s+1; strlen(str) > i; i++) {
		if (str[i] == '(' || str[i] == '[') {
			i = is_balanced(i)+1;
			if (i == 0) return -1;
		}
		if (str[i] == ')' || str[i] == ']') {
			if(str[i] != kako[str[s]])
				return -1;
			else {
				return i;
			}
		}
	}
	return -1;
}

int main() {
	while (fgets(str,100, stdin)) {
		bool f = true;
		REP(i, strlen(str)) {
			if (str[i] == '(' || str[i] == '[') {
				i = is_balanced(i)+1;
				if (i == 0) {
					printf("no\n");
					f = false;
					break;
				}
			}
		}
		if (f) printf("yes\n");
	}

	return 0;
}