-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2018-9-T3.cpp
258 lines (250 loc) · 4.39 KB
/
2018-9-T3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/**
* Topic: CSP-2018-9-样式选择器
* Author: Bao Wenjie
* Email: [email protected]
* Date: 2020/8/21
*/
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
using namespace::std;
struct Line {
int tag;
string id;
int tab;
vector<int> parents;
};
typedef Line* pLine;
map<string, int> Tag2Num;
bool search(string querry, pLine* doc, int index);
vector<int> find(int tag, string id, pLine* doc, int n);
int main()
{
// 输入
int n, m;
scanf_s("%d%d", &n, &m);
string s;
getline(cin, s);
pLine* doc = new pLine[n];
// 读取文档
for (int i(0); i < n; i++)
{
getline(cin, s);
pLine tmp = new Line;
int tab = 0;
int start = -1;
int blank = 0;
for (int j(0); j < s.length(); j++)
{
if (s[j] == '.')
tab++;
else
{
if (start == -1)
start = j;
else
{
if (s[j] == ' ')
blank = j;
}
}
}
// 无属性标签
if (blank == 0)
{
string tag = s.substr(start, s.length() - start);
int tag_num = Tag2Num.size();
map<string, int>::iterator itor = Tag2Num.find(tag);
// 已有该标签
if (itor != Tag2Num.end())
{
tag_num = itor->second;
}
else
{
Tag2Num.insert(pair<string, int>(tag, tag_num));
}
tmp->tab = (tab /= 2);
tmp->tag = tag_num;
tmp->id = "";
// 寻找父节点
for (int j(i - 1); j >= 0; j--)
{
if (doc[j]->tab == tab - 1)
tmp->parents.push_back(j);
else
break;
}
}
else
{
string tag = s.substr(start, blank - start);
string id = s.substr(blank + 1, s.length() - blank - 1);
int tag_num = Tag2Num.size();
map<string, int>::iterator itor = Tag2Num.find(tag);
// 已有该标签
if (itor != Tag2Num.end())
{
tag_num = itor->second;
}
else
{
Tag2Num.insert(pair<string, int>(tag, tag_num));
}
tmp->tab = (tab /= 2);
tmp->id = id;
tmp->tag = tag_num;
// 寻找父节点
for (int j(i - 1); j >= 0; j--)
{
if (doc[j]->tab == tab - 1)
tmp->parents.push_back(j);
else
break;
}
}
//
doc[i] = tmp;
}
// 读入查询
vector<vector<int>> ans;
for (int i(0); i < m; i++)
{
getline(cin, s);
string tag = "";
string id = "";
int end = s.length();
for (int j = (s.length() - 1); j>= 0; j--)
{
if (s[j] == ' ')
{
// 属性
if (s[j + 1] == '#')
{
if (id != "")
{
break;
}
id = s.substr(j + 1, end - j);
end = j;
continue;
}
// 标签
else
{
tag = s.substr(j + 1, end - j);
end = j;
break;
}
}
}
if (tag == "" && id == "")
{
if (s[0] == '#')
id = s;
else
tag = s;
end = 0;
}
int tag_;
if (tag == "")
tag_ = -1;
else
{
transform(tag.begin(), tag.end(), tag.begin(), ::tolower);
map<string, int>::iterator itor = Tag2Num.find(tag);
// 不存在该标签
if (itor == Tag2Num.end())
tag_ = -2;
else
tag_ = itor->second;
}
vector<int> vec = find(tag_, id, doc, n);
string querry = s.substr(0, end);
vector<int>::iterator itor_vec = vec.begin();
while(itor_vec != vec.end())
{
if (search(querry, doc, *itor_vec) == false)
{
itor_vec = vec.erase(itor_vec);
}
else
{
itor_vec++;
}
}
vec.insert(vec.begin(), vec.size() - 1);
ans.push_back(vec);
}
vector<vector<int>>::iterator itor = ans.begin();
while (itor != ans.end())
{
vector<int>::iterator innor_itor = itor->begin();
while (innor_itor != itor->end())
{
printf("%d ", *innor_itor + 1);
innor_itor++;
}
printf("\n");
itor++;
}
}
/**
* 判断在index之前是否存在查询
*/
bool search(string querry, pLine* doc, int index)
{
if (querry == "")
return true;
else
{
int i;
for (i = querry.length() - 1; i >= 0; i--)
{
if (querry[i] == ' ')
break;
}
string tag;
if (i == -1)
{
tag = querry;
querry = "";
}
else
{
tag = querry.substr(i + 1, querry.length());
querry = querry.substr(0, i);
}
map<string, int>::iterator itor = Tag2Num.find(tag);
vector<int> vec = doc[index]->parents;
bool isOK = false;
vector<int>::iterator itor_vec = vec.begin();
while (itor_vec != vec.end())
{
if (search(querry, doc, *itor_vec))
{
isOK = true;
break;
}
itor_vec++;
}
return isOK;
}
}
vector<int> find(int tag, string id, pLine* doc, int n)
{
vector<int> vec;
for (int i(0); i < n; i++)
{
if (tag == -1 || tag == doc[i]->tag)
{
if (id == "" || id == doc[i]->id)
{
vec.push_back(i);
}
}
}
return vec;
}