大佬们,请教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];
}