CSP-201809-3 元素选择器

 

 给出的就是一棵树的dfs序列,把树建立好。然后dfs询问就好了。我把所有不同的的属性和id都用一个唯一的正数离散化了,方便判断,其实直接比较string也可以,可以少写不少代码= =。

我把一个整数数组和一个string起了一样的名字,然后string一直乱码检查半天,c++太迷了为什么不报错= =

然后注意大小写敏感问题,要是不判断这个只有90.

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn=110;
 4 
 5 
 6 int name[maxn],id[maxn];
 7 map<string,int>NAME;
 8 int tot1=0,tot2=499;
 9 int dep[maxn];
10 int n,m;
11 vector<int>g[maxn],ans,element;
12 bool vis[1010];
13 void solve(int u,int cur,int tar){
14     vis[name[u]]=1;
15     if(id[u]!=-1)vis[id[u]]=1;
16     if(name[u]==element[cur] || id[u]==element[cur]){
17         if(cur+1==tar){ans.push_back(u);}
18         else{cur=cur+1;}
19     }
20     for(auto v:g[u]){
21         solve(v,cur,tar);
22     }
23     vis[name[u]]=0;
24     if(id[u]!=-1)vis[id[u]]=0;
25 }
26 void tol(string &s){
27     for(char &c:s){
28         if(c>='A' && c<='Z')c=c-'A'+'a';
29     }
30 }
31 int main(){
32     string str,_name,_id;
33     cin>>n>>m;getchar();
34     for(int i=1;i<=n;++i){str="";
35         id[i]=-1;
36     getline(cin,str);
37         int j=0,len=str.length();
38         dep[i]=0;_name="";_id="";
39         while(str[j]=='.'){j+=2;dep[i]++;}
40         while(j<len && str[j]!=' ' && str[j]!='#'){_name+=(char)(str[j++]);}j++;
41         while(j<len && str[j]!=' '){_id+=str[j++];}
42         tol(_name);
43         if(!NAME.count(_name)){NAME[_name]=++tot1;}
44         if(_id!=""&&!NAME.count(_id)){NAME[_id]=++tot2;};
45         name[i]=NAME[_name];
46         if(_id!="") id[i]=NAME[_id];
47     }
48     for(int i=1;i<=n;++i){
49         int cur=dep[i];
50         for(int j=i+1;j<=n&&dep[j]>cur;++j){
51             if(dep[j]==cur+1) g[i].push_back(j);
52         }
53     }string q,f;
54     while(m--){
55         memset(vis,0,sizeof(vis));
56         element.clear();
57         ans.clear();
58         getline(cin,q);
59         stringstream ss(q);
60         while(ss>>f){if(f[0]!='#') tol(f);element.push_back(NAME[f]);}
61         solve(1,0,element.size());
62     cout<<ans.size();for(auto v:ans)cout<<" "<<v;cout<<endl;
63     }
64     return 0;
65 }
66 /*
67 11 5
68 html
69 ..head
70 ....title
71 ..body
72 ....h1
73 ....p #subtitle
74 ....div #main
75 ......h2
76 ......p #one
77 ......div
78 ........p #two
79 p
80 #subtitle
81 h3
82 div p
83 div div p
84 */
原文地址:https://www.cnblogs.com/zzqc/p/12396494.html