后两个点MLE求调
  • 板块P2814 家谱
  • 楼主I_Love_Codm
  • 当前回复0
  • 已保存回复0
  • 发布时间2025/6/14 11:30
  • 上次更新2025/6/14 21:57:47
查看原帖
后两个点MLE求调
1436267
I_Love_Codm楼主2025/6/14 11:30
#include <cmath>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

const int mod = 99997;

struct edge {
	string name, fa;
	edge() : name(""), fa("") {}
};

string str, fa;
vector<string> ship;
vector<edge> node(mod + 10);

int get_hash(string &s) {
	int hash = 0, w = 1;
	for (char &c: s) {
		hash += (tolower(c) - 'a') * w;
		w *= 26;
	}

	return hash % mod;
}

void solve(string &son) {
	int hash_son = get_hash(son);
	node[hash_son].fa = fa;
	node[hash_son].name = son;
}

string found(string &s) {
	int hash = get_hash(s);
	if (node[hash].fa == node[hash].name) return s;
	return node[hash].fa = found(node[hash].fa);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	while (cin >> str) {
		if (str == "$") break;
		char op = str[0];
		str = str.substr(1);
		if (op == '#') {
			int hash = get_hash(str);
			fa = str;
			node[hash].name = str;
			if (node[hash].fa == "") node[hash].fa = str;
		} else if (op == '+') solve(str);
		else cout << str << ' ' << found(str) << '\n';
	}

	return 0;
}
2025/6/14 11:30
加载中...