-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathA1032.cpp
46 lines (41 loc) · 871 Bytes
/
A1032.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
#include <cstdio>
#define MAXN 100010
struct {
char data;
int next;
bool flag;
} nodes[MAXN];
int main()
{
// init
for (int i = 0; i < MAXN; i++)
nodes[i].flag = false;
// input
int head1, head2, n;
scanf("%d %d %d", &head1, &head2, &n);
for (int i = 0; i < n; i++)
{
int addr, next;
char data;
scanf("%d %c %d", &addr, &data, &next);
nodes[addr].data = data;
nodes[addr].next = next;
}
// traversal of the first word
while (head1 != -1)
{
nodes[head1].flag = true;
head1 = nodes[head1].next;
}
// traversal of the second word
while (head2 != -1)
{
if (nodes[head2].flag) break;
head2 = nodes[head2].next;
}
if (head2 == -1)
printf("-1");
else
printf("%05d", head2);
return 0;
}