leetcode 127. Word Ladder[bfs] lintcode 120. 单词接龙

博客围绕LeetCode上单词转换最短序列问题展开,要求每次仅变一个字母且转换后的单词需在字典列表中。作者起初用DFS求解遇问题,后用BFS求解,还提到step随状态变化、不一定用优先队列等要点,也尝试用deque改写,还提及lintcode版本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://leetcode.com/problems/word-ladder/

Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that:

  1. Only one letter can be changed at a time.
  2. Each transformed word must exist in the word list. Note that beginWord is not a transformed word.

Note:

  • Return 0 if there is no such transformation sequence.
  • All words have the same length.
  • All words contain only lowercase alphabetic characters.
  • You may assume no duplicates in the word list.
  • You may assume beginWord and endWord are non-empty and are not the same.

Example 1:

Input:
beginWord = "hit",
endWord = "cog",
wordList = ["hot","dot","dog","lot","log","cog"]

Output: 5

Explanation: As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length 5.

Example 2:

Input:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]

Output: 0

Explanation: The endWord "cog" is not in wordList, therefore no possible transformation.

功力退化啊 ……

区区搜索dfs返回值有问题,看到后面提示说是有bfs,吭哧吭哧写那么多

point:

1.step是随着状态变而不是随着while变,这个是记录在每一步的结构体信息中的

2.不一定一定要优先队列,普通的一样可以

3.真的和普通的bfs没多大区别,咋写的这么费劲

class Solution {
public:
    struct node {
        int pos,step;
        node(int apos, int astep) {
            pos = apos;
            step = astep;
        }
    };
    bool comp(string x, string y) {
        if (x.length() != y.length()) 
            return false;
        int cnt = 0;
        for (int i = 0; i < x.length(); i ++) {
            if (x[i] == y[i])
                cnt ++;
        }
        if (cnt == x.length()-1)
            return true;
        return false;
    }
    int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
        int len = wordList.size();
        vector<bool> vis(len, false);
        queue<node> q;
        for (int i = 0; i < wordList.size(); i ++) {
            if (comp(beginWord, wordList[i])) {
                q.push(node(i,1));
                vis[i] = true;
                //break;
            }
        }
        while(!q.empty()) {
            node x = q.front();
            cout<< wordList[x.pos] << x.step << endl;
            if (wordList[x.pos] == endWord)
                return x.step+1;
            q.pop();
            for (int i = 0; i < wordList.size(); i ++) {
                if (vis[i] == false && comp(wordList[x.pos], wordList[i])) {
                    q.push(node(i, x.step + 1));
                    vis[i] = true;
                }
            }
        }
        return 0;
    }
};

 

class Solution {
public:
    struct node {
        int pos,step;
        node(int apos, int astep) {
            pos = apos;
            step = astep;
        }
        friend bool operator <(node a,node b)
        {
            return b.step<a.step;///
        }
    };
    bool comp(string x, string y) {
        if (x.length() != y.length()) 
            return false;
        int cnt = 0;
        for (int i = 0; i < x.length(); i ++) {
            if (x[i] == y[i])
                cnt ++;
        }
        if (cnt == x.length()-1)
            return true;
        return false;
    }
    int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
        int len = wordList.size();
        vector<bool> vis(len, false);
        priority_queue<node> q;
        for (int i = 0; i < wordList.size(); i ++) {
            if (comp(beginWord, wordList[i])) {
                q.push(node(i,1));
                vis[i] = true;
                //break;
            }
        }
        while(!q.empty()) {
            node x = q.top();
            cout<< wordList[x.pos] << x.step << endl;
            if (wordList[x.pos] == endWord)
                return x.step+1;
            q.pop();
            for (int i = 0; i < wordList.size(); i ++) {
                if (vis[i] == false && comp(wordList[x.pos], wordList[i])) {
                    q.push(node(i, x.step + 1));
                    vis[i] = true;
                }
            }
        }
        return 0;
    }
};

试着改了一个deque的

