1 条题解

  • 0
    @ 2023-1-16 9:46:10

    大模拟。

    program Algebra;
    
    type
      TItem=record k:longint;p:string[10]; end;
      TAlg=array[1..50]of TItem;
    
    var
      s1,s2:string;
      p1,p2,l1,l2,n1,n2,m:integer;
      t,t1,t2:TAlg;
    
    function GetItem(var s:string;var ps,len:integer):TItem;
      var
        st,st1:string;
        it:TItem;
        o,code:integer;
      begin
        st:=s[ps];
        inc(ps);
        while (ps<=len)and(s[ps]<>'+')and(s[ps]<>'-') do
        begin
          st:=st+s[ps];
          inc(ps);
        end;
    
        o:=pos('x',st);
        if o=0
          then begin
            val(st,it.k,code);
            it.p:='';
            exit(it);
          end;
        if o=2
          then begin if st[1]='+' then it.k:=1 else it.k:=-1; end
          else begin
            st1:=copy(st,1,o-1);
            val(st1,it.k,code);
          end;
        it.p:=copy(st,o,255);
        exit(it);
      end;
    
    function cmp(a,b:string):boolean;
      begin
        if length(a)>length(b) then exit(true);
        if length(a)<length(b) then exit(false);
        exit(a>b);
      end;
    
    procedure solve;
      var
        ex:TItem;
        i,j:integer;
      begin
        //init
        p1:=1;l1:=length(s1);
        p2:=1;l2:=length(s2);
        n1:=0;n2:=0;
        while p1<=l1 do begin inc(n1); t1[n1]:=GetItem(s1,p1,l1); end;
        while p2<=l2 do begin inc(n2); t2[n2]:=GetItem(s2,p2,l2); end;
        //run
        m:=0;
        p1:=1;p2:=1;
        while (p1<=n1)and(p2<=n2) do
        begin
          inc(m);
          if t1[p1].p=t2[p2].p
            then begin
              t[m].p:=t1[p1].p;
              t[m].k:=t1[p1].k+t2[p2].k;
              inc(p1);inc(p2);
              continue;
            end;
          if cmp(t1[p1].p,t2[p2].p)
            then begin
              t[m]:=t1[p1];inc(p1);
            end
            else begin
              t[m]:=t2[p2];inc(p2);
            end;
        end;
        while p1<=n1 do begin
          inc(m);
          t[m]:=t1[p1];
          inc(p1);
        end;
        while p2<=n2 do begin
          inc(m);
          t[m]:=t2[p2];
          inc(p2);
        end;
      end;
    
    procedure Print;
      var
        i:integer;
        flag:boolean;
      begin
        flag:=true;
        for i:=1 to m do
        begin
          if t[i].k=0 then continue;
          flag:=false;
          if (t[i].k>0)and(i>1)
            then write('+');
          if t[i].p=''
            then write(t[i].k)
            else
          if t[i].k=-1
            then write('-')
            else
          if t[i].k<>1 then write(t[i].k);
          write(t[i].p);
        end;
        if flag then write(0);
        writeln;
      end;
    
    begin
      readln(s1);readln(s2);
      if (s1[1]<>'+')and(s1[1]<>'-') then s1:='+'+s1;
      if (s2[1]<>'+')and(s2[1]<>'-') then s2:='+'+s2;
      solve;
      Print;
    
      close(input);close(output);
    end.
    
    • 1

    信息

    ID
    30
    时间
    1000ms
    内存
    128MiB
    难度
    10
    标签
    递交数
    3
    已通过
    1
    上传者