ZOJ 2021 Complicated Expressions

WA!!!

  1 //Wrong Answer

  2 #include <cstdio>

  3 #include <cstring>

  4 #include <cstdlib>

  5 #include <cctype>

  6 

  7 const int MAXN = 260;

  8 

  9 char str[MAXN];

 10 bool vis[MAXN];

 11 int stack[MAXN];

 12 int kuohao[MAXN];

 13 int len;

 14 

 15 void kuo_hao_pei_dui()

 16 {

 17     int top = 0;

 18 

 19     for ( int i = 0; i < len; i++ )

 20     {

 21         if ( str[i] == '(' )

 22             stack[ ++top ] = i;

 23         else if ( str[i] == ')' )

 24             kuohao[i] = stack[top--];

 25     }

 26     return;

 27 }

 28 

 29 bool check( char a )

 30 {

 31     return ( a == '+' || a == '-' || a == '*' || a == '/' );

 32 }

 33 

 34 bool cmp( char a, char b )

 35 {

 36     if ( ( a == '+' || a == '-' ) && ( b == '*' || b == '/' ) ) return true;

 37     return false;

 38 }

 39 

 40 bool JudgeDelete( int x, int y )

 41 {

 42     char temp = '=';

 43     char temp2,temp3;

 44     bool first = true;

 45 

 46     int i;

 47     for ( i = x + 1; i < y; i++ )

 48     {

 49         if ( first && check(str[i]) )

 50         {

 51             temp = str[i];

 52             first = false;

 53         }

 54         else if ( check(str[i]) )

 55         {

 56             if ( cmp( str[i], temp ) ) temp = str[i];

 57         }

 58         else if ( str[i] == '(' )

 59             while ( str[i] != ')' ) i++;

 60     }

 61 

 62     if ( temp == '=' ) return true;              //如果括号之间没有运算符,可以直接去掉

 63 

 64     for ( i = x; i >= 0; i-- )

 65     {

 66         if ( check(str[i]) )

 67         {

 68             temp2 = str[i];

 69             if ( str[i] == '-' && ( temp == '+' || temp == '-' ) ) return false;   //如果括号里面有+-,前面有负号

 70             else if ( ( str[i] == '*' || str[i] == '/' ) && ( temp == '+' || temp == '-' ))   //如果括号里面有+-,前面有*/

 71                 return false;

 72             break;

 73         }

 74     }

 75 

 76     if( temp2 == '/' )      //如果括号前面是负号

 77         return false;

 78 

 79     for ( i = y; i < len; i++ )

 80     {

 81         if ( check(str[i]) )

 82         {

 83             temp3 = str[i];

 84             if ( ( str[i] == '*' || str[i] == '/' ) && ( temp == '+' || temp == '-' ) )  //如果括号后面是*/,里面是+-

 85                 return false;

 86             break;

 87         }

 88     }

 89     return true;

 90 }

 91 

 92 void Solve()

 93 {

 94     memset( vis, true, sizeof(vis) );

 95 

 96     for ( int i = len - 1; i >= 0; i-- )

 97     {

 98         if ( str[i] == ')' )

 99         {

100             if ( JudgeDelete( kuohao[i], i ) )

101             {

102                 vis[i] = false;

103                 vis[ kuohao[i] ] = false;

104             }

105         }

106     }

107 

108     for ( int i = 0; i < len; i++ )

109         if ( vis[i] ) putchar(str[i]);

110     putchar('\n');

111 

112     return;

113 }

114 

115 int main()

116 {

117     int T;

118     scanf( "%d", &T );

119     while ( T-- )

120     {

121         scanf( "%s", str );

122         len = strlen( str );

123 

124         kuo_hao_pei_dui();

125         Solve();

126     }

127     return 0;

128 }

 

 

你可能感兴趣的:(express)