• [Locked] Alien Dictionary


    Alien Dictionary

    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, where words 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: You may assume all letters are in lowercase. If the order is invalid, return an empty string. There may be multiple valid order of letters, return any one of them is fine.

    分析:

      前后关系可看做是一种偏序关系,对于每个序列,可以看成是有向图,所有序列合在一起就成了一个大的有向图,本题可转换成有向图的拓扑排序,用BFS可解决。主要过程包含:1、根据string数组的信息映射成图;2、在图上计算出所有节点的入度;3、进行BFS,得到拓扑排序结果。故我用三个子函数来分别处理这三个过程。

    代码:

    //将string数组映射到图上,最坏时间复杂度就是每个字母遍历一遍,这也是必要,故也是最低时间复杂度
    unordered_multimap<char, char> mapping(vector<string> &vs) {
        //先处理第一列,用validcomp来记录哪些相邻行的值相等,用于后面的列的处理,不相等的就可以得偏序条件,用边来表示,存入edges中;然后处理第二列,以此类推。
        unordered_multimap<char, char> edges;
        unordered_set<int> validcomp, temp;
        for(int i = 1; i < vs.size(); i++)
            validcomp.insert(i);
        int j = 0;
        while(!validcomp.empty()) {
            for(int pos : validcomp) {
                if(vs[pos][j] != vs[pos - 1][j])
                    edges.insert(make_pair(vs[pos - 1][j], vs[pos][j]));
                else
                    temp.insert(pos);
            }
            unordered_set<int> ().swap(validcomp);
            validcomp.swap(temp);
            j++;
        }
        return edges;
    }
    
    //返回所有节点的入度,存入degree里
    unordered_map<char, int> getDegree(unordered_multimap<char, char> &edges) {
        unordered_map<char, int> degree;
        for(char c = 'a'; c <= 'z'; c++) {
            auto range = edges.equal_range(c);
            auto pos = range.first;
            while(pos != range.second) {
                //指出节点未出现过,则设0,否则跳过;指入节点未出现过,则设1,否则++。
                if(degree.find(pos->first) == degree.end())
                    degree[pos->first] = 0;
                if(degree.find(pos->second) != degree.end())
                    degree[pos->second]++;
                else
                    degree[pos->second] = 1;
                pos++;
            }
        }
        return degree;
    }
    
    //进行BFS,用最朴素的方法,复杂度为O(N^2),用这个方法找到符合题目要求的顺序,如果能遍历完全所有节点,则return这个顺序;否则,return ""
    bool bfs(string &str, unordered_map<char, int> degree, unordered_multimap<char, char> &edges) {
        bool visited[26];
        memset(visited, 0, 26);
        for(int i = 0; i < degree.size(); i++) {
            for(auto m : degree)
                //入度为0则排入,然后更新入度hash表
                if(!visited[int(m.first - 'a')] && m.second == 0) {
                    visited[int(m.first - 'a')] = true;
                    str += m.first;
                    auto range = edges.equal_range(m.first);
                    auto pos = range.first;
                    while(pos != range.second) {
                        degree[pos->second]--;
                        pos++;
                    }
                    break;
                }
        }
        //有环,则return ""
        for(auto m : degree)
            if(m.second != 0)
                return false;
        return true;
    }
    //主函数
    string validOrder(vector<string> vs) {
        //string数组映射到图edges上
        unordered_multimap<char, char> edges = mapping(vs);
    //返回所有节点的入度,存入degree;如果无效,则return ""; unordered_map<char, int> degree = getDegree(edges); //进行BFS,找到合适的顺序,优先排入入度为0的点 string str = ""; bool valid = bfs(str, degree, edges); return valid ? str : ""; }

    其中,BFS过程,即函数bfs复杂度为O(N^2),N为节点个数,有改进的空间;可以优化为O(E),E为边的个数

    //优化BFS
    bool bfs(string &str, unordered_map<char, int> degree, unordered_multimap<char, char> &edges) {
        queue<char> myq, temp;
        for(auto m : degree)
            if(m.second == 0)
                myq.push(m.first);
        while(!myq.empty()) {
            while(!myq.empty()) {
                char c = myq.front();
                str += c;
                myq.pop();
                auto range = edges.equal_range(c);
                auto pos = range.first;
                while(pos != range.second) {
                    degree[pos->second]--;
                    if(degree[pos->second] == 0)
                        temp.push(pos->second);
                    pos++;
                }
            }
            queue<char> ().swap(myq);
            myq.swap(temp);
        }
        //有环,则return ""
        for(auto m : degree)
            if(m.second != 0)
                return false;
        return true;
    }
  • 相关阅读:
    生成树
    如何自定义百度网盘分享密码
    斯特林数入门
    各种反演难题训练集合
    LOJ[6247]九个太阳
    min25筛入门
    2020 Petrozavodsk Winter Camp, Jagiellonian U Contest-A-Bags of Candies
    ICPC WF 2018 C Conquer the World 征服世界
    dsu on tree
    生成函数入门级内容
  • 原文地址:https://www.cnblogs.com/littletail/p/5222463.html
Copyright © 2020-2023  润新知