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;
}
时间: 2024-10-01 09:03:20

2014牡丹江——Hierarchical Notation的相关文章

2014牡丹江——Known Notation

题目链接 题意: 输入一个长度不超过1000的字符串,包含数字(1-9)和星号(*).字符串中的空格已经丢失,所以连起来的数字串能够看成很多分开的数.也能够看成连续的数,即能够随意加入空格. 如今有两种操作:1)在任何位置加入随意类型的字符(数字或者星号)    2)交换字符串中的随意两个字符 求:最少操作多少次,使得得到的串是一个合法的逆波兰式 分析: 对于n个星号,n+1个数字的字符串,假设将星号都移动到串的末尾.那么一定是合法的 对于操作1,假设须要插入数字,那么插入到字符串的最前边是最优

ZOJ 3826 Hierarchical Notation(2014 牡丹江 H,字符串模拟)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5380 Hierarchical Notation Time Limit: 2 Seconds      Memory Limit: 131072 KB In Marjar University, students in College of Computer Science will learn EON (Edward Object Notation), which is a

zoj3826 Hierarchical Notation (字符串模拟)

Hierarchical Notation Time Limit: 2 Seconds      Memory Limit: 131072 KB In Marjar University, students in College of Computer Science will learn EON (Edward Object Notation), which is a hierarchical data format that uses human-readable text to trans

zoj 3826 Hierarchical Notation(模拟)

题目链接:zoj 3826 Hierarchical Notation 题目大意:给定一些结构体.结构体有value值和key值,Q次询问,输出每一个key值相应的value值. 解题思路:思路非常easy.写个类词法的递归函数,每次将key值映射成一个hash值,用map映射每一个key的value起始终止位置,预处理完了查询就非常easy了. 这题是最后10分钟出的.由于没有考虑value为{}的情况,导致RE了.可是zoj上显示的是SE,表示不理解什么意思,事实上就是RE.只是另一个地方会

2014牡丹江区域赛H(字典树)ZOJ3826

Hierarchical Notation Time Limit: 2 Seconds      Memory Limit: 131072 KB In Marjar University, students in College of Computer Science will learn EON (Edward Object Notation), which is a hierarchical data format that uses human-readable text to trans

2014牡丹江区域赛H(特里)ZOJ3826

Hierarchical Notation Time Limit: 2 Seconds      Memory Limit: 131072 KB In Marjar University, students in College of Computer Science will learn EON (Edward Object Notation), which is a hierarchical data format that uses human-readable text to trans

ZOJ 3829 Known Notation (2014牡丹江H题)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5383 Known Notation Time Limit: 2 Seconds      Memory Limit: 65536 KB Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathematics and computer science. I

ACM学习历程——ZOJ 3829 Known Notation (2014牡丹江区域赛K题)(策略,栈)

Description Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathematics and computer science. It is also known as postfix notation since every operator in an expression follows all of its operands. Bob is a student in

ZOJ3826 Hierarchical Notation(14牡丹江 H) 树套树

题意:给你一个嵌套字典,询问字典的键值 ,输出字典的值. 解题思路:我的想法是字典树套字典树,因为指针的大小为8 字节 所以动态字典树会超内存,开始以为不能静态,后来发现静态实现也挺简单.所以又改成静态. 写到220行,还要特别讨论{"a":{}} 这种特判. 解题代码: 1 // File Name: h.cpp 2 // Author: darkdream 3 // Created Time: 2014年10月18日 星期六 11时35分02秒 4 5 #include<ve