Advertisement
Pabon_SEC

Word Transformation

May 26th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.33 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. map<string,int>mp;
  6.  
  7. string str[1003];
  8.  
  9. vector<int>graph[1003];
  10.  
  11. bool color[1003];
  12.  
  13. int cost[1003];
  14.  
  15. void bfs(int s)
  16. {
  17.     queue<int>Q;
  18.  
  19.     color[s] = true;
  20.  
  21.     Q.push(s);
  22.  
  23.     cost[s] = 0;
  24.  
  25.     while(!Q.empty())
  26.     {
  27.         int u = Q.front();
  28.  
  29.         Q.pop();
  30.  
  31.         int len = graph[u].size();
  32.  
  33.         for(int i=0; i<len; i++)
  34.         {
  35.             if(!color[graph[u][i]])
  36.             {
  37.                 color[graph[u][i]] = true;
  38.  
  39.                 cost[graph[u][i]] = cost[u]+1;
  40.  
  41.                 Q.push(graph[u][i]);
  42.             }
  43.         }
  44.     }
  45. }
  46.  
  47. int main()
  48. {
  49.     ios_base::sync_with_stdio(false);
  50.  
  51.     cin.tie(0);
  52.  
  53.     int tc,test,i,j,k,index=0,milenai,len,len1;
  54.  
  55.     cin>>test;
  56.  
  57.     for(tc=1;tc<=test;tc++)
  58.     {
  59.         while(getline(cin,str[++index]))
  60.         {
  61.             if(str[index][0]=='*')
  62.             {
  63.                 break;
  64.             }
  65.  
  66.             mp[str[index]] = index;
  67.         }
  68.  
  69.         for(i=1; i<index; i++)
  70.         {
  71.             for(j=i+1; j<index; j++)
  72.             {
  73.                 len = str[i].length();
  74.  
  75.                 len1 = str[j].length();
  76.  
  77.                 milenai = 0;
  78.  
  79.                 if(len==len1)
  80.                 {
  81.                     for(k=0; k<len; k++)
  82.                     {
  83.                         if(str[i][k]!=str[j][k])
  84.                         {
  85.                             milenai++;
  86.                         }
  87.                     }
  88.  
  89.                     if(milenai==1)
  90.                     {
  91.                         graph[i].push_back(j);
  92.  
  93.                         graph[j].push_back(i);
  94.                     }
  95.                 }
  96.             }
  97.         }
  98.  
  99.         if(tc!=1)
  100.         {
  101.             cout<<endl;
  102.         }
  103.  
  104.         string input,s,d;
  105.  
  106.         stringstream ss;
  107.  
  108.         while(getline(cin,input))
  109.         {
  110.             if(input.length()==0)
  111.             {
  112.                 break;
  113.             }
  114.  
  115.             ss<<input;
  116.  
  117.             ss>>s;
  118.  
  119.             ss>>d;
  120.  
  121.             bfs(mp[s]);
  122.  
  123.             cout<<s<<" "<<d<<" "<<cost[mp[d]]<<endl;
  124.  
  125.             memset(color,false,sizeof color);
  126.  
  127.             ss.clear();
  128.         }
  129.  
  130.         for(i=0; i<=index; i++)
  131.         {
  132.             graph[i].clear();
  133.         }
  134.  
  135.         mp.clear();
  136.     }
  137.  
  138.     return 0;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement