首页 > 代码库 > 2014牡丹江——Hierarchical Notation

2014牡丹江——Hierarchical Notation

题目链接

字符串模拟

const int MAXN = 2000000;

char ipt[MAXN], t[MAXN];
int f[MAXN], len, to[MAXN];
map<string, string> mp[MAXN];
string x, key, ans;
string i2s(int n)
{
    string ret = "";
    if (n == 0)
        ret += '0';
    else
    {
        while (n)
        {
            ret += n % 10 + '0';
            n /= 10;
        }
    }
    ret += '*';
    reverse(ret.begin(), ret.end());
    return ret;
}
int s2i(string& s)
{
    int ret = 0;
    FF(i, 1, s.length())
        ret = ret * 10 + s[i] - '0';
    return ret;
}

void fun_f()
{
    stack<int> sk;
    sk.push(0);
    int cnt = 0;
    REP(i, len)
    {
        if (ipt[i] == '{')
        {
            sk.push(++cnt);
            f[i] = cnt;
            to[f[i]] = i;
        }
        else if (ipt[i] == '}')
        {
            sk.pop();
            f[i] = sk.top();
        }
    }
}

void fun_ipt()
{
    int state = 0, lv = 0, nxt;
    REP(i, len)
    {
        switch (ipt[i])
        {
        case '\"':
            if (state == 0 || state == 2)
                x = "\"";
            else if (state == 1)
                key = x + '\"';
            else
                mp[lv][key] = x + '\"';
            state = (state + 1) % 4;
            break;
        case '{':
            nxt = f[i];
            if (state == 2)
            {
                mp[lv][key] = i2s(nxt);
                state = 0;
            }
            lv = nxt;
            break;
        case '}':
            lv = f[i];
            break;
        case ':' || ',':
            break;
        default:
            x += ipt[i];
        }
    }
}

void fun_case()
{
    int state = 0, lv = 0, n;
    RI(n);
    REP(kase, n)
    {
        bool ok = true;
        state = 0, lv = 1;
        RS(t);
        len = strlen(t);
        REP(i, len)
        {
            if (t[i] == '\"')
            {
                if (state == 0)
                    x = '\"';
                else
                {
                    x += '\"';
                    if (mp[lv].count(x) == 0)
                    {
                        ok = false;
                        break;
                    }
                }
                ans = mp[lv][x];
                state = !state;
            }
            else if (t[i] == '.')
            {
                if (ans[0] != '*')
                {
                    ok = false;
                    break;
                }
                lv = s2i(ans);
            }
            else
                x += t[i];
        }
        if (ok)
        {
            if (ans[0] == '*')
            {
                int i = to[s2i(ans)], cnt = 0;
                while (true)
                {
                    if (ipt[i] == '{')
                        cnt++;
                    else if (ipt[i] == '}')
                    {
                        if (--cnt == 0)
                            break;
                    }
                    putchar(ipt[i]);
                    i++;
                }
                puts("}");
            }
            else
                puts(ans.c_str());
        }
        else
            puts("Error!");
    }
}

int main()
{
    int T;
    RI(T);
    while (T--)
    {
        REP(i, MAXN)
            mp[i].clear();

        RS(ipt);
        len = strlen(ipt);
        fun_f();
        fun_ipt();
        fun_case();
    }
    return 0;
}


2014牡丹江——Hierarchical Notation