一些bfs的想法:如果存在重复孩子时,bfs一定用level层级来搜索。
裸bfs+dfs,即使用unordered_map优化还是会T掉:
T掉的代码:
/*
* @lc app=leetcode id=126 lang=cpp
*
* [126] Word Ladder II
*/
// @lc code=start
class Solution {
public:
vector<vector<string>> findLadders(string beginWord,string endWord,vector<string>& wordList)
{
unordered_map<string,int> level; // 层级从1开始
unordered_map<string,vector<string>> edge; // 邻接表
bfs(wordList,beginWord,endWord,level,edge);
vector<vector<string>> ans;
vector<string> path;
path.push_back(beginWord);
dfs(edge,level,beginWord,endWord,ans,path);
return ans;
}
void bfs(vector<string>& wordList,string& beginWord,string& endWord,unordered_map<string,int>& level,unordered_map<string,vector<string>>& edge){
queue<string> q;
q.push(beginWord);
level[beginWord] = 1;
string now;
while(!q.empty()){
now = q.front();
q.pop();
for(int i=0;i<wordList.size();i++){
if(judge(wordList[i],now) && (level[wordList[i]] == 0 || level[wordList[i]] == level[now]+1)){
if(level[wordList[i]] == 0){
q.push(wordList[i]);
level[wordList[i]] = level[now] + 1;
}
edge[now].push_back(wordList[i]);
}
}
}
}
void dfs(unordered_map<string,vector<string>>& edge,unordered_map<string,int>& level,string& nowWord,string& endWord,vector<vector<string>>& ans,vector<string>& path){
if(level[nowWord] >= level[endWord]){
if(nowWord == endWord) ans.push_back(path);
return ;
}
for(int i=0;i<edge[nowWord].size();i++){
if(level[edge[nowWord][i]] == level[nowWord] + 1){
path.push_back(edge[nowWord][i]);
dfs(edge,level,edge[nowWord][i],endWord,ans,path);
path.pop_back();
}
}
}
bool judge(string& a,string& b){
int cnt = 0;
for(int i=0;i<a.length();i++) if(a[i] != b[i]) cnt++;
return cnt==1?true:false;
}
};
// @lc code=end
关键在于judge函数调用太频繁,太耗时,时间复杂度是 O(N * N * L)
采用构造+哈希解决,时间复杂度是 O(N * 26 * L)
虽然AC了,但是很慢,1576ms,超了10%代码,感觉还不够:
/*
* @lc app=leetcode id=126 lang=cpp
*
* [126] Word Ladder II
*/
// @lc code=start
class Solution {
public:
vector<vector<string>> findLadders(string beginWord,string endWord,vector<string>& wordList)
{
unordered_map<string,int> level; // 层级从1开始
unordered_map<string,vector<string>> edge; // 邻接表
unordered_map<string,bool> has; // 判断
for(int i=0;i<wordList.size();i++) has[wordList[i]] = true;
bfs(beginWord,endWord,level,has,edge);
//for(auto m:edge){cout<<m.first<<"-";for(auto n:m.second)cout<<n<<" ";cout<<endl;}
vector<vector<string>> ans;
vector<string> path;
path.push_back(beginWord);
dfs(edge,level,beginWord,endWord,ans,path);
return ans;
}
void bfs(string& beginWord,string& endWord,unordered_map<string,int>& level,unordered_map<string,bool> has,unordered_map<string,vector<string>>& edge){
queue<string> q;
q.push(beginWord);
level[beginWord] = 1;
string now;
string next;
while(!q.empty()){
next = now = q.front();
q.pop();
for(int i=0;i<now.length();i++){
for(char j='a';j<='z';j++){
if(j == now[i]) continue;
next[i] = j;
//cout<<"<"<<now<<" "<<next<<">"<<endl;
if(has[next] && (level[next] == 0 || level[next] == level[now]+1)){
if(level[next] == 0){
q.push(next);
level[next] = level[now] + 1;
}
edge[now].push_back(next);
}
}
next[i] = now[i];
}
}
}
void dfs(unordered_map<string,vector<string>>& edge,unordered_map<string,int>& level,string& nowWord,string& endWord,vector<vector<string>>& ans,vector<string>& path){
if(level[nowWord] >= level[endWord]){
if(nowWord == endWord) ans.push_back(path);
return ;
}
for(int i=0;i<edge[nowWord].size();i++){
if(level[edge[nowWord][i]] == level[nowWord] + 1){
path.push_back(edge[nowWord][i]);
dfs(edge,level,edge[nowWord][i],endWord,ans,path);
path.pop_back();
}
}
}
};
// @lc code=end
双向bfs,AC了,520ms,感觉还有优化的空间
/*
* @lc app=leetcode id=126 lang=cpp
*
* [126] Word Ladder II
*/
// @lc code=start
class Solution {
public:
void dfs(vector<vector<string>>& ans,vector<string>& path,unordered_map<string,vector<string>>& e,const string& now,const string& T){
//cout<<"\ndfsb: "<<now;
if(now == T){
ans.push_back(path);
return ;
}
for(const auto& i : e[now]){
path.push_back(i);
dfs(ans,path,e,i,T);
path.pop_back();
}
}
bool judge(string& a,string& b){
int cnt = 0;
for(int i=0;i<a.length();i++){
if(a[i] != b[i]) cnt++;
}
return cnt==1;
}
vector<vector<string>> findLadders(string beginWord, string endWord, vector<string>& wordList) {
int N = wordList.size();
vector<vector<string>> ans;
unordered_map<string,int> lvf,lvb; // 层级
for(int i=0;i<N;i++) lvf[wordList[i]] = lvb[wordList[i]] = N+1;
if(!lvf[endWord]) return ans;
if(judge(beginWord,endWord)){
vector<string> t;
t.push_back(beginWord);
t.push_back(endWord);
ans.push_back(t);
return ans;
}
unordered_map<string,vector<string>> ef,eb; // 边
unordered_set<string> mid; // 交点
queue<string> *qf,*qb,*qt;
qf = new queue<string>;
qb = new queue<string>;
qt = new queue<string>;
lvf[beginWord] = lvb[endWord] = 1;
qf->push(beginWord);
qb->push(endWord);
int L = beginWord.length();
bool flag = true;
string now,next;
while(flag && (!qf->empty() || !qb->empty())){
// 前向bfs
while(!qf->empty()){
now = next = qf->front();
qf->pop();
//cout<<"\nfront now: "<<now<<endl;
for(int i=0;i<L;i++){
for(char j='a';j<='z';j++){
if(now[i] == j) continue;
next[i] = j;
if(lvf[next] > lvf[now]){
//cout<<" "<<next;
if(lvf[next] > lvf[now] + 1){
lvf[next] = lvf[now] + 1;
qt->push(next);
}
ef[next].push_back(now);
if(!eb[next].empty()){
flag = false;
mid.insert(next);
}
}
}
next[i] = now[i];
}
}
swap(qf,qt);
if(!flag) break;
// 后向bfs
while(!qb->empty()){
now = next = qb->front();
qb->pop();
//cout<<"\nback now: "<<now<<endl;
for(int i=0;i<L;i++){
for(char j='a';j<='z';j++){
if(now[i] == j) continue;
next[i] = j;
if(lvb[next] > lvb[now]){
//cout<<" "<<next;
if(lvb[next] > lvb[now] + 1){
lvb[next] = lvb[now] + 1;
qt->push(next);
}
eb[next].push_back(now);
if(!ef[next].empty()){
flag = false;
mid.insert(next);
}
}
}
next[i] = now[i];
}
}
swap(qb,qt);
}
vector<string> path;
for(const auto & p : mid){
//cout<<"\nmid: "<<p;
vector<vector<string>> ansf,ansb;
dfs(ansf,path,ef,p,beginWord);
dfs(ansb,path,eb,p,endWord);
for(const auto & vi : ansf){
for(const auto & vj : ansb){
vector<string> anst;
anst.insert(anst.end(),vi.rbegin(),vi.rend());
anst.push_back(p);
anst.insert(anst.end(),vj.begin(),vj.end());
ans.push_back(anst);
}
}
}
return ans;
}
};
// @lc code=end