Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <set>
- #include <map>
- #include <queue>
- using namespace std;
- struct CompanyStr {
- string person_or_company;
- int percent;
- };
- class OffShore {
- set<string> person;
- map<string, vector<CompanyStr>> company;
- vector<string> all_ownersBFS (string &comp) {
- set<string> visited;
- vector<string> result;
- queue<string> checker;
- checker.push(comp);
- while (!checker.empty()) {
- vector<CompanyStr>::iterator itr; // iterator that runs through in my vector pairs.
- string curr = checker.front();
- checker.pop();
- if (company.find(curr) != company.end() && visited.find(curr) == visited.end()) { // if current queue element is a company and not visited.
- visited.insert(curr); // add company to visited (just in case we see it again while iterating)
- for (itr = company[curr].begin(); itr != company[curr].end(); ++itr) {
- if (visited.find(itr->person_or_company) == visited.end()) { // if not visited, do...
- if (person.find(itr->person_or_company) != person.end()) { // if we find the current string in persons.
- checker.push(itr->person_or_company); // Queuing.
- result.push_back(itr->person_or_company); // push to result array.
- visited.insert(itr->person_or_company); // push to visited set.
- }
- else if (company.find(itr->person_or_company) != company.end()) { // if we find the current string in company.
- vector<CompanyStr>::iterator itr2; // iterate over the current company.
- for (itr2 = company[itr->person_or_company].begin(); itr2 != company[itr->person_or_company].end(); ++itr2) {
- checker.push(itr2->person_or_company); // queue all the persons and company under the current company.
- }
- }
- }
- else continue; // if visited, continue
- }
- }
- else if (person.find(curr) != person.end() && visited.find(curr) == visited.end()) { // if current queue element is a person and not visited.
- result.push_back(curr); // push to result array.
- visited.insert(curr); // push to visited set.
- }
- }
- return result;
- }
- vector<string> all_ownersDFS (string &comp) {
- set<string> visited; // Check if i have seen the current person or company.
- vector<string> result; // final result.
- if (company.find(comp) != company.end()) { // Initial check to see if comp is in company (wrong input).
- vector<CompanyStr>::iterator itr; // iterator that runs through in my vector pairs.
- for (itr = company[comp].begin(); itr != company[comp].end(); ++itr) {
- if (visited.find(itr->person_or_company) == visited.end()) { // if not visited, do...
- if (person.find(itr->person_or_company) != person.end()) { // if we find the current string in persons.
- result.push_back(itr->person_or_company); // push to result array.
- visited.insert(itr->person_or_company); // push to visited set.
- }
- else if (company.find(itr->person_or_company) != company.end()) { // if we find the current string in company.
- all_ownersDFS(itr->person_or_company); // recursive call on company.
- }
- }
- else continue; // if visited, continue
- }
- }
- return result;
- }
- vector<pair<string, int>> all_owners_shares (string &comp) {
- }
- };
- int main() {
- OffShore curr;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement