这道题当时考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. 浅谈HTTP中GET、POST用法以及它们的区别

    浅谈HTTP中GET.POST用法以及它们的区别 HTTP定义了与服务器交互的不同方法,最基本的方法有4种,分别是GET,POST,PUT,DELETE.URL全称是资源描述符.我们可以这样认为: 一 ...

  2. Anaconda环境下安装库

    使用anaconda环境下使用pycharm后,有些其他库也想安装,但开始在python.exe目录下安装没成功,因为pycharm用的环境已经不是原始的python.exe解释器了.就总结了一些安装 ...

  3. 查找IDEA 项目中的依赖包存放在.m2位置

    原因:在maven项目pom.xml中添加依赖,可能由于网络不好,下载不完全,导致再次下载一直报错. 就会导致出现依赖一直报错.

  4. 多路径multipath配置,udev绑定

    多路径multipath配置 以root用户登录 1.查看共享磁盘是否挂载成功 #fdisk -l 2.生成配置文件 #mpathconf --enable 修改配置文件权限 #chmod 644 / ...

  5. Lab 7-3

    For this lab, we obtained the malicious executable, Lab07-03.exe, and DLL, Lab07-03.dll, prior to ex ...

  6. HDU - 1061-快速幂签到题

    快速幂百度百科:快速幂就是快速算底数的n次幂.其时间复杂度为 O(log₂N), 与朴素的O(N)相比效率有了极大的提高. HDU - 1061 代码实现如下: import java.util.Sc ...

  7. idea启动springboot项目 报错:java.lang.NoSuchMethodError: javax.servlet.ServletContext.getClassLoader()Ljava/lang/ClassLoader;

    有一次启动springboot项目的时候,报了一个非常奇怪的错误,说是找不到servletContext,springboot不是自带tomcat的吗? 在网上找了好久,说是用以下方式解决. 解决方式 ...

  8. 『TensorFlow』读书笔记_SoftMax分类器

    开坑之前 今年3.4月份的时候就买了这本书,同时还买了另外一本更为浅显的书,当时读不懂这本,所以一度以为这本书很一般,前些日子看见知乎有人推荐它,也就拿出来翻翻看,发现写的的确蛮好,只是稍微深一点,当 ...

  9. 如何 distinct 只对一个字段有用,同时查出其他字段

    转至:http://blog.csdn.net/u013402772/article/details/51262524 在使用MySQL时,有时需要查询出某个字段不重复的记录,虽然mysql提供 有d ...

  10. AutoMapper实现对象转换的几种方式

    namespace ConsoleApplication1 { class Program { static void Main(string[] args) { //1.普通转换 Name name ...