-
Notifications
You must be signed in to change notification settings - Fork 2
/
Q10150.cpp
122 lines (104 loc) · 2.66 KB
/
Q10150.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
#include<algorithm>
struct node{
char s[32];
int len,front;
bool operator <(node t) const{
return len<t.len;
}
}dict[25200];
struct edge{
int a,b;
bool operator <(edge t) const{
return a<t.a;
}
}e[5000001];
int abs(int a){
return a>?-a;
}
int q[25200],d[25200],maxl,r[25200];
bool used[25200];
int find(int a,int b){
int i,ans=0;
for(i=0;i<dict[a].len;i++)
ans+=dict[a].s[i]!=dict[b].s[i];
return ans;
}
void print(int now){
if(dict[now].front==-1){
puts(dict[now].s);
return;
}
print(dict[now].front);
puts(dict[now].s);
}
main(){
int n=0,i,j,k,c=0,m=0,a,b,la,lb;
char st[101],ed[101];
while(gets(dict[n].s) && dict[n].s[0]!='\n' && dict[n].s[0]!='\0'){
dict[n].len=strlen(dict[n].s);
n++;
}
std::sort(dict,dict+n);
for(i=maxl=0;i<n;i++){
d[dict[i].len]++;
maxl>?=dict[i].len;
}
for(i=1;i<=maxl;i++)
d[i]+=d[i-1];
puts("");
for(i=0;i<n;i++)
puts(dict[i].s);
puts("");
for(i=0;i<n;i++)
for(j=d[dict[i].len-1];j<d[dict[i].len];j++)
if(find(i,j)==1){
e[m++]=(edge){i,j};
printf("%d %d\n",i,j);
}
std::sort(e,e+m);
for(i=0;i<=m;i++)
r[e[i].a+1]++;
for(i=1;i<=n;i++)
printf("%d\n",r[i]);
for(i=1;i<=n;i++)
r[i]+=r[i-1];
for(i=1;i<=n;i++)
printf("%d ",r[i]);
puts("");
while(scanf("%s %s",st,ed)!=EOF){
if(c) puts("");
else c=1;
if((la=strlen(st))!=strlen(ed)){
puts("No solution.");
continue;
}
memset(used,0,sizeof(used));
for(i=d[la-1];i<d[la];i++)
if(strcmp(dict[i].s,st)==0)
break;
for(j=d[la-1];j<d[la];j++)
if(strcmp(dict[j].s,ed)==0)
break;
if(i>=d[la] || j>=d[la]){
puts("No solution.");
continue;
}
a=i,b=j;
dict[a].front=dict[b].front=-1;
q[0]=a,used[a]=1;
i=0,j=1;
while(i<j){
if(q[i]==b) break;
for(k=r[e[q[i]].a];k<r[e[q[i]].a+1];k++)
if(used[e[k].b]==0){
used[e[k].b]=1;
q[j++]=e[k].b;
dict[e[k].b].front=q[i];
}
if(dict[b].front!=-1) break;
i++;
}
if(i>=j) puts("No solution.");
else print(b);
}
}