神奇的现象(xswl)

P2830 写程序

_luanyi_ @ 2022-04-27 20:23:20

WA 了一堆得代码

#include <bits/stdc++.h>
#define fq(i,a,b) for (int i = (a); i <= (b); i++)
#define fnq(i,a,b) for (int i = (a); i < (b); i++)
#define nfq(i,a,b) for (int i = (a); i >= (b); i--)
#define nfnq(i,a,b) for (int i = (a); i > (b); i--)
#define elif else if
using namespace std;

struct arr {
    int sz;
    vector <int> a;
};
map <string, arr> p;
vector <string> tmp;
int solve (string s) {
    int j = 0, num = 0;
    tmp.clear ();
    int k = 0;
    fnq (i, 0, s.size ()) {
        if (s[i] == '[') k = i;
    }
    fnq (i, 0, s.size ()) {
        if (s[i] == '[') tmp.push_back (s.substr (j, i - j)), j = i + 1;
        elif (isdigit (s[i]) && i > k) num = num * 10 + s[i] - '0';
    }
    nfq (i, tmp.size () - 1, 0) {
        string s = tmp[i];
        if (p.find (s) == p.end ()) return -1;
        if (p[s].sz <= num) return -1;
        num = p[s].a[num];
    } return num;
}
string s;
signed main () {
    while (cin >> s) {
        if (s == string ("int")) {
            cin >> s;
            string ss = s.substr (0, s.find ("["));
            int cnt = 0, flag = 0, st = -1, ed = -111;
            fnq (i, 0, s.size ()) {
                if (s[i] == '[') {
                    if (st == -1) st = i + 1;
                    cnt++, flag = 1;
                }
                elif (s[i] == ']') cnt--;
                if (!cnt && flag) {
                    ed = i - 1;
                    break;
                }
            }
            int sz = solve (s.substr (st, ed - st + 1));
            if (sz == -1) cout << -1, exit (0);
            p[ss].sz = sz;
            p[ss].a.resize (sz);
            fnq (i, 0, sz) p[ss].a[i] = 0;
        } elif (s == string ("cout")) {
            cin >> s;
            int res = solve (s);
            if (res == -1) cout << -1, exit (0);
            else cout << res << endl;
        } else {
            int cnt = 0, flag = 0, st = -1, ed = -111;
            fnq (i, 0, s.size ()) {
                if (s[i] == '[') {
                    if (st == -1) st = i + 1;
                    cnt++, flag = 1;
                }
                elif (s[i] == ']') cnt--;
                if (!cnt && flag) {
                    ed = i - 1;
                    break;
                }
            }
            int u = solve (s.substr (st, ed - st + 1));
            string t; cin >> t;
            int v = solve (t);
            if (u == -1 || v == -1) cout << -1, exit (0);
            string name = s.substr (0, st - 1);
            if (u >= p[name].sz) cout << -1, exit (0);
            p[name].a[u] = v;
        }
    }
    return 0;
}

AC 代码

#include <bits/stdc++.h>
#define fq(i,a,b) for (int i = (a); i <= (b); i++)
#define fnq(i,a,b) for (int i = (a); i < (b); i++)
#define nfq(i,a,b) for (int i = (a); i >= (b); i--)
#define nfnq(i,a,b) for (int i = (a); i > (b); i--)
#define elif else if
using namespace std;

struct arr {
    int sz;
    vector <int> a;
};
map <string, arr> p;
vector <string> tmp;
int solve (string s) {
    int j = 0, num = 0;
    tmp.clear ();
    int k = 0;
    fnq (i, 0, s.size ()) {
        if (s[i] == '[') k = i;
    }
    fnq (i, 0, s.size ()) {
        if (s[i] == '[') tmp.push_back (s.substr (j, i - j)), j = i + 1;
        elif (isdigit (s[i]) && i >= k /*就这里不一样,之前是 i > k*/) num = num * 10 + s[i] - '0';
    }
    nfq (i, tmp.size () - 1, 0) {
        string s = tmp[i];
        if (p.find (s) == p.end ()) return -1;
        if (p[s].sz <= num) return -1;
        num = p[s].a[num];
    } return num;
}
string s;
signed main () {
    while (cin >> s) {
        if (s == string ("int")) {
            cin >> s;
            string ss = s.substr (0, s.find ("["));
            int cnt = 0, flag = 0, st = -1, ed = -111;
            fnq (i, 0, s.size ()) {
                if (s[i] == '[') {
                    if (st == -1) st = i + 1;
                    cnt++, flag = 1;
                }
                elif (s[i] == ']') cnt--;
                if (!cnt && flag) {
                    ed = i - 1;
                    break;
                }
            }
            int sz = solve (s.substr (st, ed - st + 1));
            if (sz == -1) cout << -1, exit (0);
            p[ss].sz = sz;
            p[ss].a.resize (sz);
            fnq (i, 0, sz) p[ss].a[i] = 0;
        } elif (s == string ("cout")) {
            cin >> s;
            int res = solve (s);
            if (res == -1) cout << -1, exit (0);
            else cout << res << endl;
        } else {
            int cnt = 0, flag = 0, st = -1, ed = -111;
            fnq (i, 0, s.size ()) {
                if (s[i] == '[') {
                    if (st == -1) st = i + 1;
                    cnt++, flag = 1;
                }
                elif (s[i] == ']') cnt--;
                if (!cnt && flag) {
                    ed = i - 1;
                    break;
                }
            }
            int u = solve (s.substr (st, ed - st + 1));
            string t; cin >> t;
            int v = solve (t);
            if (u == -1 || v == -1) cout << -1, exit (0);
            string name = s.substr (0, st - 1);
            if (u >= p[name].sz) cout << -1, exit (0);
            p[name].a[u] = v;
        }
    }
    return 0;
}

哪位神仙可以解释一下吗?就很神奇,因为isidigit(s[k]) 应该永远是 false 啊?


|