You are viewing a single comment's thread. Return to all comments →
using namespace std;
typedef struct attribute{ string name; string value; }attribute;
class TAG{ public: TAG* getChild(const string& _childName){ TAG* child; for(auto& item: m_child){ if(item->m_name == _childName){ child = item; } } return child; } string getAttribute(const string& _name){ for(const auto& i: m_attribute){ if(_name == i.name) return i.value; } return "Not Found!"; } bool m_isRootTag = true; string m_name; vector m_child; vector m_attribute; };
vector g_tagList; vector g_tagScope;
string getEndOfTag(string _tag){ string EOTag; EOTag.append("/").append(_tag); return EOTag; }
string trimTagInputString(string _string){ auto tempString(std::move(_string)); for(char & i : tempString){ if(i == '>' || i == '<' || i == '=' || i == '"'){ i = ' '; } } return tempString; }
TAG* getTagByName(const string& _name){ TAG* tag = (TAG*)malloc(sizeof(TAG)); for(auto& item: g_tagList){ if(item.m_name == _name){ tag = &item; return tag; } } return tag; }
string getTagAttribute(const vector& _tagSerial, const string& _attName){ string att("Not Found!");
if(!getTagByName(_tagSerial.front())->m_isRootTag){ return att; } TAG* tag = getTagByName(_tagSerial[0]); if(!tag){ return att; } for(int i = 1; i < _tagSerial.size(); i++){ tag = tag->getChild(_tagSerial[i]); if(!tag){ return att; } } for(const auto& item: tag->m_attribute){ if(item.name == _attName) att = item.value; } return att;
}
int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */
int noTag, noQuery; cin >> noTag >> noQuery; while (getc(stdin) != '\n'); for(int i = 0; i < noTag; i++){ TAG tempTag; string line; getline(cin, line, '\n'); line = trimTagInputString(line); stringstream ss(line); ss >> tempTag.m_name; g_tagScope.emplace_back(tempTag.m_name); if(tempTag.m_name[0] != '/'){ while(!ss.eof()){ attribute tempAttribute; ss >> tempAttribute.name >> tempAttribute.value; if(!tempAttribute.name.empty()){ tempTag.m_attribute.emplace_back(tempAttribute); } } g_tagList.emplace_back(tempTag); } } for(auto& currentTag: g_tagList){ auto startPos = find(g_tagScope.begin(), g_tagScope.end(), currentTag.m_name); auto stopPos = find(g_tagScope.begin(), g_tagScope.end(), getEndOfTag(currentTag.m_name)); string tempChild; for(auto i = startPos + 1; i < stopPos; i++){ if(tempChild.empty()){ tempChild = *i; } if(*i == getEndOfTag(tempChild)){ currentTag.m_child.emplace_back(getTagByName(tempChild)); getTagByName(tempChild)->m_isRootTag = false; tempChild.erase(); } } } for(int i = 0; i < noQuery; i ++){ string line; getline(cin, line, '\n'); stringstream lineSs(line); string attName; string tagSting; getline(lineSs, tagSting, '~'); lineSs >> attName; stringstream tagSs(tagSting); string tagTarget; vector<string> tagSerial; do{ getline(tagSs, tagTarget, '.'); tagSerial.emplace_back(tagTarget); }while(!tagSs.eof()); cout << getTagAttribute(tagSerial, attName) <<endl; } return 0;
Seems like cookies are disabled on this browser, please enable them to open this website
Attribute Parser
You are viewing a single comment's thread. Return to all comments →
include
include
include
include
include
include "memory"
include "algorithm"
using namespace std;
typedef struct attribute{ string name; string value; }attribute;
class TAG{ public: TAG* getChild(const string& _childName){ TAG* child; for(auto& item: m_child){ if(item->m_name == _childName){ child = item; } } return child; } string getAttribute(const string& _name){ for(const auto& i: m_attribute){ if(_name == i.name) return i.value; } return "Not Found!"; } bool m_isRootTag = true; string m_name; vector m_child; vector m_attribute; };
vector g_tagList; vector g_tagScope;
string getEndOfTag(string _tag){ string EOTag; EOTag.append("/").append(_tag); return EOTag; }
string trimTagInputString(string _string){ auto tempString(std::move(_string)); for(char & i : tempString){ if(i == '>' || i == '<' || i == '=' || i == '"'){ i = ' '; } } return tempString; }
TAG* getTagByName(const string& _name){ TAG* tag = (TAG*)malloc(sizeof(TAG)); for(auto& item: g_tagList){ if(item.m_name == _name){ tag = &item; return tag; } } return tag; }
string getTagAttribute(const vector& _tagSerial, const string& _attName){ string att("Not Found!");
}
int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */
}