forked from kothariji/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path429 - Word Transformation.cpp
123 lines (115 loc) · 2.26 KB
/
429 - Word Transformation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include<bits/stdc++.h>
using namespace std;
map<string,vector<string>>word;
vector<string>sz[222];
map<string,bool>visited;
map<string,int>lvl;
bool match(string s,string ss,int ln)
{
int cnt=0;
for(int i=0;i<ln;i++)
{
if(s[i]!=ss[i])cnt++;
if(cnt>1)break;
}
if(cnt==1)return 1;
else return 0;
}
void node_s(string s,int ln)
{
for(string n:sz[ln])
{
if(match(s,n,ln))
{
word[s].push_back(n);
word[n].push_back(s);
}
}
}
void print()
{
for(auto i=word.begin();i!=word.end();i++)
{
cout<<i->first<<" : ";
vector<string>v=i->second;
for(string s:v)cout<<s<<' ';
cout<<endl;
}
}
void clr()
{
//visited.clear();
word.clear();
for(int i=0;i<220;i++)sz[i].clear();
}
int bfs(string s,string d)
{
queue<string>q;
vector<string>trv;
string node;
q.push(s);
lvl[s]=0;
while(!q.empty())
{
node=q.front();
q.pop();
visited[node]=1;
trv=word[node];
for(string nd:trv)
{
if(visited[nd]==0)
{
q.push(nd);
lvl[nd]=lvl[node]+1;
// cout<<"root= "<<node<<" rlvl= "<<lvl[node]<<" child= "<<nd<<" clvl= "<<lvl[nd]<<endl;
if(nd==d)
{
return lvl[nd];
}
}
}
}
return 0;
}
int main()
{
int t,ln;
string s,d,q;
cin>>t;
while(t--)
{
clr();
while(cin>>s && s != "*")
{
ln=s.size();
node_s(s,ln);
sz[ln].push_back(s);
}
//print();
getchar();
while(getline(cin,q))
{
ln=q.size();
if(ln==0)break;
s.clear();
d.clear();
int l;
for(l=0;l<ln;l++)
{
if(q[l]==' ')break;
s+=q[l];
}
for(l=l+1;l<ln;l++)
{
//if(q[l]==' ')break;
d+=q[l];
}
//cout<<s<<' '<<d<<endl;
visited.clear();
cout<<s<<' '<<d<<' '<<bfs(s,d)<<endl;
}
if(t>0)
cout<<"\n";
}
return 0;
}