• Word Ladder II


    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.
    struct Node
    {
        int vindex;
        int path;
        int previous;
    };
    class Solution {
    public:
        vector<vector<string>> findLadders(string start, string end, unordered_set<string> &dict) 
        {
            vector<vector<string>> result;
            int len=start.length();
    
            vector<string> vdict;
            map<string,int> ids;
            vdict.push_back(start);
            ids[start]=0;
            for(unordered_set<string>::iterator it=dict.begin();it!=dict.end();it++)
            {
                ids[*it]=vdict.size();
                vdict.push_back(*it);            
            }
            vdict.push_back(end);
            ids[end]=vdict.size()-1;
    
            dict.insert(start);
            dict.insert(end);
    
            vector<vector<int>>adj;
            for(int i=0;i<vdict.size();i++)
            {
                vector<int> adj0;
                string str=vdict[i];
                for(int j=0;j<len;j++)
                {
                    for(char c='a';c<='z';c++)
                        if(vdict[i][j]!=c)
                        {
                            str[j]=c;
                            if(dict.count(str)) adj0.push_back(ids[str]);                        
                        }
                    str[j]=vdict[i][j];
                }
                adj.push_back(adj0);
            }                                
            
            vector<Node*> path;        
            Node* newnode=new Node();
            newnode->vindex=ids[start];
            newnode->path=1;
            newnode->previous=-1;
            path.push_back(newnode);                                    
    
            int index=0;
            int minpath=0;        
            int oldpath=0;
    
            while(index<path.size())
            {            
                if(path[index]->path>oldpath)
                {
                    oldpath=path[index]->path;
                    int i=index;
                    while(i<path.size())
                    {                    
                        unordered_set<string>::iterator it= dict.find(vdict[path[i]->vindex]);
                        if(it!=dict.end()) dict.erase(it);
                        i++;
                    }                
                }
                if(minpath>0 && path[index]->path==minpath)
                    break;                
                // a path found
                if(path[index]->vindex==vdict.size()-1)
                    minpath=path[index]->path;
                //next word
                for(int i=0;i<adj[path[index]->vindex].size();i++)
                {                   
                    if(dict.count(vdict[adj[path[index]->vindex][i]]))
                    {                            
                            newnode=new Node();
                            newnode->path=oldpath+1;
                            newnode->vindex=adj[path[index]->vindex][i];
                            newnode->previous=index;
                            path.push_back(newnode);
                    }                        
                }
                index++;
            }
    
            for(int i=0;i<path.size();i++)
            if(path[i]->path==minpath && vdict[path[i]->vindex]==end)
            {
                vector<string> words;
                int index=i;
                do
                {
                    words.insert(words.begin(),vdict[path[index]->vindex]);
                    index=path[index]->previous;
                }
                while(index!=-1);
                result.push_back(words);
            }
            
            return result;
        }    
    };
  • 相关阅读:
    hdu--1018--Big Number(斯特林公式)
    NYOJ--56--阶乘因式分解(一)
    hdu--1711--kmp应用在整形数组--Number Sequence
    HDU--1358--KMP算法失配函数getfail()的理解--Period
    C++STL(vector,map,set,list,bitset,deque)成员函数整理
    NYOJ--95--multiset--众数问题
    NYOJ--86--set.find()--找球号(一)
    NYOJ--19--next_permutation()--擅长排列的小明
    NYOJ--714--Card Trick
    NYOJ--2--括号配对问题
  • 原文地址:https://www.cnblogs.com/erictanghu/p/3759676.html
Copyright © 2020-2023  润新知