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