Leetcode--word Ladder II

Source: Internet
Author: User

Changed for 3 nights finally no bug ... The result timed out ... I can't figure out how to optimize .... Read the answer, is a layer of traversal, and then push the parent of each word into a vector, I am also a layer, and if the next layer encountered a word, the record word is visited, will not be visited ... Just wondering why it takes a lot more time ... Is it because my code is not concise?!!

classSolution {//with breadth search, a search tree starting from Start is created, and the child node is the parent node that changes only one letter to all the words found in the dictionary .//each child node can record its parent node and its minimum number of layers, guaranteeing the shortest path//after the tree is established, the tree is traversed, and if the node can be converted to end, the path to start is recorded and added to the sequencesPrivate: Vector<vector<string> > sequences;//Record Results Public: Vector<vector<string>> Findladders (stringStartstringEnd, unordered_set<string> &dict) {Unordered_map<string, pair<queue<string,int> > Father;//Record parent node and number of layersunordered_set<string> visited;//record has been visitedvector<string>sequence; Queue<string>que; intShortest (Int_max);//The shortest sequence to end        if(Start.size ()! = end.size () | | start.size () <1)returnsequences; intSize =start.size (); if(Size = =1) {sequence.push_back (start);            Sequence.push_back (end);            Sequences.push_back (sequence); returnsequences; }        //Access root node queue, record parent node extremely layerQue.push (start); Queue<string> FQ; Fq.push (""); Father[start]= Make_pair (FQ,1);  while(!Que.empty ()) {            //out Team            stringWord =Que.front ();            Que.pop (); //Change the alphabet             for(inti =0; i < size; i++){                 for(Charc ='a'; C <='Z'; C++){                    if(c = = Word[i])Continue; stringNewword =Word; Newword= Newword.replace (I,1,1, c); //If it is end, it is the shortest path if it appears for the first time//because it is a layer-by-layer search, the first search to end is the shortest//if the shortest path, record the path, add sequences                    if(Newword = =end) {                        if(Shortest = = Int_max) Shortest = Father[word].second +1; intCurlayer = Father[word].second +1; if(Curlayer =shortest) addsequence (father, start, end, word); }                    //if it is in the dictionary and has not been visited                    if(Dict.find (newword)! = Dict.end () && visited.find (newword) = =Visited.end ()) {                        //queue, record its layer and its parent node                        intLayer =Father[word].second; //If the Newword does not have a parent node, create                        if(Father.find (newword) = =Father.end ())                            {Que.push (Newword); Queue<string>FQ;                            Fq.push (word); Father[newword]= Make_pair (FQ, Layer +1); }//if Newword already has a parent node and the number of layers in Newword is 1 more than the number of layers in word now, increase its parent node                        Else    if(Layer <= (Father[newword].second-1) {Que.push (Newword); Auto Fq=Father[newword].first;                            Fq.push (word); Father[newword]= Make_pair (Fq,layer +1); }//If the Newword has a parent node and the current number of layers is greater than its minimum number of layers, it is marked as accessed                        Else{Visited.insert (Newword); }                                            }                }            }        }        returnsequences; } InlinevoidAddsequence (unordered_map<string, pair<queue<string,int> > &father,stringStartstringEndstringword) {Vector<string>sequence; if(Word! =end) Sequence.push_back (word);        Sequence.push_back (end); Auto Wordfather=Father[word].first; Auto Fatherword=Wordfather.front (); Auto N=Father[word].second; if(Wordfather.size () >1) {wordfather.pop (); Father[word]=Make_pair (Wordfather, N); } Word=Fatherword;  while(Word! = Start && Word! ="") {Sequence.insert (Sequence.begin (), word); Wordfather=Father[word].first; Auto Fatherword=Wordfather.front (); Auto N=Father[word].second; if(Wordfather.size () >1) {wordfather.pop (); Father[word]=Make_pair (Wordfather, N); } Word=Fatherword; }        if(sequence[0] !=start) Sequence.insert (Sequence.begin (), start);    Sequences.push_back (sequence); }};

Leetcode--word Ladder II

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.