Problem p00884 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;

map<string, set<string>> memo;

set<string> solve(const pair<string, vector<string>> &base,
                  const vector<pair<string, vector<string>>> &g) {
  set<string> ans;
  for (int i = 0; i < base.second.size(); ++i) {
    bool found = false;
    for (int j = 0; j < g.size() && !found; ++j) {
      if (base.second[i] == g[j].first) {
        found = true;
        map<string, set<string>>::iterator it = memo.find(base.second[i]);
        if (it != memo.end()) {
          ans.insert(it->second.begin(), it->second.end());
        } else {
          set<string> s = solve(g[j], g);
          memo.insert(pair<string, set<string>>(base.second[i], s));
          ans.insert(s.begin(), s.end());
        }
      }
    }
    if (!found)
      ans.insert(base.second[i]);
  }
  return ans;
}

pair<string, vector<string>> split(const string str) {
  int s = 0, t;
  t = str.find_first_of(":", s);
  string key = str.substr(0, t);
  s = t + 1;

  vector<string> value;
  do {
    t = str.find_first_of(",", s);
    value.push_back(str.substr(s, t - s));
    s = t + 1;
  } while (t != string::npos);

  return pair<string, vector<string>>(key, value);
}

int main() {
  int n;
  while (cin >> n) {
    if (n == 0)
      break;

    memo.clear();

    string s;
    cin >> s;
    s.erase(s.end() - 1);

    pair<string, vector<string>> base = split(s);
    vector<pair<string, vector<string>>> other;
    for (int i = 1; i < n; ++i) {
      cin >> s;
      s.erase(s.end() - 1);
      other.push_back(split(s));
    }
    set<string> ans = solve(base, other);
    cout << ans.size() << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;

pair<string, vector<string>> split(string s) {
  map<string, vector<string>> ret;

  int start, end;
  end = s.find_first_of(":", 0);
  string key = s.substr(0, end);
  start = end + 1;

  vector<string> value;
  do {
    end = s.find_first_of(",", start);
    value.push_back(s.substr(start, end - start));
    start = end + 1;
  } while (end != string::npos);

  return pair<string, vector<string>>(key, value);
}

int main() {
  // freopen("AOJ1305.txt", "r", stdin);

  int n;
  while (cin >> n) {
    if (n == 0)
      break;

    string s;
    cin >> s;
    s.erase(s.end() - 1);
    pair<string, vector<string>> group = split(s);
    set<string> ans;

    for (int i = 0; i < group.second.size(); ++i)
      ans.insert(group.second[i]);

    map<string, vector<string>> other;
    for (int i = 1; i < n; ++i) {
      cin >> s;
      s.erase(s.end() - 1);
      other.insert(split(s));
    }

    for (;;) {
      bool op = false;

      for (map<string, vector<string>>::iterator it = other.begin();
           it != other.end(); ++it) {
        set<string>::iterator tar = ans.find(it->first);
        if (tar != ans.end()) {
          ans.erase(tar);
          for (int i = 0; i < it->second.size(); ++i)
            ans.insert(it->second[i]);

          op = true;
          break;
        }
      }

      if (!op)
        break;
    }

    cout << ans.size() << endl;
  }
  return 0;
}