跳到正文

个人记录

a+b可怕题解

yyhhenry约 11 分钟
原文
文章信息与版本

文章信息

收录
2026年3月28日
更新
2026年3月28日
篇幅
约 11 分钟

版本

  1. v1当前

又一个可怕的a+b题解横空出世,详见P1001和P3695,这个是本人P3695的题解跑通P1001的例子

C++
    #include <iostream>
    #include <algorithm>
    #include <stdio.h>
    #include <string.h>
    #include <string>
    #include <map>
    using namespace std;
    map<string,int>num;
    map<string,map<int,int> >arr;
    char pro[503][10003],q[10003];
    int n,x,y,v[500003],vt=0;
    void run();
    inline string gts();
    inline char gtf();
    inline int gtn();
    int gti();
    int exp();
    bool pd();
    void run() {
        y=1;
        char c=gtf();
        int q,q1,q2,x1,y1,x2;
        string s,s1;
        if(c==':') {
            y++;
            s=gts();
            if(s=="set") {
                x1=x+1;
                q=gti();
                gtf();
                y++;
                v[q]=exp();
                x=x1;
                y=1;
            } else if(s=="yosoro") {
                x1=x+1;
                printf("%d ",exp());
                x=x1;
                y=1;
            }
        } else if(c=='{') {
            y++;
            x2=x+1;
            s=gts();
            if(s=="ihu") {
                if(pd()) {
                    x=x2;
                    y=1;
                    while(gtf()!='}')run();
                    x++;
                    y=1;
                } else {
                    x=x2;y=1;
                    for(x1=0; x1>=0;) {
                        c=gtf();
                        if(c=='{')x1++;
                        if(c=='}')x1--;
                        x++;
                        y=1;
                    }
                }
            } else if(s=="hor") {
                q=gti();
                gtf();
                y++;
                q1=exp();
                gtf();
                y++;
                q2=exp();
                x=x2;
                y=1;
                for(v[q]=q1; v[q]<=q2; v[q]++) {
                    while(gtf()!='}')run();
                    x=x2;
                    y=1;
                }
                for(x1=0; x1>=0;) {
                    c=gtf();
                    if(c=='{')x1++;
                    if(c=='}')x1--;
                    if(c==0)continue;
                    x++;
                    y=1;
                }
            } else if(s=="while") {
                x1=x;
                y1=y;
                while(pd()) {
                    x=x2;
                    y=1;
                    while(gtf()!='}')run();
                    x=x1;
                    y=y1;
                }
                x=x2;
                y=1;
                for(x1=0; x1>=0;) {
                    c=gtf();
                    if(c=='{')x1++;
                    if(c=='}')x1--;
                    if(c==0)continue;
                    x++;
                    y=1;
                }
            } else if(s=="vars") {
                x=x2;
                y=1;
                while(gtf()!='}') {
                    x1=x+1;
                    s=gts();
                    gtf();
                    y++;
                    s1=gts();
                    if(s1=="int") {
                        num[s]=++vt;
                    } else {
                        gtf();
                        y++;
                        gts();
                        gtf();
                        y++;
                        q1=gtn();
                        gtf();
                        y++;
                        gtf();
                        y++;
                        q2=gtn();
                        arr[s];
                        map<string,map<int,int> >::iterator qq=arr.find(s);
                        for(int i=q1; i<=q2; i++)(qq->second)[i]=++vt;
                    }
                    x=x1;
                    y=1;
                }
                x++;
                y=1;
            }
        }
    }
    inline char gtf() {
        while(1) {
            if(!pro[x][y]) {
                if(y>0){x++;
                y=0;
                return 0;}
                y++;
            } else if(pro[x][y]==' ')y++;
            else break;
        }
        return pro[x][y];
    }
    inline string gts() {
        char c=gtf();
        int i;
        for(i=0; c>='a'&&c<='z'; i++,c=pro[x][++y])q[i]=c;
        q[i]=0;
        return string(q);
    }
    inline int gtn() {
        int a=0;
        while(!isdigit(gtf()))y++;
        while(isdigit(gtf())) {
            a=a*10+gtf()-'0';
            y++;
        }
        return a;
    }
    int gti() {
        string s=gts();
        int q;
        if(gtf()=='[') {
            y++;
            q=exp();
            gtf();
            y++;
            return arr[s][q];
        } else return num[s];
    }
    int exp() {
        int ans=0,q;char c=gtf(),d;
        if(isdigit(c))ans=gtn();
        else if(c>='a'&&c<='z')ans=v[gti()];
        c=gtf();
        while(c=='+'||c=='-'){
            y++;d=gtf();
            if(isdigit(d))q=gtn();
            else if(d>='a'&&d<='z')q=v[gti()];
            if(c=='+')ans+=q;else ans-=q;
            c=gtf();
        }
        return ans;
    }
    bool pd() {
        string s=gts();gtf();y++;
        int q1=exp();gtf();y++;int q2=exp();
        if(s=="lt")return q1<q2;
        if(s=="gt")return q1>q2;
        if(s=="le")return q1<=q2;
        if(s=="ge")return q1>=q2;
        if(s=="eq")return q1==q2;
        if(s=="neq")return q1!=q2;
    }
    int main() {
        int xx,yy;cin>>xx>>yy;
        sprintf(pro[1]+1,"{ vars");
        sprintf(pro[2]+1,"a:int");
        sprintf(pro[3]+1,"b:int");
        sprintf(pro[4]+1,"}");
        sprintf(pro[5]+1,":set a, %d",xx);
        sprintf(pro[6]+1,":set b, %d",yy);
        sprintf(pro[7]+1,":yosoro a+b");
        n=7;x=1;y=1;
        for(; x<=n;)run();
        return 0;
}
原文