Advertisement
tepyotin2

Word Ladder

May 5th, 2025
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.08 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. struct Step{
  6.     string word;
  7.     int count;
  8. };
  9.  
  10. string a, b;
  11. int n;
  12. unordered_set<string> dict;
  13. queue<Step> q;
  14. unordered_map<string, bool> visited;
  15.  
  16. int main(){
  17.     //freopen("wordladder.in", "r", stdin);
  18.    
  19.     cin >> a >> b >> n;
  20.     for(int i=0; i<n; i++){
  21.         string s;
  22.         cin >> s;
  23.         dict.insert(s);
  24.     }
  25.     q.push({a, 0});
  26.     visited[a] = true;
  27.     while(!q.empty()){
  28.         Step v = q.front();
  29.         q.pop();
  30.         if(v.word == b){
  31.             cout << v.count << '\n';
  32.             return 0;
  33.         }
  34.         for(int i=0; i<26; i++){
  35.             string temp = v.word;
  36.             for(int j=0; j<v.word.size(); j++){
  37.                 temp = v.word;
  38.                 temp[j] = i+'a';
  39.                 //cout << temp << '\n';
  40.                 if(dict.find(temp) != dict.end() && visited[temp] == false){
  41.                     //cout << "HI" << '\n';
  42.                     visited[temp] = true;
  43.                     q.push({temp, v.count+1});
  44.                 }
  45.             }
  46.             //cout << "============" << '\n';
  47.             temp = v.word;
  48.             char ap = i+'a';
  49.             temp = ap+temp;
  50.             if(dict.find(temp) != dict.end() && visited[temp] == false){
  51.                 //cout << "HI" << '\n';
  52.                 visited[temp] = true;
  53.                 q.push({temp, v.count+1});
  54.             }
  55.             for(int j=0; j<v.word.size(); j++){
  56.                 temp = v.word;
  57.                 char ap = i+'a';
  58.                 string sub1 = temp.substr(0, j+1);
  59.                 string sub2 = temp.substr(j+1, v.word.size()+1);
  60.                 temp = sub1+ap+sub2;
  61.                 //cout << temp << '\n';
  62.                 //temp.insert(j, ap);
  63.                 if(dict.find(temp) != dict.end() && visited[temp] == false){
  64.                     //cout << "HI" << '\n';
  65.                     visited[temp] = true;
  66.                     q.push({temp, v.count+1});
  67.                 }
  68.             }
  69.             temp = v.word;
  70.             ap = i+'a';
  71.             temp+=ap;
  72.             if(dict.find(temp) != dict.end() && visited[temp] == false){
  73.                 //cout << "HI" << '\n';
  74.                 visited[temp] = true;
  75.                 q.push({temp, v.count+1});
  76.             }
  77.         }
  78.         for(int i=0; i<v.word.size(); i++){
  79.             string sub1 = v.word.substr(0, i);
  80.             string sub2 = v.word.substr(i+1, v.word.size()+1);
  81.             string temp = sub1+sub2;
  82.             if(dict.find(temp) != dict.end() && visited[temp] == false){
  83.                 //cout << "HI" << '\n';
  84.                 visited[temp] = true;
  85.                 q.push({temp, v.count+1});
  86.             }
  87.         }
  88.     }
  89.     cout << "-1" << '\n';
  90.    
  91.     return 0;
  92. }
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement