nikunjsoni

1236

May 4th, 2021
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  1. /**
  2.  * // This is the HtmlParser's API interface.
  3.  * // You should not implement it, or speculate about its implementation
  4.  * class HtmlParser {
  5.  *   public:
  6.  *     vector<string> getUrls(string url);
  7.  * };
  8.  */
  9.  
  10. class Solution {
  11. public:
  12.     vector<string> crawl(string startUrl, HtmlParser htmlParser) {
  13.         vector<string> ans;
  14.         unordered_set<string> m;
  15.         string hostname = startUrl.substr(0, startUrl.find('/', 7));
  16.         queue<string> q;
  17.         q.push(startUrl);
  18.         m.insert(startUrl);
  19.         ans.push_back(startUrl);
  20.         while(!q.empty()){
  21.             string url = q.front(); q.pop();
  22.             for(auto child: htmlParser.getUrls(url)){
  23.                 if(child.find(hostname) == string::npos || m.count(child))
  24.                     continue;
  25.                 m.insert(child);
  26.                 q.push(child);
  27.                 ans.push_back(child);
  28.             }
  29.         }
  30.         return ans;
  31.     }
  32. };
Advertisement
Add Comment
Please, Sign In to add comment