北理工11计算机复试上机——领导层次关系

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

struct node{
    
    
	string name;
	string up;//直属领导 
	node(string n, string u){
    
    
		name = n;
		up = u;
	}
};

vector<node> v;

void init(string str){
    
    
	string temp = "root";
	string up = "root";
	int i, j;
	for(i = 0; i < str.size(); ++i){
    
    
		if('a' <= str[i] && str[i] <= 'z'){
    
    //截取名称 
			temp = "";
			while('a' <= str[i] && str[i] <= 'z'){
    
    
				temp += str[i];
				++i;
			}
			--i;
			v.push_back(node(temp, up));
		}else if(str[i] == '('){
    
    //进入下一层,最后一个截取到的名称为下一层的直属领导 
			up = temp;
		}else if(str[i] == ')'){
    
    //寻找上一层的up 
			for(j = 0; j < v.size(); ++j){
    
    
				if(v[j].name == up){
    
    
					up = v[j].up;
					break;
				}
			}
		}
	}
}

string findUp(string name){
    
    
	int i;
	for(i = 0; i < v.size(); ++i){
    
    
		if(v[i].name == name){
    
    
			return v[i].up;
		}
	}
}

int main(int argc, char *argv[]) {
    
    
	string str, name;
	int i;
	cin>>str;
	init(str);
	while(cin>>name){
    
    
		vector<string> result;
		result.push_back(name);
		string up = findUp(name); 
		result.push_back(up);
		while(up != "root"){
    
    //循环向上寻找领导 
			up = findUp(up);
			if(up != "root"){
    
    
				result.push_back(up);
			}			
		} 
		for(i = result.size() - 1; i >= 1; --i){
    
    
			cout<<result[i]<<">";
		}
		cout<<result[0]<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/baidu_36004106/article/details/104861216