模拟一点不会的蒟蒻(84 pts求调),代码算是比较清晰

P2830 写程序

AK_heaven @ 2024-08-29 21:46:06

3 #5 RE

#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 10;

int cnt;

map<string, int> F;
map<int, int> a[N], sz;

bool isnum(string s) {
    for(auto x : s)
      if(x < '0' || x > '9') return 0;
    return 1;
}

int work(string s) {
    if(isnum(s)) return stoi(s);
    int l = 0, r = s.size()-2;
    while(s[l] != '[') l++; l++;
    int pos = F[s.substr(0, l-1)];
    int t = work(s.substr(l, r-l+1));
    if(t >= sz[pos] || t < 0) return -1;
    return a[pos][t];
}

int main() {string s, t; 
    while(cin >> s >> t) {
        1;
        if(s == "int") {
            ++cnt;          
            int l = 0, r = t.size()-2;
            while(t[l] != '[') l++; l++;
            string nm = t.substr(0, l-1);
            F[nm] = cnt;
//          cout << t.substr(l, r-l+1) << '\n';
            sz[cnt] = stoi(t.substr(l, r-l+1));
        }
        else if(s == "cout") {
            int h = work(t);
            if(h == -1) return cout << -1, 0;
            cout << work(t) << '\n';
        }
        else {
            int l = 0, r = s.size()-2;
            while(s[l] != '[') l++; l++;
            string nm = s.substr(0, l-1);
            int pos = F[nm];
            int find = work(s.substr(l, r-l+1));
            if(find >= sz[pos] || find == -1) return cout << -1, 0;
            int h = work(t);
            if(h == -1) return cout << -1, 0;
            a[pos][find] = h;
        }
    }
    return 0;
}

|