92pts求助

P2830 写程序

某个新手 @ 2023-11-27 21:32:27

#8 WA

#include<bits/stdc++.h>
using namespace std;
map <string,int> m;
string a,b,c,stk[101];
string s(int n){
    if (!n)return "0";
    string str = "";
    char c;
    while (n){
        c = (n % 10) + '0';
        str = c + str;
        n /= 10;
    }
    return str;
}
int f(const string &str){
    int len = str.length(),cnt = 0,t = 0;
    stk[0] = "";
    for (int i = 0;i < len;i++){
        if (str[i] == '['){
            stk[cnt] += '[';
            cnt++;
            stk[cnt] = "";
        }else if ('0' <= str[i] && str[i] <= '9')
            t = t * 10 + str[i] - '0';
        else if (str[i] == ']')break;
        else stk[cnt] += str[i];
    }
    while (cnt--){
        c = stk[cnt] + s(t) + ']';
        if (!m.count(c))
            return -1;
        t = m[c];
    }
    return t;
}
int main(){
    bool flag;
    string t;
    int n,i,len;
    while (cin>>a>>b){
        if (a == "int"){
            a = "";
            t = "";
            len = b.length();
            flag = 1;
            for (i = 0;i < len - 1;i++){
                if (flag)a += b[i];
                else t += b[i];
                if (b[i] == '[')
                    flag = 0;
            }
            n = f(t);
            if (n == -1){
                cout<<-1;
                return 0;
            }
            for (i = 0;i < n;i++)
                m[a + s(i) + ']'] = 0;
        }else if (a == "cout"){
            n = f(b);
            if (n == -1){
                cout<<-1;
                return 0;
            }
            cout<<n<<endl;
        }else{
            c = "";
            t = "";
            len = a.length();
            flag = 1;
            for (i = 0;i < len - 1;i++){
                if (flag)c += a[i];
                else t += a[i];
                if (a[i] == '[')
                    flag = 0;
            }
            a = c;
            n = f(t);
            if (n == -1){
                cout<<-1;
                return 0;
            }
            a += s(n) + ']';
            n = f(b);
            if (n == -1 || !m.count(a)){
                cout<<-1;
                return 0;
            }
            m[a] = n;
        }
    }
    return 0;
}

by 某个新手 @ 2024-10-29 20:26:25

问题已解决

原因:数组名中可能有数字

hack如下:

int a12[11]
cout a12[10]

|