zoukankan      html  css  js  c++  java
  • uoj98未来程序改 纯暴力不要想了

    暴力模拟A了,数据还是良(shui)心(shui)的

    90分的地方卡了半天最后发现一个局部变量被我手抖写到全局去了,,,

    心碎*∞

    没什么好解释的,其实只要写完表达式求值(带函数和变量的),然后处理一下高维数组

    给变量和函数各开一个map(事实上我给每一层都开了一个变量的map,每次都复制一下,但还是没有T也没有M)

    终于写完了

    记得有人立了个flag说我联赛前调不完这道题,终于拆了

      1 #include <bits/stdc++.h>
      2 #define st(now) ((isvar[now])?var[st[now]]:st[now])
      3 using namespace std;
      4 int In,input[1000],len,dep=0,funsum,retu,PA,read=0;bool RETU,debug;char ch;
      5 int varsum[100000],var[3000000],funstart[1000],pa[1000],pasum[1000],wei[3000000][20],weisum[3000000];
      6 string par[1000][100],code;
      7 set<char> s1;//实词集合(控制、函数、变量) 
      8 set<char> s2;//表达式字符集合 
      9 set<char> s3;//数字集合 
     10 map<string,int> m1[100000];//变量映射 
     11 map<string,int> m2;//函数映射 
     12 void init()//初始化 
     13 {
     14     freopen("1.in","r",stdin);
     15 //    freopen("1.out","w",stdout);
     16     scanf("%d",&In);
     17     for(int i=1;i<=In;i++)
     18         scanf("%d",&input[i]);
     19     bool flag=1;
     20     for(char ch=getchar();ch!=EOF;ch=getchar())
     21         if(flag && ch==';') flag=0;
     22             else if(!flag)    code+=ch;
     23     for(char ch='a';ch<='z';ch++) s1.insert(ch),s2.insert(ch);
     24     for(char ch='A';ch<='Z';ch++) s1.insert(ch),s2.insert(ch);
     25     for(char ch='0';ch<='9';ch++) s1.insert(ch),s2.insert(ch),s3.insert(ch);
     26     s1.insert('_');s2.insert('_');
     27     s2.insert('(');s2.insert(')');
     28     s2.insert('+');s2.insert('-');s2.insert('*');s2.insert('/');s2.insert('%');
     29     s2.insert('!');s2.insert('&');s2.insert('|');s2.insert('=');s2.insert('<');s2.insert('>');s2.insert('^');
     30     s2.insert('[');s2.insert(']');
     31     code.erase(0,1);
     32     for(int i=0;i<=code.length();)
     33         if((code[i]==' '||code[i]=='
    '||code[i]=='	')&&((!s1.count(code[i-1])) || (!s1.count(code[i+1]))))
     34             code.erase(i,1);
     35         else i++;
     36     varsum[0]=1;len=code.length();RETU=0;PA=0;
     37 }
     38 int level(int &now)//符号转成运算级 
     39 {
     40     switch(code[now])
     41     {
     42         case'<':if(code[++now]=='=')return 6;
     43                     else {now--;return 8;}
     44         case'>':if(code[++now]=='=')return 7;
     45                     else {now--;return 9;}
     46         case'=':if(code[++now]=='=')return 4;
     47                     else {now--;return 0;}
     48         case'!':now++;return 5;
     49         case'|':now++;return 1;
     50         case'&':now++;return 2;
     51         case'^':return 3;
     52         case'+':return 10;
     53         case'-':return 11;
     54         case'*':return 12;
     55         case'/':return 13;
     56         case'%':return 14;
     57     }
     58 }
     59 bool dayu(int a,int b)
     60 {
     61     if(a==0) return 0;
     62     if(a>=b) return 1;
     63     if((a==4)&&(b==5))return 1;
     64     if((a>=6)&&(a<=9)&&(b>=6)&&(b<=9)) return 1;
     65     if((a==10)&&(b==11)) return 1;
     66     if((a>=12)&&(a<=14)&&(b>=12)&&(b<=14)) return 1;
     67     return 0;
     68 }
     69 int word(int now)//找下一个单词(变量 int 函数之类的) 
     70 {
     71     int end;
     72     for(end=now;s1.count(code[end]);end++);
     73     return end;
     74 }
     75 int express(int now)//找下一个表达式 
     76 {
     77     int end;
     78     for(end=now;s2.count(code[end]);end++);
     79     return end;
     80 }
     81 int pass(int now)
     82 {
     83     while(code[now]!=';') now++;
     84     return now+1;
     85 }
     86 int block(int now)//跳过一段程序 
     87 {
     88     if(code[now]=='{')
     89     {
     90         int dep=1;
     91         for(now++;dep;now++) if(code[now]=='{') dep++;else if(code[now]=='}') dep--;
     92         return now;
     93     }
     94     else
     95     {
     96         int next=word(now);
     97         if((next-now==3 && code.substr(now,3)=="for")||(next-now==5 && code.substr(now,5)=="while"))
     98         {
     99             int p=next+1,deep=1;
    100             while(deep)
    101             {
    102                 if(code[p]=='(') deep++;
    103                 if(code[p]==')') deep--;
    104                 p++;
    105             }
    106             return block(p);
    107         }
    108         else
    109         if(next-now==2 && code.substr(now,2)=="if")
    110         {
    111             int p=next+1,deep=1;
    112             while(deep)
    113             {
    114                 if(code[p]=='(') deep++;
    115                 if(code[p]==')') deep--;
    116                 p++;
    117             }
    118             p=block(p);
    119             next=word(p);
    120             if(next-p==4 && code.substr(p,4)=="else")
    121                 return block(next);
    122             else return p;
    123         }
    124         else
    125         {
    126             int p=now;
    127             while(code[p]!=';') p++;
    128             return p+1;
    129         }
    130     }
    131 }
    132 int num(int &now)//得到最近的十进制数 
    133 {
    134     int sum=0;
    135     while(s3.count(code[now]))
    136         sum=sum*10+code[now++]-'0';
    137     return sum;
    138 }
    139 int getsize(int &now,int k)//得到数组的大小 
    140 {
    141     int sum=1;
    142     while(code[now]=='[')
    143     {
    144         now++;
    145         int tem=num(now);
    146         wei[k][++weisum[k]]=tem;
    147         sum*=tem;
    148         now++;
    149     }
    150     return sum;
    151 }
    152 int focus(int &next,int first)//得到所要的变量在数组中的位置 
    153 {
    154     int calc(int &now);
    155     int sum=0,SUM=1,i=0;
    156     if(code[next]=='[')
    157     {
    158         while(code[next]=='[')
    159         {
    160             next++;i++;
    161             sum+=SUM*calc(next);
    162             SUM*=wei[first][i];
    163             next++;
    164         }
    165     }
    166     return sum;
    167 }
    168 int newfun(int beg,int end)//定义新函数 
    169 {
    170     m2[code.substr(beg,end-beg)]=++funsum;int i=0;
    171     for(int next=word(++end);code[next]!=')';end=next+1,next=word(end))
    172         if(next-end!=3 || code.substr(end,3)!="int")
    173             par[funsum][++i]=code.substr(end,next-end);
    174     if(code[end]!=')') par[funsum][++i]=code.substr(end,word(end)-end);
    175     end=word(end);
    176     pasum[funsum]=i;
    177     funstart[funsum]=++end;end++;
    178     for(int dep=1;dep;end++) if(code[end]=='{') dep++;else if(code[end]=='}') dep--;
    179     return end;
    180 }
    181 int newvar(int beg)//定义新变量 
    182 {
    183     int end;
    184     for(end=beg;code[end]!=';';beg=end+1)
    185     {
    186         end=word(beg),m1[dep][code.substr(beg,end-beg)]=varsum[dep];
    187         int from=varsum[dep];weisum[varsum[dep]]=0;
    188         for(varsum[dep]+=getsize(end,varsum[dep]);from<=varsum[dep];from++) var[from]=0;
    189     }
    190     return end+1;
    191 }
    192 void newdep()//新的一层 
    193 {
    194     varsum[dep+1]=varsum[dep];m1[dep+1]=m1[dep];dep++;
    195 }
    196 int calc(int &now)//表达式求值 
    197 {
    198     int run(int now,bool isfun);
    199     int fh[20],st[20];bool isvar[20];int top=0,pre[10],presum=0;
    200     for(;code[now]!=';' && code[now]!=')' &&(code[now]!='<' || code[now+1]!='<')&&(code[now]!=']')&&(code[now]!=',');)
    201         if(s3.count(code[now]))
    202         {
    203             st[++top]=num(now);
    204             while(presum>0)
    205             {
    206                 if(pre[presum]==1) st[top]=!st[top];
    207                 if(pre[presum]==2) st[top]=-st[top];
    208                 presum--;
    209             }
    210             isvar[top]=0;fh[top]=-1;
    211         }
    212         else
    213         if(s1.count(code[now]))
    214         if(m1[dep].count(code.substr(now,word(now)-now)))
    215         {
    216             int next=word(now),first=m1[dep][code.substr(now,word(now)-now)];
    217             st[++top]=first+focus(next,first);isvar[top]=1;
    218             while(presum>0)
    219             {
    220                 st[top]=st(top);isvar[top]=0;
    221                 if(pre[presum]==1) st[top]=!st[top];
    222                 if(pre[presum]==2) st[top]=-st[top];
    223                 presum--;
    224             }
    225             fh[top]=-1;now=next;
    226         }
    227         else
    228         {
    229             int End=word(now),all=0,end=End+1,patem[100];
    230             while(code[end]!=')')
    231                 patem[++all]=calc(end),end+=code[end]==',';
    232             PA=m2[code.substr(now,End-now)];retu=0;
    233             for(int i=1;i<=all;i++)
    234                 pa[i]=patem[i];
    235             run(funstart[PA],1);
    236             st[++top]=retu;fh[top]=-1;isvar[top]=0;
    237             while(presum>0)
    238             {
    239                 if(pre[presum]==1) st[top]=!st[top];
    240                 if(pre[presum]==2) st[top]=-st[top];
    241                 presum--;
    242             }
    243             now=end+1;
    244         }
    245         else
    246         if(code[now]=='(')
    247         {
    248             now++;
    249             st[++top]=calc(now);
    250             while(presum>0)
    251             {
    252                 if(pre[presum]==1) st[top]=!st[top];
    253                 if(pre[presum]==2) st[top]=-st[top];
    254                 presum--;
    255             }
    256             isvar[top]=0;fh[top]=-1;now++;
    257         }
    258         else
    259         if(!top || fh[top]!=-1) 
    260         while(code[now]=='!' || code[now]=='-' || code[now]=='+')
    261         switch(code[now])
    262         {
    263             case'!':pre[++presum]=1;now++;break;
    264             case'-':pre[++presum]=2;now++;break;
    265             case'+':now++;break;
    266         }
    267         else
    268         {
    269             int newlevel=level(now);
    270             while(top>1 && dayu(fh[top-1],newlevel))
    271             switch(fh[--top])
    272             {
    273                 case 0:var[st[top]]=st(top+1);st[top]=st(top+1);isvar[top]=0;break;
    274                 case 1:st[top]=st(top)||st(top+1);isvar[top]=0;break;
    275                 case 2:st[top]=st(top)&&st(top+1);isvar[top]=0;break;
    276                 case 3:st[top]=st(top)^st(top+1);isvar[top]=0;break;
    277                 case 4:st[top]=st(top)==st(top+1);isvar[top]=0;break;
    278                 case 5:st[top]=st(top)!=st(top+1);isvar[top]=0;break;
    279                 case 6:st[top]=st(top)<=st(top+1);isvar[top]=0;break;
    280                 case 7:st[top]=st(top)>=st(top+1);isvar[top]=0;break;
    281                 case 8:st[top]=st(top)<st(top+1);isvar[top]=0;break;
    282                 case 9:st[top]=st(top)>st(top+1);isvar[top]=0;break;
    283                 case 10:st[top]=st(top)+st(top+1);isvar[top]=0;break;
    284                 case 11:st[top]=st(top)-st(top+1);isvar[top]=0;break;
    285                 case 12:st[top]=st(top)*st(top+1);isvar[top]=0;break;
    286                 case 13:st[top]=st(top)/st(top+1);isvar[top]=0;break;
    287                 case 14:st[top]=st(top)%st(top+1);isvar[top]=0;break;
    288             }
    289             fh[top]=newlevel;now++;
    290         }
    291     for(top--;top>0;top--)
    292     switch(fh[top])
    293     {
    294         case 0:var[st[top]]=st(top+1);st[top]=st(top+1);isvar[top]=0;break;
    295         case 1:st[top]=st(top)||st(top+1);isvar[top]=0;break;
    296         case 2:st[top]=st(top)&&st(top+1);isvar[top]=0;break;
    297         case 3:st[top]=st(top)^st(top+1);isvar[top]=0;break;
    298         case 4:st[top]=st(top)==st(top+1);isvar[top]=0;break;
    299         case 5:st[top]=st(top)!=st(top+1);isvar[top]=0;break;
    300         case 6:st[top]=st(top)<=st(top+1);isvar[top]=0;break;
    301         case 7:st[top]=st(top)>=st(top+1);isvar[top]=0;break;
    302         case 8:st[top]=st(top)<st(top+1);isvar[top]=0;break;
    303         case 9:st[top]=st(top)>st(top+1);isvar[top]=0;break;
    304         case 10:st[top]=st(top)+st(top+1);isvar[top]=0;break;
    305         case 11:st[top]=st(top)-st(top+1);isvar[top]=0;break;
    306         case 12:st[top]=st(top)*st(top+1);isvar[top]=0;break;
    307         case 13:st[top]=st(top)/st(top+1);isvar[top]=0;break;
    308         case 14:st[top]=st(top)%st(top+1);isvar[top]=0;break;
    309     }
    310     if(code[now]==';')
    311         now++;
    312     return st(1);
    313 }
    314 int sentence(int now)//执行语句
    315 {
    316     int run(int now,bool isfun);
    317     int next=word(now);
    318     if(code[now]=='{')
    319         next=run(now,0);
    320     else
    321     if(next-now==2 && code.substr(now,2)=="if")
    322     {
    323         newdep();
    324         next++;
    325         int end=express(next);
    326         if(calc(next))
    327         {
    328             next++;next=sentence(next);
    329             int end=word(next);
    330             if(end-next==4 && code.substr(next,4)=="else")
    331                 next=block(end);
    332         }
    333         else
    334         {
    335             next++;
    336             next=block(next);
    337             int end=word(next);
    338             if(end-next==4 && code.substr(next,4)=="else")
    339                 next=sentence(end+(code[end]==' '));
    340         }
    341         dep--;
    342     }
    343     else
    344     if(next-now==3 && code.substr(now,3)=="for")
    345     {
    346         newdep();
    347         int judge;
    348         judge=(code[next+1]==';')?next+2:sentence(next+1);int ju=judge;
    349         if(RETU) return next;
    350         int done=express(judge)+1;
    351         int blo=done,deep=1;
    352         while(deep)
    353         {
    354             if(code[blo]=='(') deep++;
    355             if(code[blo]==')') deep--;
    356             blo++;
    357         }
    358         while((code[ju]==';')?1:calc(ju))
    359         {
    360             ju=judge,sentence(blo);
    361             if(RETU) return next;
    362             sentence(done);
    363             if(RETU) return next;
    364         }
    365         next=block(blo);
    366         dep--;
    367     }
    368     else
    369     if(next-now==5 && code.substr(now,5)=="while")
    370     {
    371         newdep();
    372         int judge=next+1,ju=judge;
    373         if(RETU) return next;
    374         int blo=judge,deep=1;
    375         while(deep)
    376         {
    377             if(code[blo]=='(') deep++;
    378             if(code[blo]==')') deep--;
    379             blo++;
    380         }
    381         while(calc(ju))
    382         {
    383             sentence(blo),ju=judge;
    384             if(RETU) return next;
    385         }
    386         next=block(blo);
    387         dep--;
    388     }
    389     else
    390     if(next-now==3 && code.substr(now,3)=="cin")
    391     {
    392         int qian=next,hou=next+1;
    393         while(code[qian]!=';')
    394         {
    395             qian=word(hou+1);
    396             int first=m1[dep][code.substr(hou+1,qian-hou-1)];
    397             var[first+focus(qian,first)]=input[++read];
    398             hou=qian+1;
    399         }
    400         next=hou;
    401     }
    402     else
    403     if(next-now==4 && code.substr(now,4)=="cout")
    404     {
    405         int qian=next,hou=next+1;
    406         while(code[qian]!=';')
    407         {
    408             qian=hou+1;
    409             while((code[qian]!='<' || code[qian+1]!='<' )&&(code[qian]!=';')) qian++;
    410             if(qian-hou==5 && code.substr(hou+1,4)=="endl")
    411                 puts("");
    412             else
    413                 hou++,printf("%d",calc(hou));
    414             hou=qian+1;
    415         }
    416         next=hou;
    417     }
    418     else
    419     if(next-now==7 && code.substr(now,7)=="putchar")
    420         next++,printf("%c",calc(next)),next+=2;
    421     else
    422     if(next-now==3 && code.substr(now,3)=="int")
    423         next=newvar(next+1);
    424     else
    425     if(next-now==6 && code.substr(now,6)=="return")
    426         next++,retu=calc(next),RETU=1;
    427     else
    428     {
    429         calc(now);
    430         next=now;
    431     }
    432     return next;
    433 }
    434 int run(int now,bool isfun)//从某个{开始运行 
    435 {
    436     newdep();now++;
    437     if(PA)
    438         for(int i=1;i<=pasum[PA];i++)
    439             m1[dep][par[PA][i]]=varsum[dep],var[varsum[dep]]=pa[i],varsum[dep]++;
    440     PA=0;
    441     for(int next=now;code[next]!='}'&& !RETU;now=next)
    442         next=sentence(now);
    443     dep--;if(isfun)RETU=0;now++;
    444     return now;
    445 }
    446 int main()
    447 {
    448     init();
    449     for(int i=0,j;i<len;i=j)//处理全局变量和函数 
    450     {
    451         i=word(i)+1;j=word(i);//过滤int
    452         if(code[j]=='(')    j=newfun(i,j);
    453         else    j=newvar(i);
    454     }
    455     run(funstart[m2["main"]],1);
    456     return 0;
    457  } 

    也不是很长啊(^o^)/~

  • 相关阅读:
    实验吧因缺思汀的绕过注入解析
    python模块中requests参数stream
    土司论坛nc反弹神器使用方法
    msf web脚本反弹shell
    python 多线程
    metasploit后门维持技术
    ew做socks5代理
    双内网渗透代理之reGeorg+Proxifier
    SVN源码泄露漏洞
    Java反射的实例
  • 原文地址:https://www.cnblogs.com/wanglichao/p/5974880.html
Copyright © 2011-2022 走看看