-
Notifications
You must be signed in to change notification settings - Fork 2
/
10150.cpp
181 lines (158 loc) · 2.67 KB
/
10150.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#define MAXN 25149
int N, qh, qt, rec;
char F[MAXN+2], St[MAXN+2][17];
int Rec[MAXN+10], P[MAXN+2];
int Q[MAXN+4];
struct link {
int D[100];
int ind;
}list[MAXN+2];
struct ss {
char str[17];
int ind;
}A[MAXN],dum, aa, bb;
int com(const void *a, const void *b) {
ss *x, *y;
x = (ss *)a;
y = (ss *)b;
return strcmp(x->str,y->str);
}
void Push(int n) {
Q[qh++] = n;
Rec[rec++] = n;
qh %= MAXN;
}
int Pop() {
int n;
n = Q[qt++];
qt %= MAXN;
return n;
}
void Print(int n) {
if(P[n] == -1) {
printf("%s\n",St[n]);
return;
}
Print(P[n]);
printf("%s\n",St[n]);
}
int BFSVisit( int st ,int des) {
int i, k, lim, p;
qh = qt = rec = 0;
P[st] = -1;
F[st] = 1;
Push(st);
while(qh != qt) {
k = Pop();
lim = list[k].ind;
for(i = 0; i<lim; i++) {
p = list[k].D[i];
if(p == des) {
P[p] = k;
Print(p);
return 1;
}
if(F[p]) continue;
P[p] = k;
F[p] = 1;
Push(p);
}
}
return 0;
}
void Sp(int st) {
int i, d, f = 0;
printf("%s\n",St[st]);
for(i = 0; i<list[st].ind && i<1; i++){
d = list[st].D[i];
printf("%s\n",St[d]);
f = 1;
}
if(f)
printf("%s\n",St[st]);
}
void BFS() {
int u, f = 0;
int i;
char str[100];
ss *c, *v;
while(gets(str)) {
for(i = 0; str[i]; i++) {
if(str[i] == '\n')
str[i] = NULL;
if(strlen(str) == 0) break;
}
if(f++) printf("\n");
sscanf(str,"%s%s",aa.str,bb.str);
if(strlen(aa.str) != strlen(bb.str)) {
printf("No solution.\n");
continue;
}
c = (ss *) bsearch(&aa,A,N,sizeof(A[0]),com);
v = (ss *) bsearch(&bb,A,N,sizeof(A[0]),com);
if(!c || !v) {
printf("No solution.\n");
continue;
}
if(!strcmp(aa.str,bb.str)) {
Sp(c->ind);
continue;
}
for(i = 0; i<rec; i++) {
u = Rec[i];
F[u] = 0;
}
if(!BFSVisit(c->ind,v->ind)){
printf("No solution.\n");
}
}
}
void Link() {
int i, p, k , mx = 0;
int s, t, d;
ss *c;
for(k = 0; k< N; k++){
d = A[k].ind;
for(i = 0; A[k].str[i]; i++){
strcpy(dum.str,A[k].str);
for(p = A[k].str[i]+1; p<97+26; p++) {
if(p != A[k].str[i]){
dum.str[i] = p;
c = (ss *) bsearch(&dum,A,N,sizeof(A[0]),com);
if(c){
s = list[d].ind;
t = list[c->ind].ind;
list[d].D[s] = c->ind;
list[c->ind].D[t] = d;
list[d].ind++;
list[c->ind].ind++;
}
}
}
}
}
}
void ReadCase() {
int i;
char temp[20];
N = 0;
while(gets(temp)) {
for(i = 0; temp[i]; i++)
if(temp[i] == '\n')
temp[i] = NULL;
if(strlen(temp) == 0) break;
strcpy(A[N++].str ,temp);
strcpy(St[N-1],temp);
A[N-1].ind = N-1;
}
}
int main() {
ReadCase();
qsort(A,N,sizeof(A[0]),com);
Link();
BFS();
return 0;
}