要求最短距离。采纳dijkstra查找节点之间的最短路径。

当心:假设是一个枚举字典22是否元素可以,如果转换,暂停。

提高:每串,带您历数它的字符值事件,对于的长度n一个字符串枚举n*26次要。

设仅仅是简单的枚举,则会出现重边:

如abc,bbc,cbc,建图后每两个节点间均有两条双向边,这对于邻接表存储的图会存在非常多冗余边。

解决方法:每一个节点每位字符仅仅能从原始字符往后枚举,即

枚举各字符串第一位的话

abc:bbc,cbc,dbc,...

bbc:cbc,dbc,...

cbc:dbc,....

struct Edge{
int v,next;
};
class Solution {
public:
Edge*e;
int *head,len,n;
void addedge(int u,int v){
e[len].v=v;
e[len].next=head[u];
head[u]=len++;
e[len].v=u;
e[len].next=head[v];
head[v]=len++;
}
bool canTrans(const string& p,const string&q){
int i,cnt=0;
for(i=0;i<p.size();++i){
if(p[i]!=q[i]){
cnt++;
if(cnt>1)return 0;
}
}
return 1;
}
int dijk(int st,int ed){
int* dist=new int[n],i,v,j,k;
memset(dist,127,sizeof(int)*n);
int unre=dist[0];
for(i=head[st];i!=-1;i=e[i].next){
v=e[i].v;
dist[v]=1;
}
dist[st]=-1;
for(j=1;j<n;++j){
for(i=0,k=-1;i<n;++i)
if(dist[i]>0&&dist[i]!=unre&&(k<0||dist[i]<dist[k]))
k=i;
if(k<0||k==ed)break;
for(i=head[k];i!=-1;i=e[i].next){
v=e[i].v;
if(dist[v]>=0&&dist[v]>dist[k]+1)
dist[v]=dist[k]+1;
}
dist[k]=-1;
}
if(k==ed)
k=dist[k];
else k=-1;
delete[]dist;
return k;
}
int ladderLength(string start, string end, unordered_set<string> &dict) {
if(start==end)return 2;
map<string,int>mp;
int cnt=0,i;
mp[start]=cnt++;
mp[end]=cnt++;
unordered_set<string>::iterator bg=dict.begin(),ed=dict.end(),bg2;
for(;bg!=ed;bg++){
if(mp.find(*bg)==mp.end())
mp[*bg]=cnt++;
}
dict.insert(start);
dict.insert(end);
n=dict.size();
e=new Edge[n*n];
head=new int[n];
len=0;
memset(head,-1,sizeof(int)*n);
int ch,j;
for(bg=dict.begin(),ed=dict.end();bg!=ed;bg++){
string s=*bg;
for(i=0;i<s.length();++i){
ch=s[i]-'a';
for(j=ch+1;j<26;++j){
s[i]='a'+j;
if(dict.find(s)!=dict.end())
addedge(mp[s],mp[*bg]);
}
s[i]='a'+ch;
}
/* for(bg2=bg,bg2++;bg2!=ed;bg2++){
if(canTrans(*bg,*bg2)){
addedge(mp[*bg],mp[*bg2]);
}
}*/
}
i=dijk(mp[start],mp[end]);
delete[] e;
delete[]head;
return i>=0?i+1:0;
}
};

版权声明:本文博客原创文章,博客,未经同意,不得转载。

[LeetCode]Word Ladder 最短距离字符串转换 (Dijkstra)的更多相关文章

  1. LeetCode:Word Ladder I II

    其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...

  2. [leetcode]Word Ladder II @ Python

    [leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...

  3. Leetcode(8)字符串转换整数

    Leetcode(8)字符串转换整数 [题目表述]: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我 ...

  4. [LeetCode] Word Ladder 词语阶梯

    Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...

  5. LeetCode: Word Ladder II [127]

    [题目] Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...

  6. [LeetCode] Word Ladder II 词语阶梯之二

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  7. LeetCode Word Ladder 找单词变换梯

    题意:给出两个单词,以及一个set集合,当中是很多的单词.unordered_set是无序的集合,也就是说找的序列也是无序的了,是C++11的标准,可能得升级你的编译器版本了.要求找出一个从start ...

  8. LeetCode :Word Ladder II My Solution

    Word Ladder II Total Accepted: 11755 Total Submissions: 102776My Submissions Given two words (start  ...

  9. LeetCode: Word Ladder II 解题报告

    Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation s ...

随机推荐

  1. 访何红辉:谈谈Android源码中的设计模式

    最近Android 6.0版本的源代码开放下载,刚好分析Android源码的技术书籍<Android源码设计模式解析与实战>上市,我们邀请到它的作者何红辉,来谈谈Android源码中的设计 ...

  2. DIV固定在页面某个位置,不随鼠标滚动而滚动

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org ...

  3. cocos2d-x CCNode类

    文章引用自http://blog.csdn.net/qiurisuixiang/article/details/8763260 1 CCNode是cocos2d-x中一个非常重要的类.CCNode是场 ...

  4. configure: error: zlib not installed

    export LDFLAGS="-L/usr/local/zlib/lib" export CPPFLAGS="-I/usr/local/zlib/include&quo ...

  5. (白书训练计划)UVa 120 Stacks of Flapjacks(构造法)

    题目地址:UVa 120 水题. 从最大的開始移,每次都把大的先翻到最上面,再翻到以下. 代码例如以下: #include <iostream> #include <cstdio&g ...

  6. HTML5游戏开发进阶指南

    <HTML5游戏开发进阶指南> 基本信息 作者: (印)香卡(Shankar,A.R.)    译者: 谢光磊 出版社:电子工业出版社 ISBN:9787121212260 上架时间:20 ...

  7. 在静态方法里调用spring注入的方法

    在静态方法里直接调用非静态方法是不行的. 那如何调用spring注入的方法呢? @Component public class AutoLoginUtil {     @Autowired     p ...

  8. linux下用shell删除三天前或者三天内的文件

    说明:+n 大于 n, -n 小于 n, n 相等于 n. find / -amin -30 -ls # 查找在系统中最后30分钟访问的文件find / -atime -2 -ls # 查找在系统中最 ...

  9. Java流读写

    写: package com.wjy.write; import java.io.BufferedWriter; import java.io.FileOutputStream; import jav ...

  10. sql小技巧 group by datetime类型字段,只取其中的日期部分

    工作中经常会遇到,要在sql中查询报表,查询结果要求按照日期来罗列, 或按照天, 或按照月,年. 这个时候我们经常会苦恼,datetime是精确到毫秒的,如果单纯的group by datetime就 ...