码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] Alien Dictionary

时间:2015-08-25 23:16:23      阅读:326      评论:0      收藏:0      [点我收藏+]

标签:

Well, this problem is not that easy. First you may need some clarifications about the problem itself. If you do, you may refer to this link for a nice example which illustrates the purpose of this problem.

Moreover, you need to understand graph representation, graph traversal (mainly DFS) and specifically, topological sort, which is all needed to solve this problem cleanly. Fortunately, jaewoo posts a nice solution in this link, whose code is rewritten as follows by decomposing the code into two parts:

  1. make_graph: Build the graph as an adjacency list;
  2. toposort and acyclic: Traverse the graph in DFS manner to check for cycle and generate the topological sort.
 1 class Solution {
 2 public:
 3     string alienOrder(vector<string>& words) {
 4         if (words.size() == 1) return words[0];
 5         graph g = make_graph(words);
 6         return toposort(g);
 7     }
 8 private:
 9     typedef unordered_map<char, unordered_set<char>> graph;
10     
11     graph make_graph(vector<string>& words) {
12         graph g;
13         int n = words.size();
14         for (int i = 1; i < n; i++) {
15             bool found = false;
16             string word1 = words[i - 1], word2 = words[i];
17             int m = word1.length(), n = word2.length(), l = max(m, n);
18             for (int j = 0; j < l; j++) {
19                 if (j < m && g.find(word1[j]) == g.end())
20                     g[word1[j]] = unordered_set<char>();
21                 if (j < n && g.find(word2[j]) == g.end())
22                     g[word2[j]] = unordered_set<char>();
23                 if (j < m && j < n && word1[j] != word2[j] && !found) {
24                     g[word1[j]].insert(word2[j]);
25                     found = true;
26                 }
27             }
28         }
29         return g;
30     }
31     
32     string toposort(graph& g) {
33         unordered_set<char> path, visited;
34         string topo;
35         for (auto adj : g)
36             if (!acyclic(g, path, visited, topo, adj.first))
37                 return "";
38         reverse(topo.begin(), topo.end());
39         return topo;
40     }
41     
42     bool acyclic(graph& g, unordered_set<char>& path, unordered_set<char>& visited, string& topo, char node) {
43         if (path.find(node) != path.end()) return false;
44         if (visited.find(node) != visited.end()) return true;
45         path.insert(node); visited.insert(node);
46         for (auto neigh : g[node])
47             if (!acyclic(g, path, visited, topo, neigh))
48                 return false;
49         path.erase(node);
50         topo += node;
51         return true;
52     }
53 };

Well, this problem is not easy and the code is also long. If you have difficulties understanding it,  make sure to review the fundamentals of graph (Introduction to Algorithms is a good reference) and try to solve older LeetCode problems like Clone Graph, Course Schedule and Course Schedule II first.

[LeetCode] Alien Dictionary

标签:

原文地址:http://www.cnblogs.com/jcliBlogger/p/4758761.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!