NOIP 2017 提高组 时间复杂度___模拟

题目大意;

这里写图片描述
这里写图片描述这里写图片描述
这里写图片描述
这里写图片描述

题解:

这题显示开一个栈模拟即可,
只需要考虑5种情况:
1.x为正整数,y为正整数2种情况
①x>y,它其中的循环计为无效
②x≤y,算为常数复杂度,继承上一层的复杂度

③x为正整数,y为n时,计为一层循环
④x为n,y为正整数,即和①一样,其中循环无效
⑤x为n,y为n,依然是常数复杂度,继承上一层

然后注意细节吧,这题主要是要注意很多乱七八糟的东东

代码:

var
        rp:array [0..100001] of char;
        cp:array [0..100001] of longint;
        a:array ['a'..'z'] of boolean;
        tp,op,ans,w,x1,x2,t,i,j,n,m,k1,k2:longint;
        check:boolean;
        x3,x4,s:string;

begin
        readln(t);
        while t>=1 do
          begin
                readln(s);
                w:=pos(' ',s); val(copy(s,1,w-1),x1); delete(s,1,w);
                if pos('^',s)<>0 then w:=pos('^',s)
                                 else w:=0;
                if w<>0 then val(copy(s,w+1,length(s)-w-1),x2)
                        else x2:=0;
                ans:=0;
                fillchar(a,sizeof(a),false);
                op:=0;
                tp:=0;
                check:=false;
                for i:=1 to x1 do
                  begin
                        readln(s);

                        if check then continue;


                        if s[1]='F' then
                              begin
                                   if not(a[s[3]])
                                     then begin
                                               a[s[3]]:=true;
                                               inc(op);
                                               rp[op]:=s[3];
                                          end
                                     else check:=true;
                                   if check then continue;
                                   delete(s,1,4);
                                   k1:=pos(' ',s);
                                   x3:=copy(s,1,k1-1);
                                   delete(s,1,k1);
                                   x4:=s;
                                   if (x3='n') and (x4<>'n') then begin tp:=tp+1; cp[op]:=-1; end
                                      else if (x3='n') and (x4='n') then cp[op]:=cp[op-1]
                                              else if (x3<>'n') and (x4='n') then cp[op]:=cp[op-1]+1
                                                      else begin
                                                                 val(x3,k1);
                                                                 val(x4,k2);
                                                                 if k1>k2 then begin tp:=tp+1; cp[op]:=-1 end
                                                                          else cp[op]:=cp[op-1];
                                                           end;
                                   if tp=0 then
                                      if cp[op]>ans then ans:=cp[op];

                              end
                              else if s[1]='E' then
                                      begin
                                         if op=0 then check:=true
                                                 else begin
                                                            a[rp[op]]:=false;
                                                            if (cp[op]=-1) and (tp>0) then dec(tp);
                                                            dec(op);
                                                      end;
                                      end;
                   end;
                if op<>0 then check:=true;
                if check then writeln('ERR')
                         else if (ans=x2) then writeln('Yes')
                                          else writeln('No');
                dec(t);
          end;
end.



阅读更多

更多精彩内容