zoj 3826 Hierarchical Notation(模拟)

33 篇文章 0 订阅
22 篇文章 0 订阅

题目链接:zoj 3826 Hierarchical Notation

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

解题思路:思路很简单,写个类词法的递归函数,每次将key值映射成一个hash值,用map映射每个key的value起始终止位置,预处理完了查询就很简单了。
这题是最后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;
}
  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值