[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 ...
随机推荐
- 利用linux BT5来破解无线 破解无线
下面是自己整理的详细步骤,版权小冯全部. 一.提前准备好bt5的ISO镜像文件.和虚拟机,提前把虚拟机安装好.然后进行安装bt5. 二.进入页面,点击statx.进入可视化界面. 三.进入主界面后.下 ...
- VSTO 学习笔记(六)在 Excel 2010中使用RDLC报表
原文:VSTO 学习笔记(六)在 Excel 2010中使用RDLC报表 Excel具有强大的图表显示.分析功能,这点毋庸置疑,但是如果将常规MIS系统中的数据以报表的形式在Excel中显示,却并不那 ...
- URAL1523(dp+树状数组)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=41224#problem/B 分析:可以设dp[i][j]表示以i结尾长度为j的 ...
- hdu1507--二分图最大匹配
题意:你大爷.哦不! 你大叔继承了一块地什么的都是废话..,这里说说题意,和怎么建图. 题意:这里有一块N*M的地,可是有 K 个地方.是池塘,然后输入K行(x,y),OK,如今能够出售的地必须是 1 ...
- SQLServer2012 分页语句执行分析
上一篇文章提到了,SQLServer2012在使用Offset,Fetch语句分页时,获取了大量不需要的数据,导致查询效率低的问题. 现在让我们来看看,究竟是什么导致SQLServer不能按需取数呢? ...
- [ACM] HDU 2063 过山车 (二分图,匈牙利算法)
过山车 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- mysql xtrabackup增量备份
mysql 增量备份策略 周一全备,其他增量备份,根据业务需要,设定保留日期,如保留一月. 增量备份步骤; 1 创建全备 2 根据全备目录,创建增量备份 3 第二次增量备份根据第一次增量备份目录,依次 ...
- HDU 4998 Rotate
题意: n次旋转 每次平面绕ai点旋转pi弧度 问 最后状态相当于初始状态绕A点旋转P弧度 A和P是多少 思路: 如果初始X点的最后状态为X'点 则圆心一定在X和X'连线的垂直平分线上 那 ...
- SpringMVC(转)
http://www.cnblogs.com/liukemng/p/3725582.html
- Codeforces Round #198 (Div. 2) C. Tourist Problem (数学+dp)
C. Tourist Problem time limit per test 1 second memory limit per test 256 megabytes input standard i ...