大佬们,请教map<string, int>的初始化问题
  • 板块学术版
  • 楼主小菜鸟呵呵
  • 当前回复5
  • 已保存回复5
  • 发布时间2023/4/20 18:29
  • 上次更新2023/10/23 17:58:51
查看原帖
大佬们,请教map<string, int>的初始化问题
161468
小菜鸟呵呵楼主2023/4/20 18:29

大佬们,请教map<string, int>的初始化问题

题目为拓扑排序板题

题目

去掉注释后提交段错误

除此以外只有排序板子中使用

#include<bits/stdc++.h>
using namespace std;
int n;
unordered_map<string, int>d;
unordered_map<string, vector<string>> g;
 
vector<string> get(string str)
{
	vector<string> res;
	string word;
	for (auto c : str)
	{
		if (c == '.')
		{ 
			res.push_back(word);
			//d[word];
			word = "";
		}
		else word += c;
	}
	res.push_back(word);
	
	return res;
}
 
vector<string> topsort()
{
	priority_queue<string, vector<string>, greater<string>> heap;
	for (auto&[k,v] : d)
		if (!v) heap.push(k);
	vector<string> res;
	while (heap.size())
	{
		auto t = heap.top();
		res.push_back(t);
		heap.pop();
		for (auto& u : g[t])
			if (--d[u] == 0)
				heap.push(u);
	}
	return res;
 
 
}
 
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
	cin >> n;
    string str;
	cin >> str;
	auto last = get(str);
	for (int i = 0;i < n - 1;i++)
	{
		cin >> str;
		auto cur = get(str);
		if (last.size() == cur.size())
		{
			for (int j = 0;j < cur.size();j++)
				if (last[j] != cur[j])
				{
					g[last[j]].push_back(cur[j]);
					d[cur[j]]++;
					break;
				}
		}
		last = cur;
	}
 
	auto res = topsort();
 
	cout << res[0];
	for (int i = 1;i < res.size();i++)
		cout << '.' << res[i];
}
2023/4/20 18:29
加载中...