集合间并交差运算。中缀转后缀及后缀运算。HOJ1466 Friends。

将中缀表达式转换为后缀表达式确定运算顺序。

运算通过stl stack实现。

结果通过stl map存储。

Friends


Time limit: 1sec. Submitted: 41
Memory limit: 32M Accepted: 26
Source: University of Ulm Internal Contest 1999

You want to plan a big birthday party with your friends. On planning you notice that you have to do a lot of operations with sets of friends. There is one group which consist of Arthur, Biene and Clemens. Then there is a group of friends you know from snowboarding which consists of Daniel, Ernst, Frida and Gustav. If you want to invite them both, the resulting party group consists of g1 + g2 (the result is the union of both groups). Then you can compute the intersection of the two groups g1 * g2, which consists of the empty set. Maybe you want to invite a group g1, but excluding all members of an other group g2, which is written as g1 - g2.

Intersection (*) has precedence over union (+) and set difference (-). All operations are left associative, which means that in A op1 B op2 C you first have to evaluate A op1 B (provided op1 and op2 have equal precedence).


Input

The input consists of one or more lines. Each line contains one expression that you have to evaluate. Expressions are syntactically correct and only consist of the characters:

  • '{' and '}'
  • the elements 'A' to 'Z' meaning friend Arthur to Zora.
  • the operations '+', '-' and '*'
  • '(' and ')' for grouping operations
  • the newline character '\n' marking the end of an expression.

A line is never longer than 255 characters.


Output

Output the resulting set in curly braces '{' and '}', each on a line of its own. Print elements of sets sorted alphabetically.

Sample Input

{ABC}
{ABC}+{DEFG}+{Z}+{}
{ABE}*{ABCD}
{ABCD}-{CZ}
{ABC}+{CDE}*{CEZ}
({ABC}+{CDE})*{CEZ}
Sample Output
{ABC}
{ABCDEFGZ}
{AB}
{ABD}
{ABCE}
{CE}

说明:
可以理解为集合之间的并交和差运算。优先级’+ ‘= '-‘ < ’*‘。

代码如下:
#include<iostream>
#include
<string>
#include
<stack>
#include
<map>
#include
<vector>
using namespace std;

int op(char x) {
if (x == '+')return 1;
else if (x == '-')return 1;
else if (x == '*')return 2;
else if (x == '(')return -1;
}

string pl(string a, string b) {
string ret = "";
map
<char, int> tmp;
map
<char, int>::iterator it;
for (int i = 0; i < a.length(); i++) {
tmp[a[i]]
= 1;
}
for (int i = 0; i < b.length(); i++) {
tmp[b[i]]
= 1;
}
for (it = tmp.begin(); it != tmp.end(); it++) {
if (it->second)
ret
+= (it->first);
}
return ret;
}

string mi(string a, string b) {
string ret = "";
map
<char, int> tmp;
map
<char, int>::iterator it;
for (int i = 0; i < a.length(); i++) {
tmp[a[i]]
= 1;
for (int j = 0; j < b.length(); j++) {
if (a[i] == b[j]) {
tmp[a[i]]
= 0;
}
}
}
for (it = tmp.begin(); it != tmp.end(); it++) {
if (it->second)
ret
+= (it->first);
}
return ret;
}

string mu(string a, string b) {
string ret = "";
map
<char, int> tmp;
map
<char, int>::iterator it;
for (int i = 0; i < a.length(); i++) {
for (int j = 0; j < b.length(); j++) {
if (a[i] == b[j]) {
tmp[a[i]]
= 1;
}
}
}
for (it = tmp.begin(); it != tmp.end(); it++) {
if (it->second)
ret
+= (it->first);
}
return ret;
}

int main() {
string t;
vector
<string> ret;
while (cin >> t) {
ret.clear();
stack
<char> a;
stack
<string> cal;
string tmp = "";
for (int i = 0; i < t.length(); i++) {
if (t[i] == '{') {
i
++;
while (t[i] != '}')
tmp
+= t[i++];
ret.push_back(tmp);
tmp.clear();
}
else if (t[i] == '(') a.push(t[i]);
else if (t[i] == ')') {
while (a.top() != '(') {
tmp
+= a.top();
ret.push_back(tmp);
a.pop();
tmp.clear();
}
a.pop();
}
else {
if (a.empty() || op(t[i]) > op(a.top()))a.push(t[i]);
else {
while (op(t[i]) <= op(a.top())) {
tmp
+= a.top();
ret.push_back(tmp);
a.pop();
tmp.clear();
if (a.empty())break;
}
a.push(t[i]);
}
}
}
while (!a.empty()) {
tmp
+= a.top();
ret.push_back(tmp);
a.pop();
tmp.clear();
}

string x, y;
for (int i = 0; i < ret.size(); i++) {
if (ret[i] == "+") {
x
= cal.top();
cal.pop();
y
= cal.top();
cal.pop();
cal.push(pl(x, y));
}
else if (ret[i] == "-") {
x
= cal.top();
cal.pop();
y
= cal.top();
cal.pop();
cal.push(mi(y, x));
}
else if (ret[i] == "*") {
x
= cal.top();
cal.pop();
y
= cal.top();
cal.pop();
cal.push(mu(x, y));
}
else {
cal.push(ret[i]);
}
}
cout
<< "{";
cout
<< cal.top();
cout
<< "}\n";
}
return 0;
}
posted @ 2011-06-09 14:14  归雾  阅读(484)  评论(0)    收藏  举报