class Solution {
public:
    struct node {
        int pos,step;
        node(int apos, int astep) {
            pos = apos;
            step = astep;
        }
    };
    bool comp(string x, string y) {
        if (x.length() != y.length()) 
            return false;
        int cnt = 0;
        for (int i = 0; i < x.length(); i ++) {
            if (x[i] == y[i])
                cnt ++;
        }
        if (cnt == x.length()-1)
            return true;
        return false;
    }
    int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
        int len = wordList.size();
        vector<bool> vis(len, false);
        deque<node> q;
        for (int i = 0; i < wordList.size(); i ++) {
            if (comp(beginWord, wordList[i])) {
                q.push_front(node(i,1));
                vis[i] = true;
                //break;
            }
        }
        while(!q.empty()) {
            node x = q.front();
            //cout<< wordList[x.pos] << x.step << endl;
            if (wordList[x.pos] == endWord)
                return x.step+1;
            q.pop_front();
            for (int i = 0; i < wordList.size(); i ++) {
                if (vis[i] == false && comp(wordList[x.pos], wordList[i])) {
                    q.push_back(node(i, x.step + 1));
                    vis[i] = true;
                }
            }
        }
        return 0;
    }
};

下面是lintcode的版本 unorder_set真是烦死了

class Solution {
public:
    /*
     * @param start: a string
     * @param end: a string
     * @param dict: a set of string
     * @return: An integer
     */
     struct node {
        string pos;
        int step;
        node(string apos, int astep) {
            pos = apos;
            step = astep;
        }
    };
    bool comp(string x, string y) {
        if (x.length() != y.length()) 
            return false;
        int cnt = 0;
        for (int i = 0; i < x.length(); i ++) {
            if (x[i] == y[i])
                cnt ++;
        }
        if (cnt == x.length()-1)
            return true;
        return false;
    }
    int ladderLength(string &start, string &end, unordered_set<string> &dict) {
        int len = dict.size();
	vector<bool> vis(len, false);
	deque<node> q;
	int i = 0;
	if (comp(start, end)) {
		return 2;
	}
	for (unordered_set<string>::iterator word = dict.begin(); word != dict.end(); word++) {
		if (comp(start, *word)) {
			cout<<"word:"<<*word <<" step:1" <<endl;
			q.push_front(node(*word,2));
			vis[i] = true;
			//break;
		}
		i ++;
	}
	while(!q.empty()) {
		node x = q.front();
		//cout<< wordList[x.pos] << x.step << endl;
		if (comp(x.pos,end)) {
			cout<<x.pos<<endl;
			return x.step+1;
		}
		q.pop_front();
		int i = 0;
		for (unordered_set<string>::iterator word = dict.begin(); word != dict.end(); word++) {
			if (vis[i] == false && comp(x.pos, *word)) {
				cout<<"word:"<<*word <<" step:" <<x.step+1<<endl;
				q.push_back(node(*word, x.step + 1));
				vis[i] = true;
			}
			i++;
		}
	}
	return 0;

    }
};

其实queue就可以的。。。。

class Solution {
public:
    struct node {
        int pos,step;
        node(int apos, int astep) {
            pos = apos;
            step = astep;
        }
    };
    bool comp(string x, string y) {
        if (x.length() != y.length()) 
            return false;
        int cnt = 0;
        for (int i = 0; i < x.length(); i ++) {
            if (x[i] == y[i])
                cnt ++;
        }
        if (cnt == x.length()-1)
            return true;
        return false;
    }
    int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
        int len = wordList.size();
        vector<bool> vis(len, false);
        queue<node> q;
        for (int i = 0; i < wordList.size(); i ++) {
            if (comp(beginWord, wordList[i])) {
                q.push(node(i,1));
                vis[i] = true;
                //break;
            }
        }
        while(!q.empty()) {
            node x = q.front();
            //cout<< wordList[x.pos] << x.step << endl;
            if (wordList[x.pos] == endWord)
                return x.step+1;
            q.pop();
            for (int i = 0; i < wordList.size(); i ++) {
                if (vis[i] == false && comp(wordList[x.pos], wordList[i])) {
                    q.push(node(i, x.step + 1));
                    vis[i] = true;
                }
            }
        }
        return 0;
    }
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值