码迷,mamicode.com
首页 > 其他好文 > 详细

zoj 3826 Hierarchical Notation(模拟)

时间:2015-10-23 21:37:04      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:zoj 3826 Hierarchical Notation

题目大意:给定一些结构体。结构体有value值和key值,Q次询问,输出每一个key值相应的value值。

解题思路:思路非常easy。写个类词法的递归函数,每次将key值映射成一个hash值,用map映射每一个key的value起始终止位置,预处理完了查询就非常easy了。
这题是最后10分钟出的。由于没有考虑value为{}的情况,导致RE了。可是zoj上显示的是SE,表示不理解什么意思,事实上就是RE。只是另一个地方会导致RE。就是询问的长度可能很大。手贱瞎改了一下就给过了。PS:我做的是同步赛。

#include <cstdio>
#include <cstring>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;
typedef unsigned long long ll;
typedef pair<int,int> pii;

const int maxn = 2000000;
const ll x = 123;

int N, Q, mv;
char op[maxn], s[maxn];
map<ll, pii> G;

inline int idx(char ch) {
    if (ch >= ‘0‘ && ch <= ‘9‘)
        return ch - ‘0‘;
    else if (ch >= ‘A‘ && ch <= ‘Z‘)
        return ch - ‘A‘ + 10;
    else if (ch >= ‘a‘ && ch <= ‘z‘)
        return ch - ‘a‘ + 36;
    else if (ch == ‘.‘)
        return 62;
    return 63;
}

void solve (ll u) {
    ll tmp = u;

    while (s[mv] != ‘}‘) {
        mv++;
        if (s[mv] == ‘}‘)
            return;
        u = tmp;

        while (s[mv] != ‘:‘)
            u = u * x + idx(s[mv++]);

        int l = ++mv;

        if (s[mv] == ‘{‘)
            solve(u * x + 62LL);
        else
            while (s[mv+1] != ‘,‘ && s[mv+1] != ‘}‘) mv++;

        G[u] = make_pair(l, mv);
        mv++;
    }
}

int main () {
    int cas;
    scanf("%d", &cas);
    while (cas--) {
        scanf("%s", s);

        mv = 0;
        G.clear();
        solve(0);

        scanf("%d", &Q);
        for (int i = 1; i <= Q; i++) {
            scanf("%s", op);

            ll ret = 0;
            int len = strlen(op);

            for (int i = 0; i < len; i++)
                ret = ret * x + idx(op[i]);

            if (G.count(ret)) {
                pii u = G[ret];
                for (int i = u.first; i <= u.second; i++)
                    printf("%c", s[i]);
                printf("\n");
            } else
                printf("Error!\n");
        }
    }
    return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

zoj 3826 Hierarchical Notation(模拟)

标签:

原文地址:http://www.cnblogs.com/bhlsheji/p/4905677.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!