1 条题解

  • 0
    @ 2021-6-15 1:40:12

    C++ :

    #include<iostream>
    #include<stack>
    
    #define yxj_equal sc.top()=='('&&text[i]==')'
    #define yxj_low (sc.top()=='('&&text[i]!=')')||(sc.top()=='*'&&text[i]=='(')||(sc.top()=='+'&&(text[i]=='('||text[i]=='*'))
    using namespace std;
     
    const int MOD = 10007;
    char text[100010];
    int len;
    stack<int> s0, s1;
    stack<int> sc;
     
    void init()
    {
         cin >> len;
         cin >> text;
         while (!s0.empty()) s0.pop();
         while (!s1.empty()) s1.pop();
         while (!sc.empty()) sc.pop();
         text[len++] = ')';
         text[len] = '\0';
         sc.push('('); s0.push(1); s1.push(1);
    }
     
    int main()
    {
        init();
        int i = 0;
        while (i < len)
        {
              if (yxj_equal)
              {
                  sc.pop();
                  i++;
              }
              else
              {
                  if (yxj_low)
                  {
                      sc.push(text[i]);
                      if (text[i] != '(')
                      {
                          s0.push(1);
                          s1.push(1);
                      }
                      i++;
                  }
                  else
                  {
                      if (!yxj_low)
                      {
                          int a0 = s0.top(), a1 = s1.top(), rec0, rec1;
                          s0.pop(); s1.pop();
                          int b0 = s0.top(), b1 = s1.top();
                          s0.pop(); s1.pop();
                          char ch = sc.top();
                          sc.pop();
                          if (ch == '+')
                          {
                              rec0 = a0*b0%MOD;
                              rec1 = (a1*b0%MOD+a0*b1%MOD+a1*b1%MOD)%MOD;
                          }
                          else
                          {
                              rec1 = a1*b1%MOD;
                              rec0 = (a1*b0%MOD+a0*b1%MOD+a0*b0%MOD)%MOD;
                          }
                          s0.push(rec0);
                          s1.push(rec1);
                      }
                  }
              }
        }
        cout << s0.top() << endl;
        return 0;
    }
    

    Pascal :

    var yx:array[chr(1)..chr(127)]of longint;
        st,s:array[0..1000000]of char;
        f:array[0..1000000,0..1]of longint;
        top,top2,i,j,n:longint;
        LAST:string;
    procedure pop;
    begin
      if st[top]='+' then
      begin
        f[top2,1]:=f[top2-1,0]*f[top2,1]+f[top2-1,1]*f[top2,0]+f[top2-1,1]*f[top2,1];
        f[top2,1]:=f[top2,1] mod 10007;
        f[top2,0]:=f[top2-1,0]*f[top2,0];
        f[top2,0]:=f[top2,0]mod 10007;
      end
      else
      begin
        f[top2,0]:=f[top2-1,0]*f[top2,1]+f[top2-1,1]*f[top2,0]+f[top2-1,0]*f[top2,0];
        f[top2,0]:=f[top2,0] mod 10007;
        f[top2,1]:=f[top2-1,1]*f[top2,1];
        f[top2,1]:=f[top2,1]mod 10007;
      end;
      dec(top);
      f[top2-1,1]:=f[top2,1];
      f[top2-1,0]:=f[top2,0];
      dec(top2);
    end;
    begin
      yx['*']:=2; yx['+']:=1; yx['(']:=1;
      st[0]:='@';
      readln(n);
      for i:=1 to n do read(s[i]);
      for i:=1 to n do
      begin
        if(s[i]<>'(')and(last<>')')then
        begin
          inc(top2);
          f[top2,1]:=1;
          f[top2,0]:=1;
        end;
        if(s[i]='+')or(s[i]='*')then
        begin
          while yx[s[i]]<yx[st[top]] do pop;
          inc(top);
          st[top]:=s[i];
          last:='';
        end
        else
        if s[i]=')' then
        begin
          while st[top]<>'(' do begin pop; end;
          dec(top);
          last:=')';
        end
        else
        begin
          inc(top);
          st[top]:=s[i];
          last:='';
        end;
      end;
      if last<>')' then
      begin
        inc(top2);
        f[top2,1]:=1;
        f[top2,0]:=1;
      end;
      while top>0 do pop;
      write(f[1,0]);
    end.
    
    • 1

    信息

    ID
    287
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者