Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C++ by NguyenVietNamSon ( 6 years ago )
#include <iostream>
#include <string>
#include <stack>
using namespace std;

bool KiemTraDayNgoac(string str)
{
	stack<char> s;

	int i = 0;
	while(str[i] != '\0')
	{
		if(str[i] == '(')
			s.push(str[i]);
		else if(str[i] == ')')
		{
			if(s.empty() == true)
			{
				return false;
			}
			s.pop();
		}
		i++;
	}
	if(s.empty() == false)
		return false;

	return true;
}

int main()
{
	string s = "*(";
	int soluongdausao = 0;

	int i = 0;
	while(s[i] != '\0')
	{
		if(s[i] == '*')
			soluongdausao++;

		i++;
	}

	int n = soluongdausao;
	int *a = new int[n];

	for(int i = 0; i < n; ++i)
		a[i] = 0;

	int idx = n - 1;
	int dem = 1;

	bool check = false;
	while(true)
	{
		if(a[idx] < 3)
		{
			string temp = "";

			i = 0;
			int count = 0;
			while(s[i] != '\0')
			{
				char c = s[i];

				if(s[i] == '*')
				{
					count++;

					if(a[count - 1] == 0)
						c = 'n'; // n hiểu là null
					else if(a[count - 1] == 1)
						c = '(';
					else if(a[count - 1] == 2)
						c = ')';
				}
				temp.push_back(c);

				i++;
			}
			cout << "TH " << dem++ << ": " << temp << endl;
			if(KiemTraDayNgoac(temp) == true)
			{
				check = true;
				break;
			}

			a[idx]++;
		}
		else
		{
			while(a[--idx] == 2) {}

			if(idx < 0)
				break;

			a[idx]++;

			for(int i = idx + 1; i < n; ++i)
				a[i] = 0;

			idx = n - 1;
		}
	}
	
	delete[] a;

	if(check == true)
		cout << "YES";
	else
		cout << "NO";
	

	system("pause");
	return 0;
}

 

Revise this Paste

Your Name: Code Language: