[leetcode] Word Ladder II

Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that:

  1. Only one letter can be changed at a time
  2. Each intermediate word must exist in the dictionary

For example,

Given:
start = "hit"
end = "cog"
dict = ["hot","dot","dog","lot","log"]

Return

  [
    ["hit","hot","dot","dog","cog"],
    ["hit","hot","lot","log","cog"]
  ]

Note:

  • All words have the same length.

  • All words contain only lowercase alphabetic characters.
思路:

其实解题思路和Word Ladder完全一样,BFS,但是麻烦的是要返回所有的路径。

所以没办法,只能把每个单词所对应的前驱单词记录下来,当然有可能有多个,那么

就用一个vector<string>存储好,有这些记录就可以重构路径了。

参考链接:http://blog.csdn.net/niaokedaoren/article/details/8884938

代码:

class Solution {
public:
    vector<vector<string> > findLadders(string start, string end, unordered_set<string> &dict) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        pathes.clear();
        dict.insert(start);
		dict.insert(end);
		vector<string> prev;
		unordered_map<string, vector<string> > traces;
		for (unordered_set<string>::const_iterator citr = dict.begin(); 
				citr != dict.end(); citr++) {
			traces[*citr] = prev;
		}
		
		vector<unordered_set<string> > layers(2);
		int cur = 0;
		int pre = 1;
		layers[cur].insert(start);
		while (true) {
			cur = !cur;
			pre = !pre;
			for (unordered_set<string>::const_iterator citr = layers[pre].begin();
					citr != layers[pre].end(); citr++)
				dict.erase(*citr);
			layers[cur].clear();
			for (unordered_set<string>::const_iterator citr = layers[pre].begin();
					citr != layers[pre].end(); citr++) {
				for (int n=0; n<(*citr).size(); n++) {  
                    string word = *citr;  
                    int stop = word[n] - 'a';  
                    for (int i=(stop+1)%26; i!=stop; i=(i+1)%26) {  
                        word[n] = 'a' + i;  
                        if (dict.find(word) != dict.end()) {  
                            traces[word].push_back(*citr);
    				        layers[cur].insert(word); 
                        }  
                    }
                }
			}
            if (layers[cur].size() == 0)
                return pathes;
			if (layers[cur].count(end))
				break;
		}
		vector<string> path;
		buildPath(traces, path, end);

		return pathes;
	}

	private:
		void buildPath(unordered_map<string, vector<string> > &traces, 
				vector<string> &path, const string &word) {
			if (traces[word].size() == 0) {
                path.push_back(word);
				vector<string> curPath = path;
				reverse(curPath.begin(), curPath.end());
				pathes.push_back(curPath);
                path.pop_back();
				return;
			}

			const vector<string> &prevs = traces[word];
			path.push_back(word);
			for (vector<string>::const_iterator citr = prevs.begin();
					citr != prevs.end(); citr++) {
				buildPath(traces, path, *citr);
			}
			path.pop_back();
		}

		vector<vector<string> > pathes;
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值