这道题当时考ccf,五道题中做的时间最长的一道题....可惜最好只有0分!!

后来重现写了一下--(110行超级麻烦

主要思想:就是先对括号经行匹配,创建的时候分为创建表和创建元素两种情况,难点在于对字符串的分割

然而昨天在受到某婷的启发下,想用正则重写,发现正则不能实现递归括号匹配,最后用递归的方法重写了这道题.发现超级简单

只有50行....hh

递归版本:

 #include <bits/stdc++.h>
using namespace std;
map <string,string> mapp;
map <string,int> cls;
string str; int pos;
string get_name () {
string ans;
for (pos+=;str[pos]!='\"';pos++) {
if (str[pos]=='\\') pos++;
ans.push_back(str[pos]);
}
return ans;
}
void build (string _name) {
while (pos<str.size()&&str[pos]!='}') { // 不加pos<str.size()莫名错误90
pos++;
string s1=get_name(); cls[_name+s1]=;
pos+=;
if (str[pos]=='\"') {
string s2=get_name();
mapp[_name+s1]=s2;
cls[_name+s1]=;
}
else build(_name+s1+".");
pos++;
}
return ;
}
int main ()
{
int n,m; cin>>n>>m; getchar();
while (n--) {
string tmp; getline (cin,tmp);
str+=tmp;
}
string tmp=str; str="";
for (int i=;i<tmp.size();i++) {
if (tmp[i]==' ') continue;
str.push_back(tmp[i]);
}
build ("");
while (m--) {
getline (cin,tmp);
if (cls[tmp]==) cout<<"NOTEXIST\n";
else if (cls[tmp]==) cout<<"OBJECT\n";
else cout<<"STRING "<<mapp[tmp]<<"\n";
}
return ;
}

复杂版本:

 // 这个是一个只考虑了简单情况的程序
// 关键词字符串包含'{' ,',' ,':'都没有包含
#include <bits/stdc++.h>
#define none string::npos
using namespace std;
struct node {
string na;
int key;
string id;
};
vector < vector <node> > g(); int cnt;
vector < string > sv;
int mp [];
int n,m;
string trans(string str) {
int x=str.find("\"");
int y=str.rfind("\"");
str=str.substr(x+,y-x-); string ans;
for (int i=;i<str.size();i++) { //要考虑这样的情况不能直接替换 abc\\\\sx
if (str[i]=='\\') {
if (str[i+]=='\\') ans+='\\';
else ans+='\"';
i+=;
}
else ans+=str[i];
}
return ans;
}
vector <string> split (const string& str,const char flag=' ') {
vector <string> sv; string tmp;
istringstream iss(str);
while (getline(iss,tmp,flag))
sv.push_back(tmp);
return sv;
}
void match (string str) {
stack <int> st;
for (int i=;i<str.size();i++)
if (str[i]=='{') st.push(i);
else if (str[i]=='}') {
mp[st.top()]=i;
st.pop();
} // 找到大括号对应的位置
}
node ct_nt (int start,string str);
int ct_obj (int start,string str);
bool _find (int k,int x) {
if (x==) return ;
for (int i=;i<g[x].size();i++) {
if (g[x][i].na==sv[k]) {
if (k==sv.size()-) {
if (g[x][i].key==) cout<<"STRING "<<g[x][i].id<<endl;
else cout<<"OBJECT"<<endl;
return ;
}
else
return _find(k+,g[x][i].key);
}
}
return ;
}
int main ()
{
cin>>n>>m; getchar();
string str,s1;
for (int i=;i<n;i++) {
getline(cin,s1);
str+=s1;
}
match (str);
ct_obj(,str);
while (m--) {
getline(cin,str);
sv=split(str,'.');
if (!_find(,)) cout<<"NOTEXIST"<<endl;
}
return ;
}
node ct_nt (int start,string str) {
node ans;
int k=str.find(":");
string s1=str.substr(,k);
string s2=str.substr(k+);
ans.na=trans(s1);
int p=s2.find("{");
if (p==none) {// string 类
ans.key=;
ans.id=trans(s2);
}
else ans.key=ct_obj(start+k+,s2);
return ans;
}
int ct_obj (int start,string str) {
int num=++cnt;
int xpos=str.find("{");
int ypos=str.rfind("}");
str[ypos]=',';
int i=xpos+; int p;
while ( (p=str.find(",",i))!=none ) {
int k=str.find("{",i);
if (k!=none&&k<p) {
k=mp[start+k];
k=str.find(",",k-start);
}
else k=p;
if (str.substr(i,k-i)!="") {
node tmp=ct_nt(start+i,str.substr(i,k-i));
g[num].push_back(tmp);
}
i=k+;
}
return num;
}

CCF-20170903-JSON查询的更多相关文章

  1. CCF 201709-3 JSON查询

    CCF 201709-3 JSON查询 试题编号: 201709-3 试题名称: JSON查询 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 JSON (JavaScript ...

  2. 【CCF】JSON查询

    #include<iostream> #include<cstdio> #include<string> #include<cstring> #incl ...

  3. CCF CSP 201709-3 JSON查询

    CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201709-3 JSON查询 问题描述 JSON (JavaScript Object Not ...

  4. CCF(JSON查询:40分):字符串+模拟

    JSON查询 201709-3 纯字符串模拟,考的就是耐心和细心.可惜这两样我都缺... #include<iostream> #include<cstdio> #includ ...

  5. JS 实现Json查询的方法实例

    其实很简单,我这部分代码,前一部分是简单的实现如何使用JS写模板,第二个就是具体的实现了JSON查询的一个扩展. 以后查询Json就有了利器了. 代码如下: /*         * 定义模板函数   ...

  6. CCF-CSP题解 201709-3 JSON查询

    要求写一个小程序完成JSON查询的功能. 查询dfs就好了. 存储JSON对象用图(树)就好,把\(<key[],type,val[]>\)作为节点,然后又是字符串处理了. 其实就是个简化 ...

  7. CCF-CSP 201709-3 JSON查询 题解

    试题编号: 201709-3 试题名称: JSON查询 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 JSON (JavaScript Object Notation) 是一 ...

  8. ccf 201612-3 权限查询

     ccf 201612-3 权限查询 解题思路: 建立一个二维矩阵存储权限和角色 还差30分emmm #include<iostream> #include<cstring> ...

  9. MySQL全文索引、联合索引、like查询、json查询速度大比拼

    目录 查询背景 一.like查询 二.json函数查询 三.联合索引查询 四.全文索引查询 结论 查询背景 有一个表tmp_test_course大概有10万条记录,然后有个json字段叫outlin ...

  10. Unity3D 通过JSON查询天气

    一.天气查询API 获取天气信息,首先要找到提供天气数据的接口,我使用的是高德地图免费为我们提供的,网址为 https://lbs.amap.com/api/webservice/guide/api/ ...

随机推荐

  1. 剑指offer(33)丑数

    题目描述 把只包含因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含因子7. 习惯上我们把1当做是第一个丑数.求按从小到大的顺序的第N个丑数. 题目分析 ...

  2. centos7 jmeter分布式安装

    step1 环境说明:腾讯云主机--> centos7  1主2从 下面使用内网 IP master节点:10.21.11.6 slave1节点:10.21.11.44 slave2节点:10. ...

  3. 牛客练习赛26—D xor序列 —线性基

    这是我第一次写关于线性基的题目.其实这题很好理解,先把给出的数能异或出的值给存在p数组里面,p[i]代表着该异或出的数的最高位为第i位且为1. 求出来后,再把x,y处理下,然后直接一位一位的判断是否为 ...

  4. E: Unable to locate package git

    git can’t install 报错信息: root@281eef85bb5d:~# apt-get install git Reading package lists... Done Build ...

  5. 利用python操作excel

    https://zhuanlan.zhihu.com/p/51292549 打开程序:https://segmentfault.com/q/1010000002441500

  6. centos 打印机安装方法

    这里安装的是hplip 1.首先确定cups有没有安装 没有的话 yum install cups 安装 2.安装 hplip yum install -y hplip hplip-* 3执行 hp- ...

  7. 【转】 VGA时序及其原理

    显示器扫描方式分为逐行扫描和隔行扫描:逐行扫描是扫描从屏幕左上角一点开始,从左向右逐点扫描,每扫描完一行,电子束回到屏幕的左边下一行的起始位置,在这期间,CRT对电子束进行消隐,每行结束时,用行同步信 ...

  8. ubuntu文档保存出现的一些错误

    ubuntu使用vim编辑器保存时,出现了错误,虽然知道基本的保存方法,但是还不够,出现各种错误.基本的保存命令: 写入文件后退出保存:wq后,保存时出现错误E45:已设定选项“readonly”(请 ...

  9. c++中的c_str()用法

    语法: const char *c_str(); c_str()函数返回一个指向正规C字符串的指针, 内容与本string串相同. 这是为了与c语言兼容,在c语言中没有string类型,故必须通过st ...

  10. 项目开发中关于jquery中出现问题小结(textarea,disabled,关键字等)

    1.textarea: 使用 定义了一个textarea,在使用jquery的方法获取文本内容的时候总是为空. var content = $(“#content”).val();  后来测试发现,i ...