leetcode 127. Word Ladder

曾经终败给现在 2022-07-26 01:41 262阅读 0赞

Given two words (beginWord and endWord), and a dictionary’s word list, find the length of shortest transformation sequence from beginWordto endWord, such that:

  1. Only one letter can be changed at a time
  2. Each intermediate word must exist in the word list

For example,

Given:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]

As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length 5.

Note:

  • Return 0 if there is no such transformation sequence.
  • All words have the same length.
  • All words contain only lowercase alphabetic characters.
  1. class Solution {
  2. public:
  3. int ladderLength(string beginWord, string endWord, unordered_set<string>& wordList) {
  4. //vector<vector<string>>re;
  5. if (beginWord == endWord || beginWord.length() == 1)
  6. return 2;
  7. int wl = beginWord.length();
  8. for (int i = 0; i < wl; i++)
  9. {
  10. string s1(beginWord), s2(endWord);
  11. s1.erase(i, 1); s2.erase(i, 1);
  12. if (s1 == s2)
  13. return 2;
  14. }
  15. vector<string>words(wordList.begin(), wordList.end());
  16. map<pair<string,int>, vector<int>>changelist;
  17. for (int i = 0; i < words.size(); i++)
  18. {
  19. for (int j = 0; j < words[0].length(); j++)
  20. {
  21. string s = words[i];
  22. s.erase(j, 1);
  23. changelist[pair<string, int>(s,j)].push_back(i);
  24. }
  25. }
  26. vector<int>a, b;
  27. for (int i = 0; i < words.size(); i++)
  28. a.push_back(i);
  29. map<string, vector<vector<int>>>head, tail;
  30. head[beginWord].push_back(b);
  31. tail[endWord].push_back(b);
  32. bool flag = true;
  33. int k = 0;
  34. while (k < words.size() && flag)
  35. {
  36. k++;
  37. if (tail.size()>head.size())
  38. {
  39. map<string, vector<vector<int>>>newhead;
  40. for (map<string, vector<vector<int>>>::iterator ii = head.begin(); ii != head.end(); ii++)
  41. {
  42. for (int j = 0; j < ii->second.size(); j++)
  43. {
  44. for (int h = 0; h < wl; h++)
  45. {
  46. string s = ii->first;
  47. s.erase(h, 1);
  48. for (int f = 0; f < changelist[pair<string, int>(s,h)].size(); f++)
  49. {
  50. if (words[changelist[pair<string, int>(s,h)][f]] != ii->first)
  51. {
  52. vector<int>::iterator ite = find(ii->second[j].begin(),
  53. ii->second[j].end(), changelist[pair<string, int>(s,h)][f]);
  54. if (ite == ii->second[j].end())
  55. {
  56. map<string, vector<vector<int>>>::iterator nn =
  57. tail.find(words[changelist[pair<string, int>(s,h)][f]]);
  58. if (nn != tail.end())
  59. {
  60. for (int g = 0; g < nn->second.size(); g++)
  61. {
  62. set<int>aa;
  63. set<int>a1(ii->second[j].begin(), ii->second[j].end());
  64. set<int>a2(nn->second[g].begin(), nn->second[g].end());
  65. set_intersection(a1.begin(), a1.end(),
  66. a2.begin(), a2.end(), inserter(aa, aa.begin()));
  67. if (aa.empty() && (a1.size() > 0 || a2.size() > 0))
  68. return k + 1;
  69. }
  70. }
  71. if (flag)
  72. {
  73. vector<int>xx = ii->second[j];
  74. xx.push_back(changelist[pair<string, int>(s,h)][f]);
  75. newhead[words[changelist[pair<string, int>(s,h)][f]]].push_back(xx);
  76. }
  77. }
  78. }
  79. }
  80. }
  81. }
  82. }
  83. //f = !f;
  84. head = newhead;
  85. }
  86. else
  87. {
  88. map<string, vector<vector<int>>>newtail;
  89. for (map<string, vector<vector<int>>>::iterator ii = tail.begin(); ii != tail.end(); ii++)
  90. {
  91. for (int j = 0; j < ii->second.size(); j++)
  92. {
  93. for (int h = 0; h < wl; h++)
  94. {
  95. string s = ii->first;
  96. s.erase(h, 1);
  97. for (int f = 0; f < changelist[pair<string, int>(s,h)].size(); f++)
  98. {
  99. if (words[changelist[pair<string, int>(s,h)][f]] != ii->first)
  100. {
  101. vector<int>::iterator ite = find(ii->second[j].begin(), ii->second[j].end(),
  102. changelist[pair<string, int>(s,h)][f]);
  103. if (ite == ii->second[j].end())
  104. {
  105. map<string, vector<vector<int>>>::iterator nn = head.find(words[changelist[pair<string, int>(s, h)][f]]);
  106. if (nn != head.end())
  107. {
  108. for (int g = 0; g < nn->second.size(); g++)
  109. {
  110. set<int>aa;
  111. set<int>a1(ii->second[j].begin(), ii->second[j].end());
  112. set<int>a2(nn->second[g].begin(), nn->second[g].end());
  113. set_intersection(a1.begin(), a1.end(),
  114. a2.begin(), a2.end(), inserter(aa, aa.begin()));
  115. if (aa.empty() && (a1.size()>0 || a2.size()>0))
  116. return k + 1;
  117. }
  118. }
  119. if (flag)
  120. {
  121. vector<int>xx = ii->second[j];
  122. xx.push_back(changelist[pair<string, int>(s, h)][f]);
  123. newtail[words[changelist[pair<string, int>(s, h)][f]]].push_back(xx);
  124. }
  125. }
  126. }
  127. }
  128. }
  129. }
  130. }
  131. //f = !f;
  132. tail = newtail;
  133. }
  134. }
  135. return 0;
  136. }
  137. };

accepted

发表评论

表情:
评论列表 (有 0 条评论,262人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Word Ladder--LeetCode

    这道题看似一个关于字符串操作的题目,其实要解决这个问题得用图的方法。我们先给题目进行图的映射,顶点则是每个字符串,然后两个字符串如果相差一个字符则我们进行连边。接下来看看这个方