We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- C++
- Strings
- Attribute Parser
- Discussions
Attribute Parser
Attribute Parser
+ 0 comments can anyone help me with the code??
#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> #include <sstream> using namespace std; class Tags { vector<string> attributes, values; int attIndex(string& att) { for (int i = 0; i < attributes.size(); i++) { if (attributes[i] == att) { return i; } } return -1; } public: void pushValues(string& att, string& val) { attributes.push_back(att); values.push_back(val); } void printQuery(string& att) { int index = attIndex(att); if (index == -1) { cout << "Not Found" << '\n'; } else { cout << values[index] << '\n'; } } }; class HRML { vector<Tags> ts; int n, q; public: void setTags() { int equals = 0; Tags t; char c; string input, atts, vals; stringstream ss; string temp; cin >> n >> q; ts.resize(n); // Consume the newline character after reading n and q cin.ignore(); for (int i = 0; i < n / 2; i++) { getline(cin, input); ss.clear(); ss.str(input); ss >> temp; while (ss >> temp) { if (temp == "=" && equals == 0) { equals = 1; continue; } else if (equals == 1) { vals = temp; ts[i].pushValues(atts, vals); equals = 0; } else if (equals == 0) { atts = temp; } } } for (int i = 0; i < n / 2; i++) { cin >> temp; }; } void printQueries() { stringstream ss1; int tagno = 0; string input, temp, tagName; for (int i = 0; i < q; i++) { getline(cin, input, '~'); ss1.clear(); ss1.str(input); getline(cin, tagName); tagno = 0; while (ss1 >> temp) { tagno++; } // Check if tagno is within bounds if (tagno > 0 && tagno <= n) { ts[tagno - 1].printQuery(tagName); // Adjusted index } else { cout << "Not Found" << '\n'; } } } }; int main() { HRML a; a.setTags(); a.printQueries(); return 0; }
+ 0 comments Did my best to use the clean code advices from "the book". This has actually helped me to debug easily not gonna lie
#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> #include <sstream> #include <map> #include <queue> #include <regex> using namespace std; void parseOpeningTag( string* tag, string* tagChain, map<string, string>* hrmlData ) { stringstream ss(*tag); string element, attr, value; while(!ss.eof()) { ss >> element; if(element[0] == '<') { element = regex_replace(element, regex("<"), ""); *tagChain += tagChain->empty() ? element : "." + element; } else { element = regex_replace(element, regex("="), " "); stringstream ss(element); ss >> attr >> value; *tagChain += "~" + attr; (*hrmlData)[*tagChain] = value; tagChain->erase(tagChain->find("~")); } } } void parseClosingTag(string* tag, string* tagChain) { *tag = regex_replace(*tag, regex("</"), ""); size_t pos = tagChain->find("." + *tag); if (pos != std::string::npos) { tagChain->erase(pos); } else { tagChain->erase(tagChain->find(*tag)); } } void parseHRMLAttributes( string* tag, string* tagChain, map<string, string>* hrmlData ) { if((*tag).find("</") == string::npos) parseOpeningTag(tag, tagChain, hrmlData); if((*tag).find("</") != string::npos) parseClosingTag(tag, tagChain); } int main() { string line; getline(cin, line); istringstream iss(line); int n, q; iss >> n >> q; map<string, string> hrmlData; /*----------------{Parse HRML}---------------*/ string tag; string tagChain; while (n--) { getline(cin, tag); tag = regex_replace(tag, regex(" = "), "="); tag = regex_replace(tag, regex("\""), ""); tag = regex_replace(tag, regex(">"), ""); parseHRMLAttributes(&tag, &tagChain, &hrmlData); } /*---------------{Execute Queries}-------------*/ string query; while (q--) { getline(cin, query); if (hrmlData.find(query) == hrmlData.end()) { cout << "Not Found!" << endl; } else { cout << hrmlData[query] << endl; } } return 0; }
+ 0 comments #include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; #include <sstream> #include <map> #include <queue> int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ priority_queue<string> keys; map<string, map<string, string>> data; int dataCount; int queryCount; // read the number of data and query string line; getline(cin, line); istringstream iss(line); iss >> dataCount >> queryCount; for(int i = 0; i < dataCount; i++) { getline(cin, line); line.pop_back(); istringstream iss(line); string token; string key = ""; string value = ""; string operation = ""; iss >> token; if(token[1] == '/') { keys.pop(); continue; } if(token[0] == '<') { if(keys.empty()) { keys.push(token.substr(1)); } else { keys.push(keys.top() + '.' + token.substr(1)); } } while(iss >> token) { if(key == "") { key = token; continue; } if(operation == "") { operation = token; continue; } if(value == "") { value = token; data[keys.top()][key] = value.substr(1, value.length()-2); key = ""; operation = ""; value = ""; continue; } } } for(int i = 0; i < queryCount; i++) { getline(cin, line); size_t delimiterPos = line.find('~'); if(data[line.substr(0, delimiterPos)][line.substr(delimiterPos + 1)].empty()) { cout << "Not Found!" << endl; } else { cout << data[line.substr(0, delimiterPos)][line.substr(delimiterPos + 1)] << endl; } } return 0; }
+ 0 comments input 1: 10 1 <a value = "GoodVal"> <b value = "BadVal" size = "10"> </b> <c height = "auto"> <d size = "3"> <e strength = "2"> </e> </d> </c> </a> d~sze{-truncated-}
output : 3
how this is giving 3 ? the query does not have attribute "size" ???
+ 0 comments #include <vector> #include <iostream> #include <map> using namespace std; string join(const vector<string>& tokens, const char& delimiter); int find_non_whitespace(const string& s, const int& start); int prev_whitespace(const string& s, const int& start); int main() { int n, q; cin >> n >> q; cin.ignore(); vector<string> hrml, queries; map<string, string> m; string input; while (n--) { getline(cin, input); hrml.push_back(input); } while (q--) { getline(cin, input); queries.push_back(input); } vector<string> tag_strings; for (string line : hrml) { int begin = line.find('<', 0), end; string t_str; while ((end = line.find('>', begin)) != string::npos) { t_str = line.substr(begin + 1, end - begin - 1); begin = line.find('<', end); tag_strings.push_back(t_str); } } vector<string> tags; for (string t_str : tag_strings) { vector<string> attrs; if (t_str[0] != '/') { int tag_begin = find_non_whitespace(t_str, 0); int tag_end = t_str.find(' ', tag_begin); tags.push_back(t_str.substr(tag_begin, tag_end)); int a_begin = find_non_whitespace(t_str, tag_end); int a_end; string attr; while ((a_end = t_str.find('=', a_begin)) != string::npos) { int trim_end = prev_whitespace(t_str, a_end); attr = t_str.substr(a_begin, trim_end - a_begin); attrs.push_back(attr); a_begin = t_str.find('"', a_end) + 1; a_end = t_str.find('"', a_begin + 1); string k = join(tags, '.') + "~" + attrs[attrs.size() - 1]; string v = t_str.substr(a_begin, a_end - a_begin); m[k] = v; a_begin = find_non_whitespace(t_str, a_end + 1); } } else tags.pop_back(); } for (string query : queries) { if(m.count(query)) cout << m[query] << endl; else cout << "Not Found!" << endl; } return 0; } string join(const vector<string>& tokens, const char& delimiter) { if (tokens.size() == 0) return ""; if (tokens.size() == 1) return tokens[0]; string delimited_string = tokens[0] + delimiter; for (int i = 1; i < tokens.size(); i++) { if (i < tokens.size() - 1) { delimited_string += tokens[i] + delimiter; } else delimited_string += tokens[i]; } return delimited_string; } int find_non_whitespace(const string& s, const int& start) { for (int i = start; i < s.length(); i++) { if (!isspace(s[i])) return i; } return s.length(); } int prev_whitespace(const string& s, const int& start) { for (int i = start - 1; i > 0; i--) { if (!isspace(s[i])) return i + 1; } return 0; }
Load more conversations
Sort 422 Discussions, By:
Please Login in order to post a comment