#include <iostream>
#include <fstream>
#include <string>

using namespace::std;

int main () {

	string temp;
	string search;
	string brackets, end, beg, rev, letter;	
	int length;
	int y=0;
	bool balanced=true;

	ifstream infile("DATA4.txt");
	ofstream outfile("OUT4.txt",ios::out);

	do {
		infile >> temp;
		for (int i=0; i <= temp.length(); i++) {
			if((temp[i]=='(') || (temp[i]=='[') || (temp[i]=='{') || (temp[i]==')') || (temp[i]==']') || (temp[i]=='}')) {
				//cout << temp[i];				
				brackets += temp[i];
			}
		}
		
		if (brackets.length()%2 != 0)
			outfile << "not balanced" << endl;
		else {
			length = (brackets.length())/2;
			
			beg = brackets.substr(0,length);
			end = brackets.substr(length,length);
			

			for (int j=0; j <= beg.length(); j++) {
				if((beg[j]==')') || (beg[j]==']') || (beg[j]=='}')) {
					balanced = false;
				}
			}
			if (balanced == true){
				
				for (int k=0; k <= end.length(); k++) {
					if(end[k]==')')
						end[k] = '(';
					else if (end[k]==']')
						end[k] = '[';
					else if (end[k]=='}')
						end[k] = '{';
					
				}
				
				int Storage=end.length();
				//For loop reverses the word entered by the user
				for(int j=0;j<=Storage;Storage--){
						letter=end.substr(Storage,1);
						rev += letter;
				}

				if (rev == beg) 
					outfile << "balanced" << endl;
				else
					outfile << "not balanced" << endl;
				
			}
			else 
				outfile << "not balanced" << endl;
		}
		y++;
	} while (y<5);

return(0);			
}

