-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
149 lines (117 loc) · 1.94 KB
/
main.c
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
#include <ctype.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
bool exists;
char from[4];
int f;
char to[4];
int t;
int day;
int price;
} Flight;
static int ncities = 0;
static char cities[300][4];
static Flight days[300][300][300];
static int16_t citymagic[26][26][26];
int
find(char *city) {
int16_t mag = citymagic[city[0] - 'A'][city[1] - 'A'][city[2] - 'A'];
if (mag != 0)
return mag - 1;
strcpy(cities[ncities++], city);
citymagic[city[0] - 'A'][city[1] - 'A'][city[2] - 'A'] = ncities;
return ncities - 1;
}
void
add_flight(Flight f) {
f.exists = true;
int from = find(f.from);
int to = find(f.to);
f.f = from;
f.t = to;
days[f.day][from][to] = f;
}
void
parse() {
int c;
int q = 0;
char start[4] = {0, 0, 0, 0};
char *p = start;
Flight f;
while ((c = getchar()) != EOF) {
switch (q) {
case 0: // loading start
if (isspace(c)) {
*p = 0;
q = 5;
find(start);
continue;
}
*p = c;
p++;
continue;
case 5:
f = (Flight) {
.exists = false,
.from = {0, 0, 0, 0},
.to = {0, 0, 0, 0},
.f = 0,
.t = 0,
.day = 0,
.price = 0,
};
p = f.from;
q = 1;
// pass
case 1: // loading from
if (isspace(c)) {
*p = 0;
q = 2;
p = f.to;
continue;
}
*p = c;
p++;
continue;
case 2: // loading to
if (isspace(c)) {
*p = 0;
q = 3;
continue;
}
*p = c;
p++;
continue;
case 3: // loading day
if (isspace(c)) {
q = 4;
continue;
}
f.day *= 10;
f.day += c - '0';
continue;
case 4: // loading price
if (isspace(c)) {
q = 5;
add_flight(f);
continue;
}
f.price *= 10;
f.price += c - '0';
continue;
}
if (q == 4)
add_flight(f);
}
}
int
main(int argc, char **argv)
{
parse();
printf("%s\n%d\n", cities[0], ncities);
return 0;
}