Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

代码帖错了,是这个!

Posted by sky_handsome at 2007-05-10 23:30:40 on Problem 1141
In Reply To:难道贪心法不对 ? Posted by:sky_handsome at 2007-05-10 23:16:14
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string s;
	cin >> s;
	bool* match = new bool[s.length()];
	int i, j;

	for(i = 0; i < s.length(); ++i)
		match[i] = false;

	int sleft, sright, bleft, bright;
	sleft = sright = bleft = bright = 0;

	string result;
	for(i = 0; i < s.length(); ++i)
	{
		if(s[i] == '(')
		{
			sleft = 1;
			sright = 0;
			for(j = i+1; j < s.length(); ++j)
			{
				if(s[j] == '(')
					++sleft;
				else if(s[j] == ')')
					++sright;
				if(sleft == sright)
					break;
			}
			if(sleft == sright)
			{
				result += '(';
				match[j] = true;
			}
			else
				result += "()";
		}
		else if(s[i] == ')')
		{
			if(match[i])
				result += ')';
			else
				result += "()";
		}
		else
		{
			result += s[i];
		}
	}

	s = result;
	result = "";

	delete[] match;

	match = new bool[s.length()];

	for(i = 0; i < s.length(); ++i)
		match[i] = false;

	for(i = 0; i < s.length(); ++i)
	{
		if(s[i] == '[')
		{
			sleft = 0;
			sright = 0;
			bleft = 1;
			bright = 0;
			for(j = i+1; j < s.length(); ++j)
			{
				if(s[j] == '[')
				{
					++bleft;
				}
				else if(s[j] == ']')
				{
					++bright;
				}
				else if(s[j] == '(')
					++sleft;
				else if(s[j] == ')')
					++sright;
				if(bleft == bright)
					break;
			}
			if(bleft == bright && sleft == sright)
			{
				result += '[';
				match[j] = true;
			}
			else
				result += "[]";
		}
		else if(s[i] == ']')
		{
			if(match[i])
				result += ']';
			else
				result += "[]";
		}
		else
		{
			result += s[i];
		}
	}

	delete[] match;

	cout << result << endl;


	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator