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

Re:(请问哪位知道 STL 的set的交、并操作怎么原地进行?)

Posted by xmjt621 at 2009-04-19 16:24:51 on Problem 1029 and last updated at 2009-04-19 16:25:57
In Reply To:呼呼~~~~第一次用类写程序,好像蛮爽的 Posted by:yzhw at 2009-01-13 15:26:39
我也用类写 不过效率就……
请问哪位知道 STL 的set的交、并操作怎么原地进行?

#pragma warning(disable: 4786)
#include <set>
#include <algorithm>
#include <iostream>
using namespace std;

typedef set<int> Set;

class Semi
{
public:
	Semi() : _isInited(false) {};
	void FillSet(const Set &l, const Set &r, char c)
	{
		Set temp;
		if ('<' == c)
		{
			if (_isInited)
			{
				set_intersection(_bad.begin(), _bad.end(),
					l.begin(), l.end(), inserter(temp, temp.begin()));
				_bad = temp;
			}
			else
			{
				_isInited = true;
				_bad = l;
			}
			temp.clear();
			set_union(_good.begin(), _good.end(),
				r.begin(), r.end(), inserter(temp, temp.begin()));
			_good = temp;
		}
		else if ('>' == c)
		{
			if (_isInited)
			{
				set_intersection(_bad.begin(), _bad.end(),
					r.begin(), r.end(), inserter(temp, temp.begin()));
				_bad = temp;
			}
			else
			{
				_isInited = true;
				_bad = r;
			}
			temp.clear();
			set_union(_good.begin(), _good.end(),
				l.begin(), l.end(), inserter(temp, temp.begin()));
			_good = temp;
		}
		else
		{
			set_union(_good.begin(), _good.end(),
				l.begin(), l.end(), inserter(temp, temp.begin()));
			_good.clear();
			set_union(temp.begin(), temp.end(),
				r.begin(), r.end(), inserter(_good, _good.begin()));
		}
	}
	int GetRes()
	{
		return 1 == _bad.size() ? *(_bad.begin()) : 0;
	}
	void Run()
	{
		Set temp;
		set_difference(_bad.begin(), _bad.end(),
			_good.begin(), _good.end(), inserter(temp, temp.begin()));
		_bad = temp;
	}
private:
	Set _bad, _good;
	bool _isInited;
};
class Judge
{
public:
	Judge(int n)
	{
		while (n)
		{
			_all.insert(n--);
		}
	}
	void Input(const Set &l, const Set &r, char c)
	{
		Set::const_iterator iter;
		for (iter = l.begin(); iter != l.end(); ++iter)
		{
			_all.erase(*iter);
		}
		for (iter = r.begin(); iter != r.end(); ++iter)
		{
			_all.erase(*iter);
		}
		_l.FillSet(l, r, c);
		if ('>' == c)
		{
			c = '<';
		}
		else if ('<' == c)
		{
			c = '>';
		}
		_h.FillSet(l, r, c);
	}
	int GetRes()
	{
		_l.Run();
		_h.Run();
		int res = 0;
		if (res = _l.GetRes()) 
		{
			if (_h.GetRes())
			{
				res = 0;
			}
		}
		else
		{
			res = _h.GetRes();
		}
		if (!res && 1 == _all.size())
		{
			res = *(_all.begin());
		}
		return res;
	}
protected:
	Set _all;
	Semi _l, _h;
};

int main()
{
	int m, n;
	cin >> n >> m;
	Judge j(n);
	while (m--)
	{
		int x;
		cin >> x;
		int xc = x;
		Set l, r;
		while (x--)
		{
			int y;
			cin >> y;
			l.insert(y);
		}
		while (xc--)
		{
			int y;
			cin >> y;
			r.insert(y);
		}
		char c;
		cin >> c;
		j.Input(l, r, c);
	}
	cout << j.GetRes() << 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