#include<bits/stdc++.h>
#define fr(i1,m) for(int i1=0;i1<m;i1++)
using namespace std;
vector<int>vc[10000];
int bfs(int n,int m)
{
queue<int>q;
q.push(n);
int t[100000]= {0},d[100000]= {0};
t[n]=1;
d[n]=0;
while(!q.empty())
{
int u=q.front();
if(u==m)
return d[u];
for(int i=0; i<vc[u].size(); i++)
{
int v=vc[u][i];
if(!t[v])
{
d[v]=d[u]+1;
t[v]=1;
q.push(v);
}
}
q.pop();
}
}
int main()
{
long n;
scanf("%ld",&n);
fr(ii,n)
{
if(ii>0)
cout<<"\n";
long p=0,l=1,hh,j,c;
char ch[100000];
string s[10000],s1,ff,f,kk,rr;
map<string,int>mp;
while(cin>>s1)
{
if(s1=="*")
break;
s[p]=s1;
mp[s1]=l++;
p++;
}
fr(i,p)
{
for(j=i+1; j<p; j++)
{
if(s[i].size()==s[j].size())
{
hh=0;
kk=s[j];
rr=s[i];
fr(k,s[i].size())
{
if(rr[k]!=kk[k])
{
hh++;
if(hh>1)
break;
}
if(hh>1)
break;
}
if(hh==1)
{
vc[mp[rr]].push_back(mp[kk]);
vc[mp[kk]].push_back(mp[rr]);
}
}
}
}
getchar();
// scanf("\n");
while(gets(ch))
{
if(ch[0]=='\0')
break;
f="";
ff="";
c=0;
fr(i,strlen(ch))
{
if(ch[i]==' ')
c=1;
else if(c==0)
f+=ch[i];
else
ff+=ch[i];
}
cout<<f<<" "<<ff<<" "<<bfs(mp[f],mp[ff])<<"\n";
}
fr(i,l+4)
vc[i].clear();
}
return 0;
}
#define fr(i1,m) for(int i1=0;i1<m;i1++)
using namespace std;
vector<int>vc[10000];
int bfs(int n,int m)
{
queue<int>q;
q.push(n);
int t[100000]= {0},d[100000]= {0};
t[n]=1;
d[n]=0;
while(!q.empty())
{
int u=q.front();
if(u==m)
return d[u];
for(int i=0; i<vc[u].size(); i++)
{
int v=vc[u][i];
if(!t[v])
{
d[v]=d[u]+1;
t[v]=1;
q.push(v);
}
}
q.pop();
}
}
int main()
{
long n;
scanf("%ld",&n);
fr(ii,n)
{
if(ii>0)
cout<<"\n";
long p=0,l=1,hh,j,c;
char ch[100000];
string s[10000],s1,ff,f,kk,rr;
map<string,int>mp;
while(cin>>s1)
{
if(s1=="*")
break;
s[p]=s1;
mp[s1]=l++;
p++;
}
fr(i,p)
{
for(j=i+1; j<p; j++)
{
if(s[i].size()==s[j].size())
{
hh=0;
kk=s[j];
rr=s[i];
fr(k,s[i].size())
{
if(rr[k]!=kk[k])
{
hh++;
if(hh>1)
break;
}
if(hh>1)
break;
}
if(hh==1)
{
vc[mp[rr]].push_back(mp[kk]);
vc[mp[kk]].push_back(mp[rr]);
}
}
}
}
getchar();
// scanf("\n");
while(gets(ch))
{
if(ch[0]=='\0')
break;
f="";
ff="";
c=0;
fr(i,strlen(ch))
{
if(ch[i]==' ')
c=1;
else if(c==0)
f+=ch[i];
else
ff+=ch[i];
}
cout<<f<<" "<<ff<<" "<<bfs(mp[f],mp[ff])<<"\n";
}
fr(i,l+4)
vc[i].clear();
}
return 0;
}
কোন মন্তব্য নেই:
একটি মন্তব্য পোস্ট করুন