[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 ...
随机推荐
- ExtJS学习-----------Ext.Array,ExtJS对javascript中的Array的扩展
关于ExtJS对javascript中的Array的扩展.能够參考其帮助文档,文档下载地址:http://download.csdn.net/detail/z1137730824/7748893 因为 ...
- Linux实现字符设备驱动的基础步骤
Linux应用层想要操作kernel层的API,比方想操作相关GPIO或寄存器,能够通过写一个字符设备驱动来实现. 1.先在rootfs中的 /dev/ 下生成一个字符设备.注意主设备号 和 从设备号 ...
- gcc入门
gcc入门 1. 什么是gcc gcc的全称是GUN Compiler Collection,它是一个能够编译多种语言的编译器.最开始gcc是作为C语言的编译器(GNU C Compiler),现在除 ...
- 鼠标右键怎么清除Catalyst Control Center
开始→运行→regedit→找到HKEY_CLASSES_ROOT\Directory\Background\shellex\ContextMenuHandlers\ACE→双击并修改其键值 可以删除 ...
- gradle多模块开发(转)
参考文档:gradle的官方userguide.pdf文档的chapter 55和chapter 56.gradle的多模块或项目开发一定不会比maven差,在我看来!大的项目分成多个模块来开发是常事 ...
- 通用型CRM还是行业型CRM?-定制为王
大数据时代,怎样利用工具摆脱繁杂的数据管理之苦,洞察有价值的销售信息,是每一个管理者的迫切须要.Zoho CRM问世10年来,见证了一个个行业客户怎样在CRM帮助下实现了效率和业绩提升.相同,广泛的 ...
- Boost中timer的简易用法
boost::asio::deadline_timer timer_; timer_(io_service), timer_.expires_from_now(boost::posix_time::s ...
- VMware Player安装centos
用VMware Player安装centos 到物理硬盘 想要學Linux,但卻不知道如何弄成雙系統?那就使用用虛擬機器來安裝Linux.使用虛體機器來玩,好處除了好裝之外,也不怕把電腦搞壞,不論你怎 ...
- 假设拦截WebView的错误和OS升级到4.4后链接不能点击的问题
android OS升级到4.4之后,有些WebView的链接我们点击无效了,以下能够解决当中的某一种情况: webviewClient的shouldOverrideUrlLoading方法必须返回f ...
- veridata实验举例(6)验证agent启动先后顺序是否对捕获update操作有影响
veridata实验举例(6)验证agent启动先后顺序是否对捕获update操作有影响 续接veridata实验系列 上篇:"veridata实验举例(5)改动主键上的列值.update ...