Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

1 #include <cstdio>

2 #include <vector>
3 #include <iostream>
4 #include <sstream>
5 using namespace std;
6
7 int search_for_tag(size_t start, string s_end, vector<string> &vec, string needle) {
8 int curr = start;
9 int end = vec.size();
10 string endcur;
11 char buff[200];
12 while(true) {
13 if(vec[curr][0] == '|') {
14 //see if its the one
15 if(!vec[curr].compare(needle)) {
16 return curr;
17 //see if its the end
18 } else if (!vec[curr].compare(s_end) ){
19 return -1;
20 //pass this tag
21 } else {
22 vec[curr].copy(buff,vec[curr].size(),0);
23 buff[vec[curr].size()] = '\0';
24 endcur = "|" + string(buff);
25 endcur.replace(1,1,"/");
26 while(vec[curr].compare(endcur)) {
27 curr++;
28 }
29 curr++;
30 if(curr == end) return -1;
31 }
32 } else {
33 while(vec[curr][0] != '|') {
34 curr ++;
35 }
36 }
37 }
38 }
39
40 int search_for_att(size_t start, string s_end, vector<string> &vec, string needle) {
41 int curr = start;
42 while(vec[curr][0] != '|'){
43 if(!vec[curr].compare(needle)) {
44 return curr;
45 }
46 curr++;
47 if(curr == vec.size()) return -1;
48 }
49 return -1;
50 }
51
52 int main() {
53 int tlines, qlines;
54 string ints;
55 getline(cin, ints);
56 stringstream ss(ints);
57 ss >> tlines;
58 ss >> qlines;
59
60 string line;
61 char buf[200];
62 size_t del_tag;
63 vector<string> contain;
64 //Parse the shenanigans
65 for(int i=0; i<tlines; i++) {
66 getline(cin, line);
67 del_tag = line.find_first_of(" >");
68 //Get Tag
69 line.copy(buf,del_tag-1, 1);
70 buf[del_tag-1] = '\0';
71 string tagbuf(buf);
72 tagbuf = "|" + tagbuf;
73 contain.push_back(tagbuf);
74 size_t found = line.find("=", del_tag);
75 while(found != string::npos) {
76 //Get the attribute
77 line.copy(buf, found-del_tag-2, del_tag + 1);
78 buf[found-del_tag-2] = '\0';
79 string attbuf(buf);
80 contain.push_back(attbuf);
81 //move past the =
82 del_tag = found + 2;
83 found = line.find("\"", del_tag + 1);
84 //Copy the value
85 line.copy(buf,found - del_tag - 1, del_tag + 1);
86 buf[found-del_tag -1] = '\0';
87 string valbuf(buf);
88 contain.push_back(valbuf);
89 del_tag = line.find(" ", found);
90 found = line.find("=", found);
91 }
92 }
93 //Query Time
94 for(int i = 0; i < qlines; i++) {
95 size_t startString = 0;
96 bool typeTag = true;
97 size_t startVec = 0;
98 string endVec ="";
99 bool notFound = true;
100 string attval;
101 string line;
102 getline(cin,line);
103 while (true) {
104 //Get next token
105 size_t found = line.find_first_of(".~", startString);
106 if(found == string::npos) found = line.size();
107 line.copy(buf, found - startString, startString);
108 buf[found - startString] = '\0';
109 string tagbuf(buf);
110
111 //Check if its a Tag
112 if(startString > 0 && line[startString-1] == '~') {
113 typeTag = false;
114 } else {
115 tagbuf = "|" + tagbuf;
116 }
117 //Find a tag
118 if(typeTag) {
119 int res = search_for_tag(startVec,endVec,contain,tagbuf);
120 if(res<0) {
121 break;
122 } else {
123 startVec = res+1;
124 endVec = "|" + tagbuf;
125 endVec.replace(1,1,"/");
126 startString = found + 1;
127 }
128 } else {
129 //Find an attibute
130 int res = search_for_att(startVec,endVec,contain,tagbuf);
131 if(res >=0) {
132 attval = contain[res + 1];
133 notFound = false;
134 }
135 break;
136 }
137 }
138 //Printing
139 if(notFound) {
140 cout << "Not Found!" << endl;
141 } else {
142 cout << attval << endl;
143 }
144 }
145 return 0;
146 }
147

You might also like