70分求助,#6#7#8WA
  • 板块P2814 家谱
  • 楼主yszkddzyh
  • 当前回复1
  • 已保存回复1
  • 发布时间2023/1/2 20:56
  • 上次更新2023/10/24 05:46:31
查看原帖
70分求助,#6#7#8WA
737158
yszkddzyh楼主2023/1/2 20:56

感觉问题好像出在初始化上了,但不知道具体怎么改

#include <iostream>
#include <map>
using namespace std;
string s,f;
char c;
map<string,string>fa;
map<string,int>vis; //如果fa[str]初始化过了,vis[str]=1 
string Find(string str){
	if(fa[str]==str) return str;
	return fa[str]=Find(fa[str]);
}
int main(){
	while(c!='$'){
		cin>>c>>s;
		if(vis[s]==0) fa[s]=s,vis[s]=1;
		if(c=='#') f=s;
		else if(c=='+') fa[s]=Find(f);
		else if(c=='?') cout<<s<<' '<<fa[s]<<'\n';
	}
	return 0;
}
2023/1/2 20:56
加载中...