zoukankan      html  css  js  c++  java
  • 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;
    }


  • 相关阅读:
    HTML中ID与NAME的区别
    三层架构的学习感悟(二)
    linux svn安装和配置,不结合apache
    _dopostback回发后台的局限
    ASP.NET 服务器端控件响应客户端事件
    linux centos 下面httpd支持的svn 服务器端安装
    最简单的html tab选项卡
    VC2010 控件的事件窗口面板空白解决办法
    记录一次低版本手机h5页面不能进行跳转
    设计模式的理解
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/5069589.html
Copyright © 2011-2022 走看看