Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- map<string,int>mp;
- string str[1003];
- vector<int>graph[1003];
- bool color[1003];
- int cost[1003];
- void bfs(int s)
- {
- queue<int>Q;
- color[s] = true;
- Q.push(s);
- cost[s] = 0;
- while(!Q.empty())
- {
- int u = Q.front();
- Q.pop();
- int len = graph[u].size();
- for(int i=0; i<len; i++)
- {
- if(!color[graph[u][i]])
- {
- color[graph[u][i]] = true;
- cost[graph[u][i]] = cost[u]+1;
- Q.push(graph[u][i]);
- }
- }
- }
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(0);
- int tc,test,i,j,k,index=0,milenai,len,len1;
- cin>>test;
- for(tc=1;tc<=test;tc++)
- {
- while(getline(cin,str[++index]))
- {
- if(str[index][0]=='*')
- {
- break;
- }
- mp[str[index]] = index;
- }
- for(i=1; i<index; i++)
- {
- for(j=i+1; j<index; j++)
- {
- len = str[i].length();
- len1 = str[j].length();
- milenai = 0;
- if(len==len1)
- {
- for(k=0; k<len; k++)
- {
- if(str[i][k]!=str[j][k])
- {
- milenai++;
- }
- }
- if(milenai==1)
- {
- graph[i].push_back(j);
- graph[j].push_back(i);
- }
- }
- }
- }
- if(tc!=1)
- {
- cout<<endl;
- }
- string input,s,d;
- stringstream ss;
- while(getline(cin,input))
- {
- if(input.length()==0)
- {
- break;
- }
- ss<<input;
- ss>>s;
- ss>>d;
- bfs(mp[s]);
- cout<<s<<" "<<d<<" "<<cost[mp[d]]<<endl;
- memset(color,false,sizeof color);
- ss.clear();
- }
- for(i=0; i<=index; i++)
- {
- graph[i].clear();
- }
- mp.clear();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement