/*
思路
思路一:
1. 用字典接收数据 键是儿子 值是祖先
2. 接收一个父亲名字判断是否存在键或值相同(与键相同则祖先相同,与值相同或者不存在则祖先是自己)
3. 接收一个#父亲之后 接收到+号 全以#父亲的值作为值(祖先)
*/
#include <bits/stdc++.h>
using namespace std;
void function_find()
{
//用字典接收数据 键是儿子 值是祖先
unordered_map<string,string> father;
//存储查找人的数组
vector<string> queries;
//接收名字判断存储
string line;
string temp;
while( getline(cin , line) ) { // 逐行读取
//判断第一字符# + $
if ( line == "$" )
break;
else if ( line[0] == '#' ) { // 接收一个父亲名字 父亲也可能是上一个的儿子
temp = line.substr(1);
if ( father.count(temp) == 0 ) // 键不同 表示没有此人 ==> 存储起来 祖先是自己
father[temp] = temp;
}
else if ( line[0] == '+' )
father[line.substr(1)] = father[temp]; // 存储上一个#父亲的值为值
else if ( line[0] == '?' )
queries.push_back(line.substr(1)); // 加入查询数组
}
//循环查找 输出 多了一个换行符
/*for ( string find_Name : queries ) {
cout << find_Name << " " << father[find_Name] << endl;
}*/
for ( size_t i = 0; i < queries.size(); i++ ) {
if ( i != queries.size() - 1 )
cout << queries[i] << " " << father[queries[i]] << endl;
else
cout << queries[i] << " " << father[queries[i]];
}
}
int main()
{
function_find();
return 0;
}
0 回复
0 转发
0 喜欢
25 阅读



