首页 > 其他分享 >UVA12422 (Kengdie) Mua (II) - Expression Evaluator 题解

UVA12422 (Kengdie) Mua (II) - Expression Evaluator 题解

时间:2024-02-25 12:33:28浏览次数:30  
标签:mnum return Name cl 题解 clear II UVA12422 Memory

题目传送门

闲话

蒟蒻的第一篇黑题题解!

连着花了 \(12\) 个小时才做出来,打代码 \(6\) 小时,调试 \(6\) 小时。

一开始怎么编也编不过,直到看到了 tiger 大神的题解才豁然开朗。

思路

本题主要是输出函数或运算式子的结果,最重要的就是判断优先级。

tiger 大神提出了表达式树法和递归下降法两种,蒟蒻的我不会表达式树法,于是就用递归下降法来做。

这里判断优先级的方法与 tiger 大神一样,在递归中多传一个参数来表示式子中的最小运算符优先级,每次到最后递归时将这个参数加一,直到加到参数最大——这里是 ^ 的优先级最大时再分类运算即可。

本题中的 table 是一种类似 C++ 的 map 的结构,我们就可以开一个 map 来存储它。

当然在输入时我们也需要像 UVA12421 那样把名称、保留字、数字、字符等都分割开以便后续计算。

在处理函数时,需要找到配对的括号,还是比较容易实现的。

还要注意几点:

  1. 多测要清空!

  2. 类型名和函数名不要打错!因为这个我调了至少 \(2\) 小时。

  3. 小数要保留到 \(14\) 位。

  4. 小心动态数组下标越界。

代码

