problemscpp
A collection of my answers to algorithm problems in c++.
静态 Public 成员函数 | 所有成员列表
leetcode::alien_dictionary::Solution类 参考

#include <leetcode.h>

静态 Public 成员函数

static string alienOrder (vector< string > &words)
 

详细描述

在文件 leetcode.h3213 行定义.

成员函数说明

◆ alienOrder()

string leetcode::alien_dictionary::Solution::alienOrder ( vector< string > &  words)
static

在文件 leetcode.cpp9154 行定义.

9154 {
9155 unordered_map<char, unordered_set<char>> g;
9156 unordered_map<char, int> in;
9157 ostringstream oss;
9158 unordered_set<char> charset;
9159 for(const auto &word: words) {
9160 for(const auto &ch: word) {
9161 charset.insert(ch);
9162 }
9163 }
9164 for(const auto &ch: charset) {
9165 g[ch] = unordered_set<char>();
9166 in[ch] = 0;
9167 }
9168 int max_len = 0;
9169 for(const auto &word: words) {
9170 max_len = max(max_len, static_cast<int>(word.length()));
9171 }
9172 for(int i = 0; i < max_len; i++) {
9173 unordered_map<string, vector<string>> um;
9174 for(const auto &word: words) {
9175 if(word.length() >= i) {
9176 um[i > 0 ? word.substr(0, i) : ""].emplace_back(word);
9177 }
9178 }
9179 for(const auto &[pred, section]: um) {
9180 for(int j = 0, k = 1; k < section.size(); j++, k++) {
9181 if(i < section[j].length() && i < section[k].length()) {
9182 if(section[j][i] != section[k][i]) {
9183 g[section[j][i]].insert(section[k][i]);
9184 }
9185 } else if(i < section[j].length()) {
9186 return "";
9187 }
9188 }
9189 }
9190 }
9191 for(const auto &[k, section]: g) {
9192 for(const auto &n: section) {
9193 in[n]++;
9194 }
9195 }
9196 bool flag = true;
9197 unordered_set<char> vis;
9198 while(flag) {
9199 flag = false;
9200 for(const auto &[ch, n]: in) {
9201 if(n == 0 && !vis.contains(ch)) {
9202 flag = true;
9203 vis.insert(ch);
9204 oss << ch;
9205 for(const auto &nc: g[ch]) {
9206 in[nc]--;
9207 }
9208 }
9209 }
9210 }
9211 for(const auto &[ch, n]: in) {
9212 if(n != 0) {
9213 return "";
9214 }
9215 }
9216 return oss.str();
9217 }

被这些函数引用 leetcode::alien_dictionary::TEST().


该类的文档由以下文件生成: