NOIP2011T4 表达式的值

 

 

type
    pair=array[0..1]of longint;
var
    l,i,j,kh:longint;
    ans,zero:pair;
    st:array[1..50000]of longint;
    dis:array[1..100000]of longint;
    ch:array[1..100000]of char;
function f(l,r:longint):pair;
var i:longint; x,y:pair;
begin
    if l>r then exit(zero);
    i:=r;
    while i>=l do
    begin
        case ch[i] of
            '+':break;
            ')':i:=dis[i];
        end;
        dec(i);
    end;
    if i<l then
    begin
        i:=r;
        while i>=l do
        begin
            case ch[i] of
                '*': break;
                ')': i:=dis[i];
            end;
            dec(i);
        end;
    end;
    if i<l then exit(f(l+1,r-1));
    x:=f(l,i-1);
    y:=f(i+1,r);
    if ch[i]='*' then
    begin
        f[0]:=(x[0]*y[0]+x[0]*y[1]+x[1]*y[0]) mod 10007;
        f[1]:=(x[1]*y[1]) mod 10007;
    end;
    if ch[i]='+' then
    begin
        f[0]:=x[0]*y[0] mod 10007;
        f[1]:=(x[0]*y[1]+x[1]*y[0]+x[1]*y[1]) mod 10007;
    end;
end;

begin
    assign(input,'exp.in'); reset(input);
    assign(output,'exp.out'); rewrite(output);
    readln(l);
    kh:=0;
    for i:=1 to l do read(ch[i]);
    for i:=l downto 1 do
        case ch[i] of
            ')': begin inc(kh); st[kh]:=i; end;
            '(': begin dis[st[kh]]:=i; dec(kh); end;
        end;
    zero[0]:=1; zero[1]:=1;
    ans:=f(1,l);
    writeln(ans[0]);
    close(input); close(output);
end.

 

转载于:https://www.cnblogs.com/qilinart/articles/3384663.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值