2014 Mudanjiang -- hierarchical notation

Source: Internet
Author: User

Question Link

String Simulation

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 Mudanjiang -- hierarchical notation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.