CCF(JSON查询:40分):字符串+模拟
2024-08-31 02:30:34
JSON查询
201709-3
纯字符串模拟,考的就是耐心和细心。可惜这两样我都缺。。。
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
const int maxn=102;
int n,m;
string row[maxn];
map<string,string> attr;
int main(){
cin>>n>>m;
getchar();
for(int i=0;i<n;i++){
getline(cin,row[i]);
string temp="";
for(int j=0;j<row[i].length();j++){
if(row[i][j]!=' '){
temp+=row[i][j];
}
}
row[i]=temp;
}
// for(int i=0;i<n;i++)
// cout<<row[i]<<endl;
int times=0;
string temp="";
string pre;
int cnt=0;
for(int i=0;i<n;i++){
bool flag=false;
for(int j=0;j<row[i].length();j++){
if(row[i][j]=='{'&&j==0){
continue;
}else if(row[i][j]=='}'&&j==row[i].length()-1){
continue;
}else if(row[i][j]=='\"'&&!flag&&(j==0||row[i][j-1]!='\\')){
flag=true;
cnt++;
}else if(row[i][j]=='\"'&&flag&&row[i][j-1]!='\\'){
flag=false;
cnt++;
if(times>0){//已经出现过冒号了
attr[pre]=temp;
times=0;
//cout<<pre<<" "<<temp<<endl;
}else{//还没有出现过冒号
pre=temp;
}
//cout<<pre<<" "<<temp<<endl;
temp="";
}else if(row[i][j]==':'){//键和值
if(cnt%2==0){
times++;
temp="";
}else temp+=row[i][j];
}else if(row[i][j]==','){
if(cnt==4){
temp="";
cnt=0;
}else temp+=row[i][j];
}else{
temp+=row[i][j];
}
}
}
for(int i=0;i<m;i++){
string key;
cin>>key;
temp="";
for(int j=0;j<key.length();j++){
if(key[j]=='\\'){
temp+='\\';
}
if(key[j]=='\"'){
temp+='\\';
}
temp+=key[j];
}
//cout<<temp<<endl;
if(!attr.count(temp)){
cout<<"NOTEXIST"<<endl;
}else{
cout<<"STRING ";
string now=attr[temp];
bool flag=false;
for(int j=0;j<now.length();j++){
if(now[j]=='\"'&&now[j-1]=='\\')
flag=false;
if(now[j]=='\\'&&!flag){
flag=true;
continue;
}
if(now[j]=='\\'&&flag)
flag=false;
cout<<now[j];
}
cout<<endl;
}
}
//system("pause");
return 0;
}
最新文章
- C# 读取excel日期时获取到数字转换成日期
- Safari 快捷键
- ISO 基础之 (十二) 文件管理
- angular源码分析 摘抄 王大鹏 博客 directive指令及系列
- OD调试4--绕过nag窗口
- Altera USB Blaster 仿真器(EPM240仿制版
- Codeforces Round #290 (Div. 2) C. Fox And Names dfs
- c语言指针用法
- JDK Linux环境配置
- java数组、java.lang.String、java.util.Arrays、java.lang.Object的toString()方法和equals()方法详解
- Vijos1523贪吃的九头龙【树形DP】
- [补档][COGS 2434]暗之链锁
- junit initializationError和找不到或无法加载主类
- Mycat 分片规则详解--自然月分片
- python网络爬虫与信息提取 学习笔记day3
- 剑指Offer——知识点储备-网络基础
- Rest API
- vue移动端适配
- 让Windows Server 2008r2 IIS7.5 ASP.NET 支持10万并发请求
- Selenium3详解(基本操作,定位方法)