#include<bits/stdc++.h>
#define MAX 100005
using namespace std;
string RESERVED[25]={"and","break","do","else","elseif","end","false","for","function","if","in","local","nil","not","or","repeat","return","then","true","until","while"};
string FUNCTION[35]={"tonumber","tostring","print","string","rep","sub","table","concat","sort","math","abs","floor","ceil","sqrt","exp","log","log10","pi","rad","deg","acos","asin","atan","atan2","cos","sin","tan","max","min"};
string str,ans;
int cnt,len,j,pm[MAX],tot;
bool pd;
struct Operator{
    string Name,val;
    bool operator == (const Operator & A)const{
        return A.Name ==Name && A.val==val;
    }
};
string changestring(double x){
    string tmp;
    char ch[MAX]={};
    memset(ch,0,sizeof(ch));
    sprintf(ch,"%.14g",x);
    tmp=ch;
    return tmp;
}
string dealstring(string tmp){
    string ANS="";
    for(int i=1;i<tmp.length();i++){
        if(!tmp[i+1])break;
        if(tmp[i]!='\\')ANS+=tmp[i];
        else{
            i++;
            if(tmp[i]=='\"')ANS+='\"';
            if(tmp[i]=='\'')ANS+='\'';
            if(tmp[i]=='\\')ANS+='\\';
            if(tmp[i]=='n')ANS+='\n';
        }
    }
    return ANS;
}
struct Memory{
    string Name,mstr;
    double mnum;
    int mloc;
    map<Memory,int> mp;
    bool operator < (const Memory & A)const{
        if(Name!=A.Name)return Name<A.Name;
        if(mstr!=A.mstr)return mstr<A.mstr;
        if(mnum!=A.mnum)return mnum<A.mnum;
        if(mloc!=A.mloc)return mloc<A.mloc;
        return mp<A.mp;
    }
};
vector<Operator> opt;
vector<Memory> vm;
stack<int> st;
map<string,int> Store;
bool checknum(int x){
    char ch=str[x];
    if(ch>='0'&&ch<='9'||ch=='e'||ch>='A'&&ch<='F'||ch>='a'&&ch<='f')return 1;
    if(ch=='-'||ch=='+')if(str[x-1]=='e')return 1;else return 0;
    if(ch=='.'&&!pd){pd=1;return 1;}
    return 0;
}
bool checkstring(int x,char tmp){
    char ch1=str[x],ch2=str[x-1];
    if(tmp!=ch1)return 1;
    else{if(ch2=='\\')return 1;else return 0;}
    return 0;
}
int checksymbol(int x){
    char ch1=str[x],ch2=str[x+1];
    if(ch1=='.'&&ch2=='.'&&str[x+2]=='.')return 3;
    if(ch1=='.'&&ch2=='.'||ch1=='='&&ch2=='='||ch1=='>'&&ch2=='='||ch1=='<'&&ch2=='='||ch1=='~'&&ch2=='=')return 2;
    /*+ - * / % ^ # == >= <= > < ~= ( ) { } [ ] ; : , . .. ... =*/
    if(ch1=='.'||ch1==','||ch1==':'||ch1==';'||ch1=='='||ch1=='+'||ch1=='-'||ch1=='*'||ch1=='/'||ch1=='%'||ch1=='^'||ch1=='#'||ch1=='<'||ch1=='>'||ch1=='('||ch1==')'||ch1=='{'||ch1=='}'||ch1=='['||ch1==']')return 1;
    return 0;
}
int Priority(string tmp){
    if(tmp=="or")return 1;
    if(tmp=="and")return 2;
    if(tmp==">"||tmp=="<"||tmp==">="||tmp=="<="|tmp=="~="||tmp=="==")return 3;
    if(tmp=="..")return 4;
    if(tmp=="+" || tmp=="-")return 5;
    if(tmp=="*"|| tmp=="/"||tmp=="%")return 6;
    if(tmp=="not"||tmp=="#")return 7;
    if(tmp=="^")return 8;
    if(tmp=="=")return 0;
    return -1;
}
bool checkname(int x){
    char ch=str[x];
    if(ch>='0'&&ch<='9'||ch>='a'&&ch<='z'||ch>='A'&&ch<='Z'||ch=='_')return 1;
    return 0;
}
bool checkreserved(string s){
    for(int i=0;i<21;i++)if(s==RESERVED[i])return 1;
    return 0;
}
bool checkmemory(int l,int r){
    if(l==r||(l==r-1&&opt[l].val=="{"))return 1;l++;
    while(l<=r&&(opt[l].val=="["||opt[l].val=="."))if(opt[l].val==".")l+=2;else l=pm[l]+1;
    if(l==r+1)return 1;
    return 0;
}
bool checkfunction(string s){
    for(int i=0;i<29;i++)if(s==FUNCTION[i])return 1;
    return 0;
}
bool checknumber(string s){
    if(s[0]=='+'||s[0]=='-')s=s.substr(1);
    if(s[0]=='0'&&(s[1]=='x'||s[1]=='X')){
        for(int i=2;i<s.length();i++)if(!(s[i]>='0'&&s[i]<='9'||s[i]>='A'&&s[i]<='F'||s[i]>='a'&&s[i]<='f'))return 0;
        return 1;
    }
    bool pd1=0,pd2=0;
    for(int i=0;i<s.length();i++){
        if(s[i]>='0'&&s[i]<='9')continue;
        else if(s[i]=='.'){
            if(!pd1&&(i!=0||s[i+1]>='0'&&s[i+1]<='9'))pd1=1;
            else return 0;
        }else if(!pd2||s[i]=='e'||s[i]=='E'){
            pd2=1;
            if(s[i+1]=='+'||s[i+1]=='-')i++;
            pd1=1;
        }else return 0;
    }
    return 1;
}
double Table(Memory tmp){
    double tmpi=1.0;
    while(1){
        Memory cl={"number","",tmpi,0};cl.mp.clear();
        if(!tmp.mp[cl])break;
        tmpi++;
    }
    return tmpi-1;
}
double changedou(string tmp){
    double tmpd=0,dou=0.1;
    bool pd=0;
    if(tmp[0]=='+')return changedou(tmp.substr(1));
    else if(tmp[0]=='-') return -1*changedou(tmp.substr(1));
    for(int i=0;i<tmp.length();i++){
        if(tmp[i]=='.')pd=1;
        else if(tmp[i]=='e'||tmp[i]=='E'){
            int PoN=1,ANS=0;
            if(tmp[++i]=='-')PoN=-1;
            i++;
            while(tmp[i]>='0'&&tmp[i]>='9')ANS=ANS*10+tmp[i]-'0',i++;
            ANS*=PoN;
            tmpd*=pow(10,ANS);
            return tmpd;
        }else if(pd)tmpd+=dou*(tmp[i]-'0')*1.0,dou/=10;
        else tmpd=tmpd*10+(tmp[i]-'0')*1.0;
    }
    return tmpd;
}
double changehex(string tmp){
    double tmpd=0;
    for(int i=0;i<tmp.length();i++){
        if(tmp[i]>='a'&&tmp[i]<='f')tmpd=tmpd*16+tmp[i]-'a'+10;
        else if(tmp[i]>='A'&&tmp[i]<='F')tmpd=tmpd*16+tmp[i]-'A'+10;
        else tmpd=tmpd*16+tmp[i]-'0';
    }
    return tmpd;
}
double changenum(string tmp){
    if(tmp[0]=='0'&&(tmp[1]=='x'||tmp[1]=='X'))return changehex(tmp.substr(2));
    return changedou(tmp);
}
string STRING(Memory tmp){
    if(tmp.Name=="string")return tmp.mstr;
    else if(tmp.Name=="number")return changestring(tmp.mnum);
    else if(tmp.Name=="boolean")return tmp.mnum?"true":"false";
    else return tmp.Name;
}
Memory VAR(Memory tmp){
    if(tmp.Name!="var")return tmp;
    return vm[tmp.mloc];
}
Memory dealsymbol(Operator op,Memory m1,Memory m2){
    if(op.val=="")return m2;
    if(op.val!="=") m1=VAR(m1);
    m2=VAR(m2);
    if(op.val=="or"){
        Memory cl;cl.Name="boolean",cl.mstr="";cl.mloc=0;cl.mp.clear();
        if(m1.mnum!=0.0||m2.mnum!=0.0)cl.mnum=1;else cl.mnum=0;
        return cl;
    }else if(op.val=="and"){
        Memory cl;cl.Name="boolean",cl.mstr="";cl.mloc=0;cl.mp.clear();
        if(m1.mnum!=0.0&&m2.mnum!=0.0)cl.mnum=1;else cl.mnum=0;
        return cl;
    }else if(op.val=="not"){
        Memory cl;cl.Name="boolean",cl.mstr="";cl.mloc=0;cl.mp.clear();
        if(m2.mnum==0.0)cl.mnum=1;else cl.mnum=0;
        return cl;
    }else if(op.val=="<"){
        if(m1.Name=="boolean"||m1.Name=="number"){
            Memory cl;cl.Name="boolean",cl.mstr="";cl.mloc=0;cl.mp.clear();
            if(m1.mnum<m2.mnum)cl.mnum=1;else cl.mnum=0;
            return cl;
        }else{
            Memory cl;cl.Name="boolean",cl.mstr="";cl.mloc=0;cl.mp.clear();
            if(m1.mstr<m2.mstr)cl.mnum=1;else cl.mnum=0;
            return cl;
        }
    }else if(op.val==">"){
        if(m1.Name=="boolean"||m1.Name=="number"){
            Memory cl;cl.Name="boolean",cl.mstr="";cl.mloc=0;cl.mp.clear();
            if(m1.mnum>m2.mnum)cl.mnum=1;else cl.mnum=0;
            return cl;
        }else{
            Memory cl;cl.Name="boolean",cl.mstr="";cl.mloc=0;cl.mp.clear();
            if(m1.mstr>m2.mstr)cl.mnum=1;else cl.mnum=0;
            return cl;
        }
    }else if(op.val=="<="){
        if(m1.Name=="boolean"||m1.Name=="number"){
            Memory cl;cl.Name="boolean",cl.mstr="";cl.mloc=0;cl.mp.clear();
            if(m1.mnum<=m2.mnum)cl.mnum=1;else cl.mnum=0;
            return cl;
        }else{
            Memory cl;cl.Name="boolean",cl.mstr="";cl.mloc=0;cl.mp.clear();
            if(m1.mstr<=m2.mstr)cl.mnum=1;else cl.mnum=0;
            return cl;
        }
    }else if(op.val==">="){
        if(m1.Name=="boolean"||m1.Name=="number"){
            Memory cl;cl.Name="boolean",cl.mstr="";cl.mloc=0;cl.mp.clear();
            if(m1.mnum>=m2.mnum)cl.mnum=1;else cl.mnum=0;
            return cl;
        }else{
            Memory cl;cl.Name="boolean",cl.mstr="";cl.mloc=0;cl.mp.clear();
            if(m1.mstr>=m2.mstr)cl.mnum=1;else cl.mnum=0;
            return cl;
        }
    }else if(op.val=="=="){
        if(m1.Name=="boolean"||m1.Name=="number"){
            Memory cl;cl.Name="boolean",cl.mstr="";cl.mloc=0;cl.mp.clear();
            if(m1.mnum==m2.mnum)cl.mnum=1;else cl.mnum=0;
            return cl;
        }else{
            Memory cl;cl.Name="boolean",cl.mstr="";cl.mloc=0;cl.mp.clear();
            if(m1.mstr==m2.mstr)cl.mnum=1;else cl.mnum=0;
            return cl;
        }
    }else if(op.val=="~="){
        if(m1.Name=="boolean"||m1.Name=="number"){
            Memory cl;cl.Name="boolean",cl.mstr="";cl.mloc=0;cl.mp.clear();
            if(m1.mnum!=m2.mnum)cl.mnum=1;else cl.mnum=0;
            return cl;
        }else{
            Memory cl;cl.Name="boolean",cl.mstr="";cl.mloc=0;cl.mp.clear();
            if(m1.mstr!=m2.mstr)cl.mnum=1;else cl.mnum=0;
            return cl;
        }
    }else if(op.val==".."){
        Memory cl;cl.Name="string",cl.mstr=m1.mstr+m2.mstr,cl.mnum=0,cl.mloc=0;cl.mp.clear();
        return cl;
    }else if(op.val=="+"){
        Memory cl;cl.Name="number",cl.mstr="",cl.mnum=m1.mnum+m2.mnum,cl.mloc=0;cl.mp.clear();
        return cl;
    }else if(op.val=="-"){
        if(m1.Name=="nil"){
            Memory cl;cl.Name="number",cl.mstr="",cl.mnum=-m2.mnum,cl.mloc=0;cl.mp.clear();
            return cl;
        }else{
            Memory cl;cl.Name="number",cl.mstr="",cl.mnum=m1.mnum-m2.mnum,cl.mloc=0;cl.mp.clear();
            return cl;
        }
    }else if(op.val=="*"){
        Memory cl;cl.Name="number",cl.mstr="",cl.mnum=m1.mnum*m2.mnum,cl.mloc=0;cl.mp.clear();
        return cl;
    }else if(op.val=="/"){
        Memory cl;cl.Name="number",cl.mstr="",cl.mnum=m1.mnum/m2.mnum,cl.mloc=0;cl.mp.clear();
        return cl;
    }else if(op.val=="%"){
        Memory cl;cl.Name="number",cl.mstr="",cl.mnum=m1.mnum-floor(m1.mnum/m2.mnum)*m2.mnum,cl.mloc=0;
        cl.mp.clear();
        return cl;
    }else if(op.val=="#"){
        if(m2.Name=="string"){
            Memory cl;cl.Name="number",cl.mstr="",cl.mnum=m2.mstr.size(),cl.mloc=0,cl.mp.clear();
            return cl;
        }else{
            Memory cl;cl.Name="number",cl.mstr="",cl.mnum=Table(m2),cl.mloc=0,cl.mp.clear();
            return cl;
        }
    }else if(op.val=="^"){
        Memory cl;cl.Name="number",cl.mstr="",cl.mnum=pow(m1.mnum,m2.mnum),cl.mloc=0,cl.mp.clear();
        return cl;
    }else if(op.val=="="){
        if(m1.Name=="var")vm[m1.mloc]=VAR(m2);
        return m1;
    }
    Memory cl;cl.Name="nil",cl.mstr="",cl.mnum=0,cl.mloc=0,cl.mp.clear();
    return cl;
}
Memory dealfunction(string type,vector<Memory> v){
    if(type!="table.sort")v[0]=VAR(v[0]);
    for(int i=1;i<v.size();i++)v[i]=VAR(v[i]);
    if(type=="tonumber"){
        if(v[0].Name=="string"&&checknumber(v[0].mstr)){
            Memory cl={"string","",changenum(v[0].mstr),0};cl.mp.clear();
            return cl;
        }else if(v[0].Name=="number")return v[0];
        else{
            Memory cl={"nil","",0,0};cl.mp.clear();
            return cl;
        }
    }else if(type=="tostring"){
        Memory cl;cl.Name="string",cl.mstr=STRING(v[0]),cl.mnum=0,cl.mloc=0;
        return cl;
    }else if(type=="print"){
        Memory output=VAR(v[0]);
        if(output.Name=="string")cout<<output.mstr<<'\n';
        else if(output.Name=="number")printf("%.14g\n",output.mnum);
        else if(output.Name=="boolean")if(output.mnum)cout<<"true\n";else cout<<"false\n";
        else cout<<output.Name<<'\n';
        Memory cl;cl.Name="nil",cl.mstr="",cl.mnum=0,cl.mloc=0,cl.mp.clear();
    return cl;
    }else if(type=="string.sub"){
        if(v[1].mnum<=0)v[1].mnum+=v[0].mstr.size()+1;
        else if(v.size()==2){
            Memory cl={"number","",1.0*v[0].mstr.size(),0};cl.mp.clear();
            v.push_back(cl);
        }else if(v[2].mnum<=0)v[2].mnum+=v[0].mstr.size()+1;
        else if(v[2].mnum-v[1].mnum+1>0){
            Memory cl={"string",v[0].mstr.substr(v[1].mnum-1,v[2].mnum-v[1].mnum+1),0,0};cl.mp.clear();
            return cl;
        }
        Memory cl={"string","",0,0};cl.mp.clear();
        return cl;
    }else if(type=="string.rep"){
        string tmp="";
        for(int i=0;i<v[1].mnum;i++)tmp+=v[0].mstr;
        Memory cl={"string",tmp,0,0};cl.mp.clear();
        return cl;
    }else if(type=="table.concat"){
        int tmp=Table(v[0]);
        if(!tmp){
            Memory cl={"string","",0,0};cl.mp.clear();
            return cl;
        }
        Memory cl={"number","",1,0};cl.mp.clear();
        string ANS=vm[v[0].mp[cl]].mstr;
        for(int i=2;i<=tmp;i++){
            if(v.size()>1)ANS+=v[1].mstr;
            Memory cl={"number","",1.0*i,0};cl.mp.clear();
            ANS+=vm[v[0].mp[cl]].mstr;
        }
        Memory Cl={"string",ANS,0,0};Cl.mp.clear();
        return Cl;
    }else if(type=="table.sort"){
        if(v.size()>1){
            Memory cl={"nil","",0,0};cl.mp.clear();
            return cl;
        }
        int tmp=Table(VAR(v[0]));
        vector<Memory> Sort;Sort.clear();
        for(int i=1;i<=tmp;i++){
            Memory cl={"number","",1.0*i,0};cl.mp.clear();
            Sort.push_back(vm[VAR(v[0]).mp[cl]]);
        }
        sort(Sort.begin(),Sort.end());
        for(int i=1;i<=tmp;i++){
            Memory cl={"number","",1.0*i,0};cl.mp.clear();
            vm[vm[v[0].mloc].mp[cl]]=Sort[i-1];
        }
        Memory cl={"nil","",0,0};cl.mp.clear();
        return cl;
    }
    if(type=="math.abs"){
        Memory cl={"number","",abs(v[0].mnum),0};cl.mp.clear();
        return cl;
    }else if(type=="math.floor"){
        Memory cl={"number","",floor(v[0].mnum),0};cl.mp.clear();
        return cl;
    }else if(type=="math.ceil"){
        Memory cl={"number","",ceil(v[0].mnum),0};cl.mp.clear();
        return cl;
    }else if(type=="math.sqrt"){
        Memory cl={"number","",sqrt(v[0].mnum),0};cl.mp.clear();
        return cl;
    }else if(type=="math.exp"){
        Memory cl={"number","",exp(v[0].mnum),0};cl.mp.clear();
        return cl;
    }else if(type=="math.log"){
        Memory cl={"number","",log(v[0].mnum),0};cl.mp.clear();
        return cl;
    }else if(type=="math.log10"){
        Memory cl={"number","",log10(v[0].mnum),0};cl.mp.clear();
        return cl;
    }else if(type=="math.rad"){
        Memory cl={"number","",v[0].mnum/(45/atan(1.0)),0};cl.mp.clear();
        return cl;
    }else if(type=="math.deg"){
        Memory cl={"number","",v[0].mnum*(45/atan(1.0)),0};cl.mp.clear();
        return cl;
    }else if(type=="math.acos"){
        Memory cl={"number","",acos(v[0].mnum),0};cl.mp.clear();
        return cl;
    }else if(type=="math.atan"){
        Memory cl={"number","",atan(v[0].mnum),0};cl.mp.clear();
        return cl;
    }else if(type=="math.asin"){
        Memory cl={"number","",asin(v[0].mnum),0};cl.mp.clear();
        return cl;
    }else if(type=="math.sin"){
        Memory cl={"number","",sin(v[0].mnum),0};cl.mp.clear();
        return cl;
    }else if(type=="math.cos"){
        Memory cl={"number","",cos(v[0].mnum),0};cl.mp.clear();
        return cl;
    }else if(type=="math.tan"){
        Memory cl={"number","",tan(v[0].mnum),0};cl.mp.clear();
        return cl;
    }else if(type=="math.atan2"){
        Memory cl={"number","",atan2(v[0].mnum,v[1].mnum),0};cl.mp.clear();
        return cl;
    }else if(type=="math.max"){
        if(v[0].Name=="string"){
            if(v[0].mstr>v[1].mstr){
                Memory cl={"string",v[0].mstr,0,0};cl.mp.clear();
                return cl;
            }
            Memory cl={"string",v[1].mstr,0,0};cl.mp.clear();
            return cl;
        }else if(v[0].mnum>v[1].mnum){
            Memory cl={"number","",v[0].mnum,0};cl.mp.clear();
            return cl;
        }
        Memory cl={"number","",v[1].mnum,0};cl.mp.clear();
        return cl;
    }else if(type=="math.min"){
        if(v[0].Name=="string"){
            if(v[0].mstr<v[1].mstr){
                Memory cl={"string",v[0].mstr,0,0};cl.mp.clear();
                return cl;
            }
            Memory cl={"string",v[1].mstr,0,0};cl.mp.clear();
            return cl;
        }else if(v[0].mnum<v[1].mnum){
            Memory cl={"number","",v[0].mnum,0};cl.mp.clear();
            return cl;
        }
        Memory cl={"number","",v[1].mnum,0};cl.mp.clear();
        return cl;
    }
    Memory cl={"nil","",0,0};cl.mp.clear();
    return cl;
}
Memory DEALT(int l,int r,int k){
    if(l>r){
        Memory cl={"nil","",0};cl.mp.clear();
        return cl;
    }
    while(opt[l]==Operator{"SYMBOL","("}&&opt[r]==Operator{"SYMBOL",")"})l++,r--,k=0;
    Operator tmpo=Operator{"EOL",""};/*tmpo.Name="EOL",tmpo.val="";*/
    Memory CL={"nil","",0};CL.mp.clear();
    Memory tmpm=CL;/*tmpm.Name="nil";tmpm.mstr="";tmpm.mnum=0;*/
    tmpm.mp.clear();
    if(checkmemory(l,r)){
        if(l==r){
            if(opt[l].val=="true"){
                Memory cl={"boolean","",1,0};cl.mp.clear();
                return cl;
            }
            if(opt[l].val=="false"){
                Memory cl={"boolean","",0,0};cl.mp.clear();
                return cl;
            }
            if(opt[l].Name=="STRING"){
                Memory cl;cl.Name="string";
                cl.mstr=dealstring(opt[l].val);cl.mnum=0;cl.mloc=0;cl.mp.clear();
                return cl;
            }
            if(opt[l].Name=="NUMBER"){
                Memory cl={"number","",changenum(opt[l].val),0};cl.mp.clear();
                return cl;
            }
            if(checkfunction(opt[l].val)){
                Memory cl={"function",opt[l].val,0,0};cl.mp.clear();
                return cl;
            }
        }
        if(checkfunction(opt[l].val)){
            Memory cl={"function",opt[l].val+'.'+opt[l+2].val,0,0};cl.mp.clear();
            return cl;
        }
        if(l==r-1){
            Memory cl={"table","",0,0};cl.mp.clear();
            return cl;
        }
        if(opt[l].Name=="NAME"){
            int aper=Store[opt[l].val];
            if(!aper){
                tot++;
                Memory cl={"nil","",0,0};cl.mp.clear();
                vm.push_back(cl);
                aper=tot;
                Store[opt[l].val]=aper;
            }
            l++;
            while(l<=r){
                if(opt[l].val=="["){
                    int matchs=pm[l];
                    Memory mem=DEALT(l+1,matchs-1,0);
                    int id=vm[aper].mp[VAR(mem)];
                    if(!id){
                        tot++;
                        Memory cl={"nil","",0,0};cl.mp.clear();
                        vm.push_back(cl);
                        id=tot;
                        vm[aper].mp[VAR(mem)]=id;
                    }
                    aper=id;
                    l=matchs+1;
                }else{
                    Memory cl={"string",opt[l+1].val,0,0};cl.mp.clear();
                    int id=vm[aper].mp[cl];
                    if(!id){
                        tot++;
                        Memory cl={"nil","",0,0};cl.mp.clear();
                        vm.push_back(cl);
                        id=tot;
                        Store[opt[l+1].val]=id;
                    }
                    aper=id;
                    l+=2;
                }
            }
            Memory cl={"var","",0,aper};cl.mp.clear();
            return cl;
        }
    }
    if(k==8){
        vector<pair<int,int> >vp;vp.clear();
        int First=l;
        for(int i=l;i<=r;i++)if(opt[i].val=="^")vp.push_back({First,i-1}),First=i+1;
        vp.push_back({First,r});
        int vplen=vp.size();
        tmpm=DEALT(vp[vplen-1].first,vp[vplen-1].second,9);
        vplen--;
        while(vplen)tmpm=dealsymbol(Operator{"SYMBOL","^"},DEALT(vp[vplen-1].first,vp[vplen-1].second,9),tmpm),vplen--;
        return tmpm;
    }
    for(int i=l;i<=r;i++){
        if(opt[i].val=="("||opt[i].val=="["){i=pm[i];continue;}
        else if(opt[i].Name=="NAME"&&k==9){
            if(checkfunction(opt[i].val)||vm[Store[opt[i].val]].Name=="function"){
                vector<Memory> par;string ftype;
                if(vm[Store[opt[i].val]].Name=="function")ftype=vm[Store[opt[i].val]].mstr;
                else{
                    ftype=opt[i].val;
                    if(opt[i+1].val==".")ftype+=('.'+opt[i+2].val),i+=2;
                }
                par.clear();int j;
                for(j=i+2,i=i+2;opt[j].val!=")";i=j+1,j=i){
                    while(opt[j].val!=","&&opt[j].val!=")"){j++;if(opt[j].val=="(")j=pm[j]+1;}
                    par.push_back(DEALT(i,j-1,0));
                    if(opt[j].val!=",")j--;
                }
                return dealfunction(ftype,par);
            }
        }else if(opt[i].Name=="SYMBOL"&&Priority(opt[i].val)==k){
            Memory tmp=DEALT(l,i-1,k+1);
            l=i+1;
            tmpm=dealsymbol(tmpo,tmpm,tmp);
            tmpo=opt[i];
        }
    }
    return dealsymbol(tmpo,tmpm,DEALT(l,r,k+1));
}
void init(){
    tot=0;
    vm.clear();
    Memory cl={"nil","",0,0};cl.mp.clear();
    vm.push_back(cl);
    Store.clear();
    ans="";
}
int main(){
    // ios::sync_with_stdio(false);
    // cin.tie(0);
    // cout.tie(0);
    init();
    while(getline(cin,str)){
        len=str.length();
        if(!len)init();
        for(int i=0;i<len;i++){
            if(str[i]==' ')continue;
            else if(str[i]=='-'&&str[i+1]=='-')break;
            else if(str[i]>='0'&&str[i]<='9'||str[i]=='.'&&str[i+1]>='0'&&str[i+1]<='9'){
                pd=0;
                if(str[i]=='.')ans+=str[i],i++,pd=1;
                if(str[i]=='0'&&(str[i+1]=='X'||str[i+1]=='x'))ans+=str[i],ans+=str[i+1],i+=2,pd=1;
                while(checknum(i))ans+=str[i],i++;i--;
                opt.push_back(Operator{"NUMBER",ans});
                ans="";
            }else if(str[i]=='\''||str[i]=='\"'){
                ans+=str[i];
                char tmp=str[i];i++;
                while(i<str.length()&&checkstring(i,tmp))ans+=str[i],i++;
                ans+=str[i];
                opt.push_back(Operator{"STRING",ans});
                ans="";
            }else if(checksymbol(i)){
                int tmp=checksymbol(i);
                for(int j=0;j<tmp;j++)ans+=str[i+j];
                i+=tmp,i--;
                opt.push_back(Operator{"SYMBOL",ans});
                ans="";
            }else if(str[i]>='a'&&str[i]<='z'||str[i]>='A'&&str[i]<='Z'){
                while(checkname(i))ans+=str[i],i++;i--;
                if(checkreserved(ans)){
                    if(ans=="and"||ans=="or"||ans=="not")opt.push_back(Operator{"SYMBOL",ans});
                    else if(ans=="true"||ans=="false")opt.push_back(Operator{"NUMBER",ans});
                    else opt.push_back(Operator{"RESERVED",ans});
                }else{
                    opt.push_back(Operator{"NAME",ans});
                }
                ans="";
            }
        }
        int optlen=opt.size();
        for(int i=0;i<optlen;i++){
            if(opt[i]==Operator{"SYMBOL","("}||opt[i]==Operator{"SYMBOL","["}||opt[i]==Operator{"SYMBOL","{"})st.push(i);
            if(opt[i]==Operator{"SYMBOL",")"}||opt[i]==Operator{"SYMBOL","]"}||opt[i]==Operator{"SYMBOL","}"})pm[st.top()]=i,st.pop();
        }
        for(int i=0;i<optlen;i++){
            if(opt[i].val=="math"&&opt[i+1].val=="."&&opt[i+2].val=="pi"){
                opt[i]=Operator{"NUMBER",changestring(acos(-1.0))};
                opt[i+1]=Operator{"SYMBOL","*"};
                opt[i+2]=Operator{"NUMBER","1"};
            }
        }
        DEALT(0,optlen-1,0);
        opt.clear();
    }
    return 0;
}

