这道题当时考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. Win10在右键菜单添加在此处打开命令窗口项

    转载:https://jingyan.baidu.com/article/2d5afd6930107a85a2e28e2d.html 第一步:新建一个文本文档,输入如下代码,然后另存为OpenCmdH ...

  2. Bootstrap3基础 warning/active... 表格的状态类(不同的背景色)

      内容 参数   OS   Windows 10 x64   browser   Firefox 65.0.2   framework     Bootstrap 3.3.7   editor    ...

  3. centos6.5安装zabbix3.2

    1.安装PHP Zabbix 3以后对PHP的要求最低为5.4,而CentOS6默认为5.3.3,完全不满足要求,故需要利用第三方源,将PHP升级到5.4以上,注意,不支持PHP7 rpm -ivh  ...

  4. redux-thunk的理解

    问题:1.redux-thunk要解决什么问题? 要解决异步请求问题,Action发出以后,Reducer立即算出State,这叫做同步:Action发出以后,过一段时间再执行 Reducer,这就叫 ...

  5. GEC6818交叉开发环境搭建拟稿

    为manjaro linux配置交叉工具链 为gec6818开发版下载一个arm-2014.05-29-arm-none-linux-gnueabi-i686-pc-linux-gnu.tar.bz2 ...

  6. Vue父子组件传值 | 父传子 | 子传父

    父传子 父容器 <template> <div> <zdy :module='test'></zdy> </div> </templa ...

  7. 小程序之 tab切换(选项卡)

    好久没有写东西了   今天写一个简单的东西 小程序tab切换 (选项卡功能) .wxml <view class="swiper-tab"> <view < ...

  8. 【SMTP】常见错误码

    '* 邮件服务返回代码含义 '* 500 格式错误,命令不可识别(此错误也包括命令行过长) '* 501 参数格式错误 '* 502 命令不可实现 '* 503 错误的命令序列 '* 504 命令参数 ...

  9. webpack点滴

    一个比较完整的webpack的配置,自己配置不断更新. const path = require('path') const configs = require('./configs/') const ...

  10. VXLAN, 一种叠加在L3网络上的L2网络

    这几天看了下RFC7348,顺便翻译了一下,根据自己理解做了注解 虚拟化及租户隔离 服务器虚拟化增加了对物理网络基础设施的需求,服务器有多个虚机,要求交换机支持更大的MAC地址表. 在数据中心场景下, ...