0分求助
  • 板块P2814 家谱
  • 楼主TheAurora
  • 当前回复2
  • 已保存回复2
  • 发布时间2022/9/29 23:16
  • 上次更新2023/10/27 09:29:51
查看原帖
0分求助
93374
TheAurora楼主2022/9/29 23:16

代码如下,样例过了,提交上去全WA

#pragma warning(disable:4703)
#include<iostream>
#define N 50005
using namespace std; 

struct node {
	string name; 
	node* f; 
};

node* m[N]; 
int l = 0; 

string input() {
	char c; 
	string res = "";
	c = getchar(); 
	while (c != '\n') {
		res += c; 
		c = getchar(); 
	}
	return res; 
}

node* find(string x) {
	for (int i = 1; i <= l; ++i) {
		if (m[i]->name == x) {
			return m[i]; 
		}
	}
	m[++l] = new node(); 
	m[l]->name = x; 
	return m[l];
}

int main()
{
	char c; 
	string s; 
	c = getchar(); 
	node* f; 
	while (c != '$') {
		s = input(); 
		node* res = find(s);
		if (c== '#') {
			f = res;
		}
		if (c == '+') {
			res->f = f;
		}
		if (c == '?') {
			cout << res->name << ' '; 
			while (res->f != nullptr) {
				res = res->f; 
			}
			cout << res->name << endl; 
		}
		c = getchar(); 
		while (c=='\n')
			c = getchar();
	}
	return 0; 
}
2022/9/29 23:16
加载中...