zoukankan      html  css  js  c++  java
  • [水]剔除多余括号

    【问题描述】
    键盘输入一个含有括号的四则运算表达式,可能含有多余的括号,编程整理该表达式,去掉所有多余的括号,原表达式中所有变量和运算符相对位置保持不变,并保持与原表达式等价。
    例:

    输入表达式  应输出表达式
    a+(b+c)  a+b+c

    (a*b)+c/d   a*b+c/d


    a+b/(c-d)   a+b/(c-d)
    注意输入 a+b 时不能输出 b+a。
    表达式以字符串输入。
    所有变量为单个小写字母。只是要求去掉所有多余括号,不要求对表达式化简。
    【输入数据】
    一个字符串,长度不超过 255,输入不要判错
    【输出数据】
    去掉所有多余括号后的表达式
    【样例输入】
    a+(b+c)
    【样例输出】
    a+b+c

    经典水题。分治。

    说来惭愧之前竟然没有写过。

    20min水掉。

    View Code
     1 program ub;
     2 
     3 Type
     4  rec=record
     5    st,ed:longint;
     6  end;
     7 
     8  Var
     9   s:string;
    10 
    11 Procedure fopen;
    12   begin
    13   assign(input,'ub.in');
    14   assign(output,'ub.out');
    15   reset(input);
    16   rewrite(output);
    17 end;
    18 
    19 Procedure fclose;
    20   begin
    21   close(input);
    22   close(output);
    23 end;
    24 
    25 Function havep(S:String):boolean;
    26 var
    27  deep,i:longint;
    28   begin
    29   havep:=false;
    30   deep:=0;
    31   for i:=1 to length(s) do
    32     begin
    33     if ( s[i] in ['+','-'] )  and (deep=0) then
    34       exit(true);
    35     if s[i]='(' then inc(deep);
    36     if s[i]=')' then dec(deep);
    37   end;
    38 end;
    39 
    40 Function work(S:String):string;
    41 var
    42  ct,deep,i:longint;
    43  ans,y:string;
    44  a:array[0..300] of rec;
    45  can:boolean;
    46   begin
    47   ct:=0;
    48   deep:=0;
    49   for i:=1 to length(s) do
    50     if (s[i]='(') then
    51       begin
    52       inc(deep);
    53       if deep=1 then begin inc(ct); a[ct].st:=i; end;
    54     end else
    55       if s[i]=')' then
    56         begin
    57         dec(deep);
    58         if deep=0 then a[ct].ed:=i;
    59       end;
    60   if ct=0 then exit(s);
    61   a[ct+1].st:=length(s)+1;
    62   ans:=copy(s,1,a[1].st-1);
    63   for i:=1 to ct do
    64     begin
    65     y:=work(copy(s,a[i].st+1,a[i].ed-a[i].st-1));
    66     can:=true;
    67     //fore
    68     if (a[i].st>1) then
    69       case s[ a[i].st-1 ] of
    70         '-':if (length(y)>1) and havep(y) then can:=false;
    71         '*':if havep(y) and (length(y)>1) then can:=false;
    72         '/':if length(y)>1 then can:=false;
    73       end;
    74     //back
    75     if a[i].ed<length(s) then
    76       case s[ a[i].ed+1] of
    77         '*':if havep(y) and (length(y)>1) then can:=false;
    78         '/':if havep(y) and (length(y)>1) then can:=false;
    79       end;
    80     if can then ans:=ans+y else ans:=ans+'('+y+')';
    81       ans:=ans+copy(s,a[i].ed+1,a[i+1].st-1-a[i].ed);
    82   end;
    83   exit(ans);
    84 end;
    85 
    86   begin
    87   fopen;
    88   readln(s);
    89   writeln(work(s));
    90   fclose;
    91 end.
  • 相关阅读:
    年末反思
    Flink运行时架构
    Phoenix 启动报错:Error: ERROR 726 (43M10): Inconsistent namespace mapping properties. Cannot initiate connection as SYSTEM:CATALOG is found but client does not have phoenix.schema.
    Clickhouse学习
    Flink简单认识
    IDEA无法pull代码到本地,Can't Update No tracked branch configured for branch master or the branch doesn't exist.
    第1章 计算机系统漫游
    简单的 Shell 脚本入门教程
    开源≠免费 常见开源协议介绍
    MySQL 视图
  • 原文地址:https://www.cnblogs.com/htfy/p/2738732.html
Copyright © 2011-2022 走看看