码迷,mamicode.com
首页 > 其他好文 > 详细

ZOJ Problem Set - 1004-Anagrams by Stack

时间:2014-11-06 14:59:49      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:zoj   回溯法   c++   



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

#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;
}
bubuko.com,布布扣




ZOJ Problem Set - 1004-Anagrams by Stack

标签:zoj   回溯法   c++   

原文地址:http://blog.csdn.net/yyc1023/article/details/40859249

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!