ZOJ 3826 大模拟

ZOJ 3826
题目链接:
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3826
题意:
模拟python的字典
思路:
注意有一个数据是{},所以在使用solve前就必须清空map而不是边用边清空
源码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#include <map>
using namespace std;
const int MAXN = 500000 + 5;
char str[MAXN];
string key[MAXN];
int out[MAXN];
int cnt;
string leaf[MAXN];
int leaf_cnt;
map<string,int>mm[MAXN];
int solve(int L, int R)
{
    int ok = 1;
    for(int i = L ; i <= R ; i++){
        if(str[i] == ':' || str[i] == ','){
            ok = 0;
            break;
        }
    }
    if(ok){
        string temp = "";
        for(int i = L ; i <= R ; i++)   temp += str[i];
        leaf[++leaf_cnt] = temp;
        return -leaf_cnt;
    }
    if(str[L] == '{' && str[R] == '}'){L++, R--;}
    int last = L;
    int mark = 0;
    int id = ++cnt;
// printf("id = %d\n", id);
// mm[id].clear();
    for(int i = L ; i <= R ; i++){
        if(str[i] == '{')   mark++;
        else if(str[i] == '}')  mark--;
        else if(str[i] == ',' && mark == 0){
            int j = last;
            string temp = "";
            while(str[j] != ':')    temp += str[j++];
// printf("id = %d, temp = ", id);
// cout << temp << endl;
            mm[id][temp] = solve(j + 1, i - 1);
            last = i + 1;
        }
    }
    int j = last;
    string temp = "";
    while(str[j] != ':')
        temp += str[j++];
    mm[id][temp] = solve(j + 1, R);
    return id;
}
void print(int u)
{
    if(u < 0){
        cout << leaf[-u];
        return;
    }
    int f = 1;
    printf("{");
    map<string,int>::iterator it;
    for(it = mm[u].begin(); it != mm[u].end() ; it++){
        if(f)   f = 0;
        else    printf(",");
        cout << (it->first) << ":";
// printf(" ");
// system("pause");
        print(it->second);
    }
    printf("}");
}
string S[MAXN];
int main()
{
// freopen("ZOJ 3826.in", "r", stdin);
    int t;
    scanf("%d", &t);
    while(t--){
        scanf("%s", str);
        int len = strlen(str);
// mm[0].clear();
// mm[1].clear();
        for(int i = 0 ; i < len ; i++)
            mm[i].clear()
        cnt = 0;
        leaf_cnt = 0;
        solve(0, len - 1);
        int q;
        scanf("%d", &q);
        while(q--){
            scanf("%s", str);
            string temp = "";
            int len = strlen(str);
            int ok = 1;
            int now = 1;
            int m = 0;
            for(int i = 0 ; i < len ; i++){
                if(str[i] == '.'){
                    S[m++] = temp;  temp = "";
                }
                else temp = temp + str[i];
            }
            S[m++] = temp;
            for(int i = 0 ; i < m ; i++){
                if(now < 0){
                    ok = 0;
                    break;
                }
                if(mm[now].find(S[i]) == mm[now].end()){
                    ok = 0;
                    break;
                }
                else now = mm[now][S[i]];
            }
            if(!ok){
                printf("Error!\n");
            }
            else{
                print(now);
                printf("\n");
            }
// system("pause");
        }
    }
    return 0;
}

你可能感兴趣的:(ZOJ 3826 大模拟)