qingcheng奕  

https://oj.leetcode.com/problems/word-ladder-ii/

啊,终于过了

class Solution {
public:
    vector<vector<string> > findLadders(string start, string end, unordered_set<string> &dict) {
        vector<vector<string> > ans;

        if(start.size() == 0 || end.size() ==0 || dict.size() == 0)
            return ans;
        
        if(start == end)
        {
            vector<string> piece;
            piece.push_back(start);
            ans.push_back(piece);
            return ans;
        }
        
        unordered_map<string, vector<string> > parents;
        
        unordered_set<string> current;
        current.insert(start);
        dict.erase(start);

        unordered_set<string> next;
        bool flagFind = false;
        vector<string> lastOnes; // 记录最后一个变换的string
        int depth = 1; // 记录深度

        while(current.size() != 0 && flagFind == false)  // flagFind 标记是否已经找到
        {
            depth++;
            // 对于本层的每一个单词
            unordered_set<string>::iterator itr;
            for(itr = current.begin(); itr != current.end(); itr++)
            {
                // 对于本单词的每一个位置
                for(int index = 0; index < start.size(); index++)
                {
                    // 替换成 a~z,并且不等于原单词,并且在dict中存在
                    for(char ch = 'a'; ch <= 'z'; ch++)
                    {
                        string newStr = *itr; 
                        if(newStr[index] != ch)  // 换了以后不是原来的那个
                        {
                            newStr[index] = ch;
                            if(newStr == end)
                            {
                                lastOnes.push_back( *itr);
                                flagFind = true;
                            }
                        }
                        else
                            continue;

                        // 如果变换后在 dict 里面
                        if(dict.find(newStr) != dict.end())
                        {
                            next.insert(newStr);
                            // record parents
                            parents[newStr].push_back( *itr);
                        }
                    }
                }
            }
            // remove all next strings from dict
            for(itr = next.begin(); itr != next.end(); itr++)
                dict.erase(*itr);
                    
            current = next;
            next.clear();
        }
        if(flagFind == true)
        {
            vector<string> ansPiece;
            ansPiece.push_back(end);
            buildPath(ans,lastOnes,ansPiece,parents,depth);
        }
        return ans;
    }
    void buildPath(vector<vector<string> > &ans,vector<string> &lastOnes, vector<string> &ansPiece, unordered_map<string,vector<string> > &parents,int depth)
    {
        depth--;
        for(int i = 0; i < lastOnes.size(); i++)
        {
            ansPiece.push_back(lastOnes[i]);
            if(depth == 1)
            {
                vector<string> temp = ansPiece;
                reverse(temp.begin(),temp.end());
                ans.push_back(temp);
            }
            else
            {
                buildPath(ans,parents[lastOnes[i]],ansPiece,parents,depth);
            }
            ansPiece.pop_back();
        }
    }
};

 

posted on 2014-08-19 10:32  qingcheng奕  阅读(162)  评论(0编辑  收藏  举报