The best approach would be to store your entire input data in a tree structure. If you are familiar with tree-structures, this will probably the most obvious solution.
As you have not chosen that path, I assume that you are not familiar with building a tree in C++. So here is an easier way to solve your problem almost as efficiently, just based on the two vectors that you already have:
1. Create a third vector, a std::vector<int> and name it level. The idea is to store the employee's level in the company hierarchy in that vector. The boss is assigned level 0, his immediate subordinates level 1, and so forth.
2. Initially fill the level vector with the value of -1 as indicator that no level has been assigned yet.
3. Loop over all employees and try to evaluate their level. This is easiest done in a recursive function, lets call it EvaluateLevel, which operates like this:
3a: if the manager of that employee is null (none), assign level 0.
3b: else search the name vector for the managers name and look up his level. If it is not assigned yet (i.e. -1) recursivly call the EvaluateLevel function on the manager's index. Add one to the manager's level and enter it into the employee's level field.
4. Finally loop over the level vector and find the highest value (or you do that as a side effect of the EvaluateLevel function by just remembering the highest level ever assigned).
I haven't spelled that out in C++ code, as I want to leave that as an exercise to you. Just let me know if you have any particular problem with the implementation.
[ADDED]
The recursive function would be something like this:
using namespace std;
int EvaluateLevel (int idx,
const vector<string>& names,
const vector<string>& managers,
vector<int>& levels)
{
if (managers[idx].empty())
{
levels[idx] = 0;
return 0;
}
if (levels[idx] != -1)
return levels[idx];
string manager = managers[idx];
int managersIdx = -1;
for (int i = 0; i < names.size(); ++i)
if (names[i] == manager)
{
managersIdx = i;
break;
}
if (managersIdx = -1)
return -1; int managersLevel = EvaluateLevel (managersIdx, names, managers, levels);
if (managersLevel < 0)
return -1; levels[idx] = managersLevel + 1;
return levels[idx];
}
In the next step, try to replace the three parallel vectors by a single vector of a class that describes a person, i.e.
class Person
{
public:
string name;
string manager;
int level;
};
vector <Person>