Home > Back-end >  POJ1100
POJ1100

Time:10-06

Is this poj questions 1100 Dreisam Equations
Submitted to overrun the memory, don't know how to modify, way of thinking is recursive DFS and judge each branch is in line with the requirements, to ask everybody how should change? My guess is that in the process of recursive USES too many variables (each layer of recursive it took me three stacks, subject limit number of up to 12)
 
#include
#include
#include
#include
#include
using namespace std;
Enum flag {find not_find};
Flag outputflag=not_find;
Int size=0, the result;
Int global_element=0;
Char global_sign [50].
int count=0;
Void initial_global ()
{
Size=0;
Result=0;
Global_element=0;
Memset (global_sign, 0, sizeof (char) * 50);
Outputflag=not_find;
}
Void readin (int answer_i, int element [50], int & amp; Elem_num)
{
Elem_num=0;
Memset (element, 0, sizeof (int) * 50);
String STR.//initialize
Getline (cin, STR);
If (STR=="0")
exit(0);
Int loca=STR. Find ('=');
String answer=STR. Substr (0, loca - 1);
Answer_i=atoi (STR) c_str ());
Result=answer_i;//use global for convenience
For (int I=loca; i{
If (STR [I]==' ')//start getting a num
{
Int the end=STR. Find_first_of (", I + 1);
String temp=STR., substr (I + 1, end - I - 1);
If (temp [0]=='(')
{
Element [elem_num + +]=1;
Element [elem_num]=atoi (temp. Substr (1, 1). The c_str ());
}
Else if (end> 0 & amp; & \ [end - I - 2)==') ')//there are end - I - 1 digital so the location of the last digit should - 1
{
Element [elem_num + +]=atoi (temp. Substr (0, 1). The c_str ());
Element [elem_num]=2;
}
The else
{
Element [elem_num]=atoi (temp. C_str ());
}
Elem_num + +;
Size++;
}
}
Global_element=elem_num;
return;

}
Bool calculate (stack Sign, int element [50])
{
Stack Reverse, copy;
Stack Cal_stack;
Stack Num_stack;
while(! Sign. Empty ())
{
Reverse. Push (sign. The top ());
Sign. Pop ();
}
Copy=reverse;
Memset (global_sign, 0, sizeof (char) * 50);
for(int i=0; ! Copy. The empty (); I++)
{
Global_sign [I]=copy. The top ();
Copy. Pop ();
}
for(int i=0; i{
If (element [I]==1)
Num_stack. Push (1);
Else if (element [I]==2)
{
Int temp=num_stack. Top ();
Num_stack. Pop ();
Num_stack. Pop ();
If (num_stack. Empty () | | num_stack. The top ()==1)
Num_stack. Push (temp);//pop out and push the number back
The else
{
Int op1=num_stack. Top ();
Num_stack. Pop ();
If (cal_stack. Top ()=='+')
Num_stack. Push (op1 + temp);
Else if (cal_stack. Top ()=='-')
Num_stack. Push (op1 - temp);
Else if (cal_stack. Top ()=='*')
Num_stack. Push (op1 * temp);
Cal_stack. Pop ();
}
Cal_stack. Push (reverse. The top ());
Reverse. Pop ();
}
The else//it is number
{
If (num_stack. Empty () | | num_stack. The top ()==1)
{
Num_stack. Push (element [I]);
}
The else
{
Int op1=num_stack. Top ();
Num_stack. Pop ();
If (cal_stack. Top ()=='+')
Num_stack. Push (op1 + element [I]);
Else if (cal_stack. Top ()=='-')
Num_stack. Push (op1 - element [I]);
Else if (cal_stack. Top ()=='*')
Num_stack. Push (op1 * element [I]);
Cal_stack. Pop ();
}
If (element [I + 1)!=- 2 & amp; & Element [I + 1)!=0)
{
Cal_stack. Push (reverse. The top ());
Reverse. Pop ();
}

}
}
If (num_stack. Top ()==result)
return true;
The else
return false;
}
Void the output element (int [50])
{
Cout<& lt;" Equation # "& lt; Cout}
Else if (element [I]==2)
{
Cout<& lt;" ) "& lt; n++;
}
The else
{
If (element [I + 1)!
=2){
Coutn++;
}
The else
{
Cout}

}
}
Coutreturn ;
}
Void search (int element_num stack Sign, int element [50])
{
2//1 stands for '+' stands for '-' 3 stands for '*'
If (element_num==1)//element should be 1 more than the operator and the element num 1 is larger than actual element number
{
If (calculate (signs and element))
{
If (outputflag==not_find)
The output (element);
Outputflag=find;
}
return;
}
The else
{
Sign. Push (' + ');
Search (element_num - 1, signs and element).
Sign. Pop ();
Sign. Push (' - ');
Search (element_num - 1, signs and element).
Sign. Pop ();
Sign. Push (' * ');
Search (element_num - 1, signs and element).
Sign. Pop ();
If (element_num==size& & Outputflag==not_find)
{
Cout<& lt;" Equation # "& lt; Cout<& lt;" Impossible "& lt; }
}

}
Int main ()
{
While (1)
{
Initial_global ();
count++; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related