要求最短距离。采纳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. java学习笔记06--正则表达式

    java学习笔记06--正则表达式 正则表达式可以方便的对数据进行匹配,可以执行更加复杂的字符串验证.拆分.替换等操作. 例如:现在要去判断一个字符串是否由数字组成,则可以有以下的两种做法 不使用正则 ...

  2. ZooKeeper的安装、配置、启动和使用(一)——单机模式

    ZooKeeper的安装.配置.启动和使用(一)——单机模式 ZooKeeper的安装非常简单,它的工作模式分为单机模式.集群模式和伪集群模式,本博客旨在总结ZooKeeper单机模式下如何安装.配置 ...

  3. Codeforces Round #296 (Div. 2) A B C D

    A:模拟辗转相除法时记录答案 B:3种情况:能降低2,能降低1.不能降低分别考虑清楚 C:利用一个set和一个multiset,把行列分开考虑.利用set自带的排序和查询.每次把对应的块拿出来分成两块 ...

  4. SmartDraw2008破解过程总结

    SmartDraw2008破解过程总结作者:chszs  原创转载请保留作者名. 按下列步骤完毕,保证能够支持中文. 一.所需软件:1)SmartDraw2008安装软件:2)SmartDraw200 ...

  5. 知识网之C++总结

    米老师常说的一句话:构造知识网. 立即要考试了.就让我们构造一下属于C++的知识网.首先从总体上了解C++: 从图中能够了解到,主要有五部分.而当我们和之前的知识联系的话,也就剩下模板和运算符重载以及 ...

  6. form表单中的 action=./?> 是什么意思

    ./代表当前目录,?代表查询字符串为空 action="" //一般可以为空的,这里的双引号都要有的,表示提单提交给自己(也就是当前页处理)action="a.php&q ...

  7. 从零开始,创建GitHub团队开发环境

    从零开始,创建GitHub团队开发环境 GitHub提供免费的团队环境,不过免费仓库容量是300MB,请大家注意. 申请GitHub个人账号 1. 使用浏览器访问GitHub主页.如果使用IE,尽量不 ...

  8. poj3642 Charm Bracelet(0-1背包)

    题目意思: 给出N,M,N表示有N个物品,M表示背包的容量.接着给出每一个物品的体积和价值,求背包可以装在的最大价值. http://poj.org/problem? id=3624 题目分析: o- ...

  9. hdu1503(最长公共子序列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1503 题意:由两个字符串构造出另一个字符串,该字符串包含前两个字符串(按字符顺序,但不一定连续),使该 ...

  10. JavaScript中的对象(一)

    Email:longsu2010 at yeah dot net 最近我和朋友谈起JavaScript中对象的问题.朋友以写JavaScript为生,而且生活的很好,然而我发现他并不真正懂这们语言的某 ...