Test instructions: Give two words, and a set set, which is a lot of words. Unordered_set are unordered collections, which means that the sequence you are looking for is unordered and c++11, and you may have to upgrade your compiler version. Ask for a transformation sequence of two words from start to end. Starting with start, you can change one letter at a time, and the changed word must be in set, and the final request becomes end, asking how many intermediate transformations have elapsed? Note that you need to add 2 more times (start and end are counted), which is the rule.
Idea: Breadth search, with start as the root, a layer of expansion, until you find the end, return the depth of the number. This is done by first drawing the root start, traversing the set, all of which can be transformed by the 1-letter transformation of the start, and taken out (to be deleted) as a second layer, that is, the child of the root. Then, starting with each element of the second layer, continue traversing the elements in the set until end is searched and the depth of the calculation is returned.
Note: Arduous use of g++ 4.8.1 version to compile the test, the network can be used for each letter of the current word with a~z each letter instead of once, and then find out in set, this method does not feel the advantage, n words, the word length is k, the worst probably n*k*26 times. The following is N*k. Very tired without detailed verification, probably so. It took 3 days to know that the second formula in the for parenthesis was broken, and it was checked every time, that is, updating the bounds.
1 classSolution {2 Public:3 BOOLMatstring&lef,Const string&rig)/*returns whether two strings match (allows a letter mismatch)*/4 {5 intCount=0;6 for(intI=0; I<lef.size (); i++)7 {8 if(lef[i]!=Rig[i])9 {Tencount++; One if(count>=2)return false; A } - } - return true;//It is impossible to be equal, that is, the count=0 situation. the } - - intLadderlength (stringStartstringEnd, unordered_set<string> &dict) { - if(Start.empty () | | end.empty () | | start==end | | start.length ()! = End.length ())return 0; + if(Mat (Start,end))return 2;//there is only one mismatch. - if(Dict.find (end) = = Dict.end ()) Dict.insert (end);//end must be in the set + if(Dict.find (Start)!=dict.end ()) dict.erase (start);//start must not be Setzhong Aunordered_set<string>::iterator dist = dict.find (end);//End Pointer atunordered_set<string>::iterator it =Dict.begin (); -queue<string>que; -Que.push (start);//starting point Advanced Team - intCount=1; - while(!que.empty ()) - { incount++; - intQ=que.size ();//note here that que.size () cannot be placed in parentheses in the next line instead of Q, and it is checked once per loop to for(intI=0; i<q; i++)//this for scanning elements of the same layer + { -it =Dict.begin (); the while(It!=dict.end ())//search every element in Dict * { $ if(Mat (Que.front (), *it)) Panax Notoginseng { - if(it = = dist)returnCount//find the Finish end theQue.push (*it); +it = dict.erase (it);//Delete from the collection A } the Elseit++; + } - Que.pop (); $ } $ } - return 0; - } the};
Word Ladder
Leetcode Word Ladder Find word change ladder