无法输出祖先
  • 板块P2814 家谱
  • 楼主Tsuki091117
  • 当前回复2
  • 已保存回复2
  • 发布时间2023/11/24 19:06
  • 上次更新2023/11/24 20:57:35
查看原帖
无法输出祖先
1069816
Tsuki091117楼主2023/11/24 19:06
#include<bits/stdc++.h>
using namespace std;
map<string,string> ma; 
char c;
string s,p;
string find(string x){
	return ma[x]==x?x:ma[x]=find(ma[x]);
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin>>c;
	while(c!='$'){
		cin>>s;
		if(c=='#'){
			p=s;
			if(ma[s]==" "){
				ma[s]=s; 				
			}
		}
		else if(c=='+'){
			ma[s]=p;
		}
		if(c=='?'){
			cout<<s<<" "<<find(s)<<"\n";
		}
		cin>>c;
	}
	return 0;
}
2023/11/24 19:06
加载中...