Given two words (start and end), and a dictionary, find all shortest transformation sequence (s) from start to end, such that:
- Only one letter can be changed at a time
- 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"] ]
This question is 5 stars difficult. BFS is used like I, but the time is not enough because it needs to be output.
Traverse to find the precursor words of each word, and use two queues to save the words of the current layer and the previous layer. One is being processed, and the other is to be processed next time.
Recursively print the path from the end after obtaining the precursor vocabulary
AC code
Class solution {public: vector <string> res; vector <string> findladders (string start, string end, unordered_set <string> & dict) {dict. insert (start); dict. insert (end); unordered_map <string, vector <string> precursor; // Save the precursor word for (unordered_set <string>: const_iterator citer = dict. begin (); citer! = Dict. end (); ++ citer) // initialize precursor. insert (make_pair (* citer, vector <string> (); vector <unordered_set <string> layers (2 ); // stores the current layer of words and the previous layer of words layers [0]. insert (start); int cur = 0; int pre = 1; while (true) {cur =! Cur; // two-layer interchange pre =! Pre; For (unordered_set <string>: const_iterator citer = Layers [pre]. Begin (); citer! = Layers [pre]. end (); ++ citer) dict. erase (* citer); // Delete the words layers [cur] that appear in the dictionary in the previous layer. clear (); // BFS searches for the words that can be achieved through a transformation in the previous layer. If the word is in the dictionary, it is placed in the current layer for (unordered_set <string> :: const_iterator citer = Layers [pre]. begin (); citer! = Layers [pre]. end (); ++ citer) {string STR = * citer; For (INT I = 0; I <Str. size (); ++ I) {for (Int J = 'a'; j <= 'Z'; ++ J) {If (STR [I] = J) continue; string TMP = STR; TMP [I] = J; If (dict. count (TMP) {precursor [TMP]. push_back (STR); layers [cur]. insert (TMP) ;}}} if (layers [cur]. empty () // if the current layer is empty, return res cannot be ended from start to end; If (layers [cur]. count (end) // if the current layer shows end, the conversion has been found, and because it is based on the number of layers, it is all short Est break;} vector <string> path; generatepath (precursor, path, end); Return res;} // recursive void generatepath (unordered_map <string, vector <string >>& precursor, vector <string> & Path, string end) {If (precursor [end]. size () = 0) {// NO precursor description has arrived at start path. push_back (end); vector <string> TMP = path; reverse (TMP. begin (), TMP. end (); // reverse the order res. push_back (TMP); Path. pop_back ();} path. push_ba CK (end); For (vector <string >:: const_iterator citer = precursor [end]. Begin (); citer! = Precursor [end]. End (); ++ citer) {generatepath (precursor, path, * citer) ;}path. pop_back ();}};
Tle is a mess, but it still cannot be recursive.
class Solution {public: vector<vector<string>> findLadders(string start, string end, unordered_set<string> &dict) { vector< vector< string> > res; vector< string> cur; cur.push_back(start); dict.insert(end); unordered_set< string> tmp = dict; int length = ladderLength(start, end, tmp); core( res, cur, start, end, dict, length); return res; } void core( vector< vector< string> > &res, vector< string> &cur, string start, string end, unordered_set< string> &dict, int l){ if( cur.size() > l) return; if( start == end){ res.push_back(cur); return; } for( int i = 0; i < start.size(); ++i){ for( int j = 'a'; j <= 'z'; ++j){ if( start[i] == j) continue; else{ string tmp = start; tmp[i] = j; if( dict.count(tmp)){ cur.push_back(tmp); dict.erase(tmp); core( res, cur, tmp, end, dict, l); cur.pop_back(); dict.insert(tmp); } } } } } int ladderLength(string start, string end, unordered_set<string> &dict) { int shortest = INT_MAX; dict.insert(end); queue< pair< string, int> > q; q.push( pair< string, int>( start, 1)); while( !q.empty()){ pair< string, int> cur = q.front(); q.pop(); if( cur.first == end){ shortest = shortest < cur.second ? shortest : cur.second; continue; } string str = cur.first; for( int i = 0; i < str.size(); ++i){ for( int j = 'a'; j <= 'z'; ++j){ if( str[i] == j) continue; string tmp = str; tmp[i] = j; if( dict.count(tmp)){ q.push( make_pair( tmp, cur.second+1)); dict.erase(tmp); } } } } if( shortest == INT_MAX) return 0; return shortest; }};