码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode Word Ladder 广搜

时间:2014-09-13 12:02:15      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   使用   ar   strong   for   

bubuko.com,布布扣

利用两个队列(或vector):curlevel和nextlvevl分别表示当前层的所有可能状态和转换到的下一层的所有可能状态。我们的目标是转换到end单词即可,深搜会超时。使用广搜,各层的可能结点同时向下进行,找到end即可return。当找完当前层的所有可能结点后,当前层也就空了,然后swap(当前层,下一层)循环向下搜索。

下面两个代码都是用这个方法,第一个超时,使用第二段代码,对于每个结点,只需找25*len种下一个可能结点是否存在,第一段代码需要在大数据里慢慢找。

note:在一个字符串集合里,找到一个由某个字符串改变一个字符而成的字符串(判定其是否存在),可以通过枚举这个字符串的所有的可能变换情况,然后在字符串集合里查找是否存在,从而避开了大数据的困扰。

Time Limit Exceeded

class Solution {
    bool cantrans(string str1, string str2)
    {
        int len=str1.length();
        int count=0;
        for(int i=0; i<len; i++)
            if(str1[i] != str2[i]){
                count++;
                if(count>1) return false;
            }
        if(count==0) return false;
        return true;
    }
public:
    int ladderLength(string start, string end, unordered_set<string> &dict) {

        dict.insert(end);
        set<string>visited;
        queue<string>curlevel, nextlevel;
        curlevel.push(start);
        int level=1;
        
        while(true)
        {
            if(!curlevel.empty()){
                string curstr=curlevel.front();
                curlevel.pop();
                for(auto i=dict.begin(); i!=dict.end(); i++){
                    if(cantrans(curstr, *i) && visited.find(*i)==visited.end()){
                        if(*i==end) return level+1;
                        visited.insert(*i);
                        nextlevel.push(*i);
                    }
                }
            } else {
                swap(curlevel, nextlevel);
                level++;
            }
            if(nextlevel.empty() && curlevel.empty()) break;
        }
        return 0;
    }
};

AC:

class Solution {
    
public:
    int ladderLength(string start, string end, unordered_set<string> &dict) {

        dict.insert(end);
        set<string>visited;
        queue<string>curlevel, nextlevel;
        curlevel.push(start);
        int level=1;

        while(true)
        {
            if(!curlevel.empty()){
                string curstr=curlevel.front();
                curlevel.pop();
                for(size_t i=0; i<curstr.size(); i++){
                    for(char c=a; c<=z; c++){
                        if(c == curstr[i]) continue;
                        swap(c, curstr[i]);
                        if (dict.count(curstr) > 0 && !visited.count(curstr)) {
                            if(curstr==end) return level+1;
                            visited.insert(curstr);
                            nextlevel.push(curstr);
                        }
                        swap(c, curstr[i]);
                    }
                }
            } else {
                swap(curlevel, nextlevel);
                level++;
            }
            if(nextlevel.empty() && curlevel.empty()) break;
        }
        return 0;
    }
};

leetcode Word Ladder 广搜

标签:style   blog   http   color   io   使用   ar   strong   for   

原文地址:http://www.cnblogs.com/vegg117/p/3969594.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!