//\\__ hr1212 __//\\
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef map<int,int> mi;
#define si(a) scanf("%d",&a)
#define sii(a,b) scanf("%d %d",&a,&b)
#define siii(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define pi(a) printf("%d\n",a)
#define nl printf("\n");
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define f(i,a,b) for(i=a;i<b;i++)
#define rf(i,a,b) for(i=a;i>=b;i--)
#define clr(x,a) memset(x,a,sizeof(x))
#define MAX 100010
#define MOD 1000000007
int n,m,parent[MAX],mk[MAX];
vector<string> w;
map<string,int> mm;
vi v[MAX];
int main(){
int r,k,i,c=0,x=0,y=0,j,t,l,z,x1=0,y1=0;
ll ans=0;string p,s;
k=0;
while(getline(cin,p) && p.size()){
if(mm.find(p)==mm.end()){
mm[p]=k++;
w.pb(p);
}
}
f(i,0,w.size()){
f(j,0,w.size()){
c=0;
if(w[i].size()!=w[j].size())
continue;
f(l,0,w[i].size()){
if(w[i][l]!=w[j][l])
c++;
}
if(c==1)
v[i].pb(j);
}
}
t=0;
while(cin>>p>>s){
if(t++)
nl;
if(mm.find(p)==mm.end() || mm.find(s)==mm.end()){
printf("No solution.\n");
continue;
}
x=mm[p];y=mm[s];
queue<pii > q;
q.push(mp(x,0));
clr(parent,-1);
clr(mk,0);
z=-1;
mk[x]=1;
while(!q.empty()){
pii pp=q.front();
q.pop();
x1=pp.first;y1=pp.second;
if(x1==y){
z=y1;
break;
}
f(i,0,v[x1].size()){
if(!mk[v[x1][i]]){
mk[v[x1][i]]=1;
parent[v[x1][i]]=x1;
q.push(mp(v[x1][i],y1+1));
}
}
}
if(z==-1)
printf("No solution.\n");
else{
vi u;
while(parent[y]!=-1){
u.pb(y);
y=parent[y];
}
u.pb(x);
reverse(all(u));
f(i,0,u.size()){
cout<<w[u[i]]<<endl;
}
}
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef map<int,int> mi;
#define si(a) scanf("%d",&a)
#define sii(a,b) scanf("%d %d",&a,&b)
#define siii(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define pi(a) printf("%d\n",a)
#define nl printf("\n");
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define f(i,a,b) for(i=a;i<b;i++)
#define rf(i,a,b) for(i=a;i>=b;i--)
#define clr(x,a) memset(x,a,sizeof(x))
#define MAX 100010
#define MOD 1000000007
int n,m,parent[MAX],mk[MAX];
vector<string> w;
map<string,int> mm;
vi v[MAX];
int main(){
int r,k,i,c=0,x=0,y=0,j,t,l,z,x1=0,y1=0;
ll ans=0;string p,s;
k=0;
while(getline(cin,p) && p.size()){
if(mm.find(p)==mm.end()){
mm[p]=k++;
w.pb(p);
}
}
f(i,0,w.size()){
f(j,0,w.size()){
c=0;
if(w[i].size()!=w[j].size())
continue;
f(l,0,w[i].size()){
if(w[i][l]!=w[j][l])
c++;
}
if(c==1)
v[i].pb(j);
}
}
t=0;
while(cin>>p>>s){
if(t++)
nl;
if(mm.find(p)==mm.end() || mm.find(s)==mm.end()){
printf("No solution.\n");
continue;
}
x=mm[p];y=mm[s];
queue<pii > q;
q.push(mp(x,0));
clr(parent,-1);
clr(mk,0);
z=-1;
mk[x]=1;
while(!q.empty()){
pii pp=q.front();
q.pop();
x1=pp.first;y1=pp.second;
if(x1==y){
z=y1;
break;
}
f(i,0,v[x1].size()){
if(!mk[v[x1][i]]){
mk[v[x1][i]]=1;
parent[v[x1][i]]=x1;
q.push(mp(v[x1][i],y1+1));
}
}
}
if(z==-1)
printf("No solution.\n");
else{
vi u;
while(parent[y]!=-1){
u.pb(y);
y=parent[y];
}
u.pb(x);
reverse(all(u));
f(i,0,u.size()){
cout<<w[u[i]]<<endl;
}
}
}
return 0;
}
No comments:
Post a Comment