C++ 词语阶梯

已知两个单词(分别是起始单词与结束单词),一个单词词典,根据转换规则计算从起始单词到结束单词的最短转换步数。
转换规则如下:

  1. 在转换时,只能转换单词中的1个字符。
  2. 转换得到的新单词,必须在单词词典中。
    例如:beginWord=“hit”; endWord=“cog”; wordList=[“hot”,“dot”,“dog”,“lot”,“log”,“cog”]
    最短转换方式:“hit”->“hot”->“dot”->“dog”->“cog”,结果为5。
#include<string>
#include<vector>
#include<map>
#include<queue>
#include<set>
bool connect(std::string& word1, std::string& word2)
{
   
 int cnt = 0;
 for (int i = 0; i < word1.length(); i++)
 {
   
  if (word1[i]!=word2[i])
  {
   
   cnt++;
  }
 }
 return cnt == 1;
}
void construct_graph(std::string& beginWord, std::vector<std::string>& wordList, std::map<std::string, std::vector<std::string>>& graph)
{
   
 wordList
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值