zoj 3826 Hierarchical Notation 模拟

题意:

思路:

1
{"hm":"Edward","stu":{"stu01":"Alice","stu02":"Bob"}}
4
"hm"
"stu"
"stu"."stu01"
"students"

错误思路:

把上一级的字符串也放到map里了。例如:“stu”."stu01" 作为一个key放到map。这样做的结果是MLE。

正确思路:

根据上面的样例,构建这样一个map,key为只记录“hm”,“stu”,“stu01”,“stu02”,value记录第几个映射num,以及该key冒号后所对应的字符串的l, r;

再构建vector,记录映射与映射的级别关系;例如,stu和stu01之间就有一个级别关系,像一颗树一样。

注意:

测试数据没有像这样的数据,否则我也过不了。题目中也没明说,害我一旁想很久。

{"int":{"long":"longlong","qian":"gong"},"double":{"long":"longlong","qian":"gong"}

code:

/* **********************************************
Created Time: 2014-10-14 17:04:53
File Name   : zoj3826.cpp
*********************************************** */
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <fstream>
#include <cstring>
#include <climits>
#include <deque>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <functional>
#include <algorithm>
using namespace std;
typedef long long LL;
const int MAXN = 20005;
struct PP
{
        int l, r, num;
};
map <string, PP> mp;
vector <int> vc[MAXN];
string str;
int cot;
int dfs(int si, int par)
{
        int l, r;
        string ch;
        int curpar;
        for(int i = si; i < str.length(); i++)
        {
                if(str[i] == '}') return i;
                if(str[i] == ',') 
                {
                        ch.clear();
                        curpar = -1;
                        continue;
                }
                else if(str[i] == '"')
                {
                        l = i;
                        for(r = i+1; r < str.length(); r++)
                                if(str[r] == '"') break;
                        ch = str.substr(l, r-l+1);
                        mp[ch].num = cot++;
                        curpar = cot-1;
                        if(par != -1)
                                vc[par].push_back(cot-1);
                        i = r;
                }
                else if(str[i] == ':')
                {
                        if(str[i+1] == '"')
                        {
                                l = i+1;
                                for(r = l+1; r < str.length(); r++)
                                        if(str[r] == '"') break;
                                mp[ch].l = l, mp[ch].r = r;
                                i = r;
                        }
                        else if(str[i+1] == '{')           //str[i+1] = '{'
                        {
                                l = i+1;
                                r = dfs(l+1, curpar);
                                mp[ch].l = l, mp[ch].r = r;
                                i = r;
                        }
                }
        }
        return str.length();
}

void init()
{
        for(int i = 0;i < MAXN; i++)
                vc[i].clear();
        cot = 0;
        mp.clear();
}
int main()
{
        ios::sync_with_stdio(false);
        int T;
        cin>>T;
        while(T--)
        {
                init();
                cin>>str;
                //
                dfs(0, -1);
                int q;
                cin>>q;
                map <string, PP> :: iterator it;
                string tmp;
                for(int i = 0;i < q; i++)
                {
                        cin>>tmp;
                        int par = -1;
                        for(int j = 0;j < tmp.length(); j++)
                        {
                                //if(tmp[j] == '.') continue;
                                if(tmp[j] == '"')
                                {
                                        int l = j, r = j+1;
                                        while(tmp[r] != '"')
                                                r++;
                                        j = r;
                                        //string tt = tmp.substr(l, r-l+1);
                                        //
                                        it = mp.find(tmp.substr(l, r-l+1));
                                        if(it == mp.end())     { cout<<"Error!"<<endl; break; }
                                        int tpar = (it->second).num;
                                        bool mark = false;
                                        //cout<<"par = "<<par<<endl;
                                        //cout<<"tpar = "<<tpar<<endl;
                                        if(par == -1)
                                        {
                                                mark = true;
                                                par = tpar;
                                        }
                                        else
                                        {
                                                for(int i = 0;i < vc[par].size(); i++)
                                                {
                                                        if(vc[par][i] == tpar)
                                                        {
                                                                mark = true;
                                                                break;
                                                        }
                                                }
                                                par = tpar;
                                        }
                                        if(!mark)     { /*cout<<"no pointer!"<<endl; */cout<<"Error!"<<endl; break; }
                                        int tl = (it->second).l, tr = (it->second).r;
                                        if(j == tmp.length() - 1)   //current string is last
                                        {
                                                cout<<str.substr(tl, tr-tl+1)<<endl;
                                                break;
                                        }
                                }
                        }
                }
        }
        return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值