标签:mnum,return,Name,cl,题解,clear,II,UVA12422,Memory
From: https://www.cnblogs.com/MithrilSwordXIV/p/18032260

相关文章

  • Atcoder ABC 342 全题解
    闲话当我还是一个只会AB的小蒟蒻时,由于不会C又看不懂官方题解,只好看网上的题解。结果:ABC签到题不讲AB对着题意模拟即可。A有个好玩的做法,先看前两个,如果不同跟第三个比较,如果相同看后面哪个字母跟第一个不一样。C由于是将所有的$c_i$替换,所以可得同一个字母最......
  • CF1930E 2..3...4.... Wonderful! Wonderful! 题解
    DescriptionStackhasanarray$a$oflength$n$suchthat$a_i=i$forall$i$($1\leqi\leqn$).Hewillselectapositiveinteger$k$($1\leqk\leq\lfloor\frac{n-1}{2}\rfloor$)anddothefollowingoperationon$a$an......
  • [ARC155D] Avoid Coprime Game 题解
    Description非负整数\(x,y\)的最大公约数记为\(\gcd(x,y)\),规定\(\gcd(x,0)=\gcd(0,x)=x\)。黑板上写了\(N\)个整数\(A_1,A_2,...,A_N\),这\(N\)个数的最大公约数是\(1\)。Takahashi和Aoki在玩游戏,有一个变量\(G\)初值为\(0\),他们轮流进行以下操作:从黑板上选择......
  • 【力扣】删除排序链表中的重复元素II
    题目描述思路这些链表的有序性使这些操作都不难写。前面在数组的题目里写过跳过重复元素的算法,这个和那个类似,用快慢指针写,但是由于这个是删除重复元素,所以我用了两个相邻的慢指针,左边的慢指针其实是为了保存真正的慢指针的上一个位置。代码如下:classSolution{public:......
  • 代码随想录算法训练营第二十七天| 93.复原IP地址 78.子集 90.子集II
    复原IP地址 题目链接:93.复原IP地址-力扣(LeetCode)思路:投降。在判断字符串是否合法这部分遇到了困难。classSolution{private:vector<string>result;//记录结果//startIndex:搜索的起始位置,pointNum:添加逗点的数量voidbacktracking(string&s,int......
  • 2024牛客寒假算法基础集训营4个人补题题解(B、E)
    B、左右互博不能操作的情况有且仅有所有石子堆的石子个数只有1的时候,因此不管途中怎么操作,让所有石子堆都变成1的总操作次数是确定的。即假设一共有\(n\)堆石子,石子总数为\(sum\),总操作次数为\((sum-n)\)次。因此当\((sum-n)\)%\(2=0\)时一定在sweet操作完(或没有操作)后gui无法......
  • leedcode 杨辉三角-II
    自己写的:classSolution:defgetRow(self,rowIndex:int):#初始化一个列表以存储帕斯卡三角形的行li=[]#前两行是预定义的li.append([1])li.append([1,1])#rowIndex为0和1的基本情况ifrowIndex......
  • CF1832B Maximum Sum 题解
    【题目描述】给定一个长度为\(n\)的数列,其中每个元素互不相同,进行\(k\)次操作,每次可以选择删除序列中最小的两个数或最大的一个数。求操作后剩余数的和的最大值。【思路】我们构造一组数据:首先我们看到题目中的一句话:每次可以选择删除序列中最小的两个数或最大的一个数。......
  • CF1857B Maximum Rounding 题解
    题目描述给定一个自然数\(n\),可以对任意一位进行四舍五入,可以进行任意次,求能得到的最大数。(这里的\(n\)没有前导零)思路首先我们发现,如果我们将其中一位进位了,那后面的所有位都会变成\(0\),因此,如果我们进位了两次,那么位置靠后的那次进位,其实是没有用的。所以我们要从高位往......
  • CF1857G Counting Graphs 题解
    题目描述给定一棵最小生成树,求有多少张图的最小生成树是给定的树,并且这张图的所有边边权不超过\(S\)。思路考虑在最小生成树中加边。我们回顾一下Kruskal的过程:找到没被用过的,最小的边判断这条边的两端是否在一个联通块中加入这条边,将两端的联通块连在一起根据第三条......