代码改变世界

leetcode - Word Ladder II

2013-12-23 21:32  张汉生  阅读(219)  评论(0编辑  收藏  举报

 

 1 class Solution {
 2 public:
 3     typedef unordered_map<string, unsigned> MyHashMap;
 4     typedef unordered_map<string, vector<string>> SVMap;
 5     vector<vector<string>> findLadders(string start, string end, unordered_set<string> &dict) {
 6         // Note: The Solution object is instantiated only once and is reused by each test case.
 7         unsigned rltLen = INT_MAX;
 8         vector<vector<string> > rlt;
 9         if (start == end)
10             rltLen = 2;
11         if (start.length() != end.length())
12             return rlt;
13         queue<string> qs;
14         int len = start.length();
15         qs.push(start);
16         MyHashMap depMap;
17         SVMap prevMap;
18         prevMap[start] = vector<string>();
19         depMap[start] = 1;
20         while (!qs.empty()){
21             string cur = qs.front();
22             qs.pop();
23             unsigned curLen = depMap[cur];
24             if (curLen >= rltLen)
25                 continue;
26             for (int i = 0; i < len; i++){
27                 for (int j = 0; j < 26; j++){
28                     string tmp = cur;
29                     tmp[i] = 'a' + j;
30                     if (tmp == cur)
31                         continue;
32                     if (tmp == end){
33                         rltLen = depMap[cur]+1;
34                         prevMap[end].push_back(cur);
35                         continue;
36                     }
37                     if (dict.find(tmp) == dict.end())
38                         continue;
39                     if (curLen+1 >= rltLen)
40                         continue;
41                     MyHashMap::iterator mhi = depMap.find(tmp);
42                     if (mhi!=depMap.end() && mhi->second <= curLen)
43                         continue;
44                     if (prevMap.find(tmp) == prevMap.end())
45                         prevMap[tmp] = vector<string>(1, cur);
46                     else prevMap[tmp].push_back(cur);
47                     if (mhi != depMap.end())
48                         continue;
49                     qs.push(tmp);
50                     depMap[tmp] = curLen+1;
51                 }
52             }
53         } // end while
54         if (rltLen == INT_MAX)
55             return rlt;
56         stack<vector<string> > svs;
57         svs.push(vector<string>(1, end));
58         while (!svs.empty()){
59             vector<string> vs = svs.top();
60             svs.pop();
61             string last = *(vs.end() - 1);
62             if (last == start){
63                 rlt.push_back(vs);
64                 continue;
65             }
66             for (auto iter = prevMap[last].begin(); iter != prevMap[last].end(); iter++){
67                 vector<string> tmpVect = vs;
68                 tmpVect.push_back(*iter);
69                 svs.push(tmpVect);
70             }
71         } 
72         for (auto iter = rlt.begin(); iter != rlt.end(); iter++){
73             int size = iter->size();
74             int j = size / 2;
75             for (int i = 0; i < j; i++){
76                 string tmp = (*iter)[i]; 
77                 (*iter)[i] = (*iter)[size - i - 1];
78                 (*iter)[size - i - 1] = tmp;
79             }
80         }
81         return rlt;
82     }
83 };