• POJ1690 简单运算去括号


     题目大意:

    给定一串只含加减和括号的运算,去掉没用的括号和空白字符输出

    这里其实只要去找当前括号前面那个运算符是不是减号,如果是减号且这个括号内出现过运算符说明这个括号应该存在

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <iostream>
     4 #include <algorithm>
     5 
     6 using namespace std;
     7 #define N 1010
     8 #define MOD 1000007
     9 #define base 31
    10 
    11 char s[N];
    12 char que[N];
    13 int top;
    14 bool flag[N];
    15 
    16 void solve()
    17 {
    18     top = 0;
    19     for(int i=0 ; i<strlen(s) ; i++){
    20         que[++top] = s[i];
    21         flag[top] = true;
    22         if(s[i] == ')'){
    23             int cnt = 0 , ch=0;//ch记录括号过程中有没有出现过多次运算
    24             for(int j=top ; j>0 ; j--){
    25                 if(que[j]=='(' && flag[j]==true){
    26                     cnt--;
    27                     if(cnt == 0){
    28                         int k=j-1;
    29                         while(k){
    30                             if(flag[k]==true&&(que[k]=='('||que[k]=='+'||que[k]=='-')) break;
    31                             k--;
    32                         }
    33                       //  cout<<i<<" "<<j<<" "<<k<<endl;
    34                         if(ch==0 || k==0 ||(k>0&&que[k] != '-')) flag[top] = flag[j] = false;
    35                     }
    36                 }
    37                 else if(que[j]==')' && flag[j]==true) cnt++;
    38                 else if(que[j]=='+' || que[j]=='-') ch++;
    39             }
    40         }
    41     }
    42 }
    43 
    44 int main()
    45 {
    46     #ifndef ONLINE_JUDGE
    47         freopen("a.in" , "r" , stdin);
    48     #endif // ONLINE_JUDGE
    49     int n;
    50     while(~scanf("%d" , &n))
    51     {
    52         gets(s);
    53         for(int i=0 ; i<n ; i++){
    54             gets(s);
    55             solve();
    56             for(int i=1;i<=top;i++){
    57                 if(que[i] == ' ') continue;
    58                 if(flag[i]==true) printf("%c" , que[i]);
    59             }
    60             puts("");
    61         }
    62     }
    63     return 0;
    64 }
  • 相关阅读:
    python 元组及操作
    python 字典所有操作
    python 列表操作
    python 运算符
    python while循环
    python 分支语句 等值判断 逻辑运算符
    GDI+_从Bitmap里得到的Color数组值分解
    服务器设计策略
    服务器设计策略
    IOCP的缓冲区
  • 原文地址:https://www.cnblogs.com/CSU3901130321/p/4546108.html
Copyright © 2020-2023  润新知