zoukankan      html  css  js  c++  java
  • [LeetCode] Alien Dictionary

    Problem Description:

    There is a new alien language which uses the latin alphabet. However, the order among letters are unknown to you. You receive a list of words from the dictionary, wherewords are sorted lexicographically by the rules of this new language. Derive the order of letters in this language.

    For example,
    Given the following words in dictionary,

    [
      "wrt",
      "wrf",
      "er",
      "ett",
      "rftt"
    ]

    The correct order is: "wertf".

    Note:

      1. You may assume all letters are in lowercase.
      2. If the order is invalid, return an empty string.
      3. There may be multiple valid order of letters, return any one of them is fine.

    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 post for a nice example which illustrates the purpose of this problem.

    Moreover, you need to understand graph representation, graph traversal and specifically, topological sort, which are all needed to solve this problem cleanly.


    DFS

    Fortunately, jaewoo posts a nice solution in this post, whose code is rewritten as follows by decomposing the code into two parts:

    1. make_graph: Build the graph as a list of adjacency lists;
    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         vector<bool> path(256, false), visited(256, false);
    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, vector<bool>& path, vector<bool>& visited, string& topo, char node) {
    43         if (path[node]) return false;
    44         if (visited[node]) return true;
    45         path[node] = visited[node] = true;
    46         for (auto neigh : g[node])
    47             if (!acyclic(g, path, visited, topo, neigh))
    48                 return false;
    49         path[node] = false;
    50         topo += node;
    51         return true;
    52     }
    53 };

    BFS

    Well, given the graph well represented, the BFS solution is also not that hard :-)

     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         unordered_map<char, int> degrees = compute_indegree(g);
     7         int numNodes = degrees.size();
     8         string order;
     9         queue<char> toVisit;
    10         for (auto node : degrees)
    11             if (!node.second)
    12                 toVisit.push(node.first);
    13         for (int i = 0; i < numNodes; i++) {
    14             if (toVisit.empty()) return "";
    15             char c = toVisit.front();
    16             toVisit.pop();
    17             order += c;
    18             for (char neigh : g[c])
    19                 if (!--degrees[neigh])
    20                     toVisit.push(neigh);
    21         }
    22         return order;
    23     }
    24 private:
    25     typedef unordered_map<char, unordered_set<char>> graph;
    26 
    27     graph make_graph(vector<string>& words) {
    28         graph g;
    29         int n = words.size();
    30         for (int i = 1; i < n; i++) {
    31             bool found = false;
    32             string word1 = words[i - 1], word2 = words[i];
    33             int l1 = word1.length(), l2 = word2.length(), l = max(l1, l2);
    34             for (int j = 0; j < l; j++) {
    35                 if (j < l1 && g.find(word1[j]) == g.end())
    36                     g[word1[j]] = unordered_set<char>();
    37                 if (j < l2 && g.find(word2[j]) == g.end())
    38                     g[word2[j]] = unordered_set<char>();
    39                 if (j < l1 && j < l2 && word1[j] != word2[j] && !found) {
    40                     g[word1[j]].insert(word2[j]);
    41                     found = true;
    42                 }
    43             }
    44         }
    45         return g; 
    46     }
    47 
    48     unordered_map<char, int> compute_indegree(graph& g) {
    49         unordered_map<char, int> degrees;
    50         for (auto adj : g) {
    51             if (!degrees[adj.first]);
    52             for (char neigh : adj.second)
    53                 degrees[neigh]++;
    54         }
    55         return degrees;
    56     }
    57 };

    BTW, if (!degrees[adj.first]); in compute_indegree is to make sure that a node with 0indegree will not be left out. For this part, something about the default constructor ofunordered_map is useful: each time when we try to access a key k which is still not inunordered_map by [k], the default constructor of unordered_map will set its value to 0.


    Well, this problem is not easy and the code is also long. If you have difficulties understanding it,  you may 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.

  • 相关阅读:
    .net操作cookies
    sqlserver 自增列清零
    数据库之mysql视图、触发器、事务、存储过程、函数等相关内容-47
    知识补充之面向对象魔法方法及mysql等相关内容
    数据库之mysql多表查询(子查询)以及pymysql等相关内容-46
    数据库之mysql多表查询(连表)以及pymysql等相关内容-45
    数据库之mysql约束条件、表关系、记录操作等相关内容-44
    数据库之mysq修改表、表字段类型等相关内容-43
    数据库之mysql基础等相关内容-42
    并发编程之基于协程的高并发服务端、面向对象回顾等相关内容-41
  • 原文地址:https://www.cnblogs.com/jcliBlogger/p/4758761.html
Copyright © 2011-2022 走看看