zoj 3826 Hierarchical Notation(類比),zojhierarchical

來源:互聯網
上載者:User

zoj 3826 Hierarchical Notation(類比),zojhierarchical

題目連結: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;}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.