word ladder

来源:互联网 发布:java富有创意的小程序 编辑:程序博客网 时间:2024/05/24 03:30

链接:http://leetcode.com/onlinejudge#question_126

原题:

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>存储好,有这些记录就可以重构路径了。

代码:

[cpp] view plain copy
  1. class Solution {  
  2. public:  
  3.     vector<vector<string> > findLadders(string start, string end, unordered_set<string> &dict) {  
  4.         // Start typing your C/C++ solution below  
  5.         // DO NOT write int main() function  
  6.         pathes.clear();  
  7.         dict.insert(start);  
  8.         dict.insert(end);  
  9.         vector<string> prev;  
  10.         unordered_map<string, vector<string> > traces;  
  11.         for (unordered_set<string>::const_iterator citr = dict.begin();   
  12.                 citr != dict.end(); citr++) {  
  13.             traces[*citr] = prev;  
  14.         }  
  15.           
  16.         vector<unordered_set<string> > layers(2);  
  17.         int cur = 0;  
  18.         int pre = 1;  
  19.         layers[cur].insert(start);  
  20.         while (true) {  
  21.             cur = !cur;  
  22.             pre = !pre;  
  23.             for (unordered_set<string>::const_iterator citr = layers[pre].begin();  
  24.                     citr != layers[pre].end(); citr++)  
  25.                 dict.erase(*citr);  
  26.             layers[cur].clear();  
  27.             for (unordered_set<string>::const_iterator citr = layers[pre].begin();  
  28.                     citr != layers[pre].end(); citr++) {  
  29.                 for (int n=0; n<(*citr).size(); n++) {    
  30.                     string word = *citr;    
  31.                     int stop = word[n] - 'a';    
  32.                     for (int i=(stop+1)%26; i!=stop; i=(i+1)%26) {    
  33.                         word[n] = 'a' + i;    
  34.                         if (dict.find(word) != dict.end()) {    
  35.                             traces[word].push_back(*citr);  
  36.                             layers[cur].insert(word);   
  37.                         }    
  38.                     }  
  39.                 }  
  40.             }  
  41.             if (layers[cur].size() == 0)  
  42.                 return pathes;  
  43.             if (layers[cur].count(end))  
  44.                 break;  
  45.         }  
  46.         vector<string> path;  
  47.         buildPath(traces, path, end);  
  48.   
  49.         return pathes;  
  50.     }  
  51.   
  52.     private:  
  53.         void buildPath(unordered_map<string, vector<string> > &traces,   
  54.                 vector<string> &path, const string &word) {  
  55.             if (traces[word].size() == 0) {  
  56.                 path.push_back(word);  
  57.                 vector<string> curPath = path;  
  58.                 reverse(curPath.begin(), curPath.end());  
  59.                 pathes.push_back(curPath);  
  60.                 path.pop_back();  
  61.                 return;  
  62.             }  
  63.   
  64.             const vector<string> &prevs = traces[word];  
  65.             path.push_back(word);  
  66.             for (vector<string>::const_iterator citr = prevs.begin();  
  67.                     citr != prevs.end(); citr++) {  
  68.                 buildPath(traces, path, *citr);  
  69.             }  
  70.             path.pop_back();  
  71.         }  
  72.   
  73.         vector<vector<string> > pathes;  
  74. };  
0 0
原创粉丝点击