[LeetCode]Word Ladder 最短距离字符串转换 (Dijkstra)
要求最短距离。采纳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)的更多相关文章
- LeetCode:Word Ladder I II
其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...
- [leetcode]Word Ladder II @ Python
[leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...
- Leetcode(8)字符串转换整数
Leetcode(8)字符串转换整数 [题目表述]: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我 ...
- [LeetCode] Word Ladder 词语阶梯
Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...
- LeetCode: Word Ladder II [127]
[题目] Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...
- [LeetCode] Word Ladder II 词语阶梯之二
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- LeetCode Word Ladder 找单词变换梯
题意:给出两个单词,以及一个set集合,当中是很多的单词.unordered_set是无序的集合,也就是说找的序列也是无序的了,是C++11的标准,可能得升级你的编译器版本了.要求找出一个从start ...
- LeetCode :Word Ladder II My Solution
Word Ladder II Total Accepted: 11755 Total Submissions: 102776My Submissions Given two words (start ...
- LeetCode: Word Ladder II 解题报告
Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation s ...
随机推荐
- HashMap的工作原理(转)
HashMap的工作原理是近年来常见的Java面试题.几乎每个Java程序员都知道HashMap,都知道哪里要用HashMap,知道Hashtable和HashMap之间的区别,那么为何这道面试题如此 ...
- nohup sort -k1 -n -t$'\t' ./bigfile.16 -o./test/bigfile.16.ok &
nohup sort -k1 -n -t$'\t' ./bigfile.16 -o./test/bigfile.16.ok &
- Houdini Pyro流体的插值变速
用简单的节点尝试了下Houdini流体的变速,这里的流体指的是Pyro,而不是FLIP.FLIP仅仅须要记录ID属性然后TimeBlend & TimeShift就可以. Vimeo 上图是一 ...
- GitHub 优秀Android 开源项目
阅读目录 1.Xabber客户端 2.oschina客户端 3.手机安全管家 4.星座连萌 5.玲闹铃 6.魔乐盒 7.PWP日历 8.Apollo音乐播放器 9.夏普名片识别 10.高仿人人网 11 ...
- OCP读书笔记(7) - 使用RMAN执行恢复
7.Using RMAN to Perform Recovery 使用RMAN进行完全恢复system表空间文件丢失的恢复 模拟损坏: SQL> conn /as sysdba; SQL> ...
- LeetCode My Solution: Minimum Depth of Binary Tree
Minimum Depth of Binary Tree Total Accepted: 24760 Total Submissions: 83665My Submissions Given a bi ...
- Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)(转)
互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...
- libsvm中的dec_values以及分类结果评分问题
最近一个图像识别项目里需要对分类的结果进行打分,因为使用的是libsvm3.12,一开始决定直接将svm_predict_values函数的dec_values作为评分返回,后来研究了之后才觉得里面有 ...
- hdu1243(最长公共子序列变形)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1243 分析:dp[i][j]表示前i个子弹去炸前j个恐怖分子得到的最大分.其实就是最长公共子序列加每个 ...
- vim忽略大写和小写查找配置
作者:zhanhailiang 日期:2014-12-17 默认 vim 的查找是区分大写和小写,可通过下面两种方式实现忽略大写和小写查找 set ic? noignorecase 1 指令设定: : ...