首页 > 代码库 > ZOJ Problem Set - 1004-Anagrams by Stack

ZOJ Problem Set - 1004-Anagrams by Stack



唉!先直接上源代码吧!什么时候有时间的再来加说明!

#include<iostream>
#include<vector>
#include<stack>
#include<deque>
#include<algorithm>
#include<iterator>
using namespace std;

/*
*i代表PUSH,o代表POP
*/
bool judge(deque<char> sou,deque<char> des,vector<char> & res)
{
	stack<char> inistack;
	for(vector<char>::iterator iter = res.begin();iter!=res.end();iter++)
	{
	 if('i' == *iter)
	 {
		 inistack.push(sou.front());
		 sou.pop_front();
	 }
	 else
	 {
		 if(des.front() != inistack.top())
			 return false;
		 des.pop_front();
		 inistack.pop();
	 }
	}
	return true;
}

void fun(deque<char>& sou,deque<char>& des,vector<char> & res)
{
	if(res.size() >= 2*sou.size())
	{
		if(judge(sou,des,res))//输出结果
		{
			copy(res.begin(),res.end(),ostream_iterator<char>(cout," "));
			cout<<endl;
		}
	  return;
	}

	for (int i = 0; i <= 6; i+=6)
	{
		if(6==i&&(count(res.begin(),res.end(),'o')>=count(res.begin(),res.end(),'i')))
			continue;
		if(0==i&&((count(res.begin(),res.end(),'i')-count(res.begin(),res.end(),'o'))>=(2*sou.size()-res.size()) ))
			continue;
		res.push_back('i'+i);
		fun(sou,des,res);
		res.pop_back();
	}
}

int main()
{
	deque<char> source,desti;
	vector<char>  res;
	copy(istream_iterator<char>(cin),istream_iterator<char>(),inserter(source,source.end()));
	cin.clear();
	copy(istream_iterator<char>(cin),istream_iterator<char>(),inserter(desti,desti.end()));
	fun(source,desti,res);
	return 0;
}




ZOJ Problem Set - 1004-Anagrams by Stack