【!!最近写了好多C题,打算先放代码供大家参考,之后再补充题解!!】
题目大意
考点
思路
易错点
收获
满分代码
#include<bits/stdc++.h>
using namespace std;
const int N=8010;
#define fi first
#define se second
#define pb push_back
typedef pair<int,int> PII;
typedef pair<string,int> PSI;
typedef struct OBJ{
int neobj;
string str;
}obj;
map<PSI,obj> mp;
int n,m,idx,row,col,stk[N],stktop;
bool stop;
string req,s;
int find_ref_or_kuoh(){
int len=s.length();
if(col>=len){
getline(cin,s);
col=0;
row++;
}
while(1){
len=s.length();
while(col<len){
if(s[col]=='"'){
col++;
return 1;
}
if(s[col]=='{'){
col++;
idx++;
stk[stktop++]=idx;
return 0;
}
col++;
}
getline(cin,s);
col=0;
row++;
}
}
string find_key(bool find_lef){
int len=s.length();
if(col>=len){
getline(cin,s);
col=0;
row++;
}
if(find_lef){
while(1){
len=s.length();
bool flag=0;
while(col<len){
if(s[col]=='"'){
col++;
flag=1;
break;
}
else if(s[col]=='}'){
stktop--;
if(stktop==0){
stop=1;
return ".";
}
}
col++;
}
if(flag)break;
getline(cin,s);
col=0;
row++;
if(row>n)return "..";
}
}
len=s.length();
if(col>=len){
col=0;
getline(cin,s);
row++;
}
string res;
len=s.length();
while(col<len){
if(s[col]=='"'){
col++;
return res;
}
if(s[col]=='\\'){
res+=s[++col];
col++;
}
else res+=s[col++];
}
return res;
}
bool sol(){
int len=req.length(),i=0,cur=1;
string ans;
while(i<len){
string v;
while(i<len && req[i]!='.')v+=req[i++];
if(!mp.count({v,cur}))return 0;
if(mp[{v,cur}].neobj>=0)cur=mp[{v,cur}].neobj;
else{
if(i<len)return 0;
cout<<"STRING "<<mp[{v,cur}].str<<endl;
return 1;
}
i++;
}
cout<<"OBJECT\n";
return 1;
}
int main(){
cin>>n>>m;
char c=getchar();
if(n){
getline(cin,s);
row++;
col=0;
find_ref_or_kuoh();
}
int op;
while(row<=n){
string skey;
obj val;
skey=find_key(1);
int cur=stk[stktop-1];
if(stop){
while(row<n)
{
getline(cin,s);
row++;
}
break;
}
op=find_ref_or_kuoh();
if(op){
val.str=find_key(0);
if(row>n)break;
val.neobj=-1;
}
else{
val.neobj=stk[stktop-1];
}
mp[{skey,cur}]=val;
}
while(m--){
cin>>req;
if(!sol())cout<<"NOTEXIST\n";
}
return 0;
}