POJ:题号 2269---------Friends问题

本文介绍了一种使用 C++ 实现的算法,该算法能够解析并计算带有优先级的表达式。通过构建后缀表达式的方式处理含有变量、加减乘运算及括号的表达式,并利用栈和集合等数据结构来存储中间结果和操作符。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//题目网址:http://poj.org/problem?id=2269
#include<iostream>
#include<string>
#include<set>
#include<vector>
#include<map>
#include<stack>
//#include<Windows.h>

using namespace std;


int main()
{
	//DWORD t1 = GetTickCount();
<span style="white-space:pre">	</span>//用map数据结构表示优先级,值越大,优先级越高
	map<char,int> mapoper;
	mapoper['(']=2;
	mapoper[')']=2;
	mapoper['*']=1;
	mapoper['+']=0;
	mapoper['-']=0;

	char lpstrnum[26];
	for(int i=0;i<26;++i)
	{
		lpstrnum[i]='A'+i;
	}
	set<char> setnum(lpstrnum,lpstrnum+sizeof(lpstrnum)/sizeof(char));
	
	string str;
	vector<string> vecStr;
	
	while(cin>>str){

	set<char> friends;
	vector< set<char> > frdfmat;
	stack<char> staope;
	for(string::iterator it=str.begin();/**it!='\n'&&*/it!=str.end();++it)      //构建后缀表达式
	{
		if(setnum.count(*it))
			friends.insert(*it);
		if(*it=='}'){<span style="white-space:pre">					</span>//以set<char>集合存储朋友堆
			frdfmat.push_back(friends);
			friends.clear();
		}
		if(mapoper.count(*it)){<span style="white-space:pre">				</span>//朋友堆和操作符堆都以set<char>的形式放到vector< set<char> >容器中
			staope.push(*it);
			if(staope.size()!=1){
				if(*it==')'){
					staope.pop();
					while(staope.top()!='('){     //操作符放到stack<char>中
						set<char> ope;
						ope.insert(staope.top());
						staope.pop();
						frdfmat.push_back(ope);
					}
					staope.pop();
				}
				if(*it!=')' && mapoper[*it]<=mapoper[staope.top()]){
					staope.pop();
					while(!staope.empty()&&staope.top()!='('&&mapoper[*it]<=mapoper[staope.top()]){
						set<char> temp;
						temp.insert(staope.top());
						frdfmat.push_back(temp);
						staope.pop();
					}
					staope.push(*it);
				}
			}
		}
	}
	while(!staope.empty()){
		set<char> temp;
		temp.insert(staope.top());
		frdfmat.push_back(temp);
		staope.pop();
	}

    <span style="white-space:pre">	</span>//计算后缀表达式
	stack< set<char> > num;
	for(vector< set<char> >::iterator it=frdfmat.begin();it!=frdfmat.end();++it)
	{
		set<char>::iterator ii=it->begin();
		set<char> ri;
		set<char> le;
		if(ii==it->end()){
			num.push(*it);
			continue;
		}
		if(setnum.count(*ii)){
			num.push(*it);
			continue;
		}
		switch (*ii)
		{
		case '+' : 
			ri=num.top();num.pop();le=num.top();num.pop();
			ri.insert(le.begin(),le.end());
			num.push(ri);
			break;
		case '-':
			ri=num.top();num.pop();le=num.top();num.pop();
			for(set<char>::iterator ite=le.begin();ite!=le.end();){
				if(ri.count(*ite))
					ite=le.erase(ite);
				else
					++ite;
			}
			num.push(le);
			break;
		case '*':
			ri=num.top();num.pop();le=num.top();num.pop();
			for(set<char>::iterator ite1=le.begin();ite1!=le.end();){
				if(!ri.count(*ite1))
					ite1=le.erase(ite1);
				else
					++ite1;
			}
			num.push(le);
			break;
		default :
			num.push(*it);
		}
		//cout<<*it<<endl;
		//friends.insert(*it);
	}


		cout<<'{';
		for(set<char>::const_iterator it=num.top().begin();it!=num.top().end();++it){
			cout<<*it;
		}
		cout<<'}'<<endl;
	}
	//printf("%fms\n", GetTickCount() - t1);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值