栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

7-27 家谱处理 (30 分)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

7-27 家谱处理 (30 分)

题目链接

#include
#include
#include
using namespace std;
unordered_map> child,ancestor,sibling,parent,descendant;
vector prio(110);
int setPrio(string name){
	int len = 0;
	for(len = 0; len < name.length(); len++){
		if(name[len] != ' ') break;
	}
	prio[len/2] = name.substr(len);
	return len/2;
}
void setGen(string name,int num){
	if(num == 0) return;
	//兄弟 
	for(auto sb:child[prio[num-1]]){
		sibling[name].push_back(sb);
		sibling[sb].push_back(name);
	}
	//子女 
	child[prio[num-1]].push_back(name);
	//父母 
	parent[name].push_back(prio[num-1]); 
	//祖先
	for(int i = num - 1; i >= 0; i--){
		ancestor[name].push_back(prio[i]); //祖先 
		descendant[prio[i]].push_back(name); //后代 
	}
} 
bool queryRela(const string &name1,const string &name2,const string &rela){
	if(rela == "child"){
		for(auto tmp:child[name2]){
			if(tmp == name1) return true;
		}
	} else if(rela == "parent"){
		for(auto tmp:parent[name2]){
			if(tmp == name1) return true;
		}
	}  else if(rela == "sibling"){
		for(auto tmp:sibling[name2]){
			if(tmp == name1) return true;
		}
	}  else if(rela == "descendant"){
		for(auto tmp:descendant[name2]){
			if(tmp == name1) return true;
		}		
	}  else if(rela == "ancestor"){
		for(auto tmp:ancestor[name2]){
			if(tmp == name1) return true;
		}
	}
	
	return false;
}
int main(){
	int n,m;
	cin>>n>>m;
	getchar();
	for(int i = 0; i < n; i++){
		string tmp;
		getline(cin,tmp);
		int num = setPrio(tmp);//设置等级
		tmp = tmp.substr(num*2);
		setGen(tmp,num);//放入族谱 
	}
	while(m--){
		string name1,name2,rela,t;
		cin>>name1>>t>>t>>rela>>t>>name2;
		if(queryRela(name1,name2,rela)) cout<<"True";
		else cout<<"False";
		if(m) cout<<'n'; 
	} 
	return 0;
} 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/290499.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号