This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <stdio.h>
#include <string.h>
#define N 60
#define L 60
#define N_ (N * L + 2)
#define INF 0x3f3f3f3f
int min(int a, int b) { return a < b ? a : b; }
int main() {
static char cc[L + 1];
static int ll[N], tt[N_][2], ii[N_], dd[N_ * N_], qu[2][N_ * N_], hh[2], cnt[2];
int n, n_, h, i, s, s_, t, t_, st, d, d_;
scanf("%d%*d", &n);
memset(ii, -1, N_ * sizeof *ii);
n_ = 2, d_ = INF;
for (i = 0; i < n; i++) {
scanf("%s", cc), ll[i] = strlen(cc);
for (h = 0, s = 1; h < ll[i]; h++) {
d = cc[h] - '0';
if (!tt[s][d])
tt[s][d] = n_++;
s = tt[s][d];
}
if (ii[s] == -1)
ii[s] = i;
else
d_ = min(d_, ll[i]);
}
memset(dd, 0x3f, n_ * n_ * sizeof *dd);
for (s = 1; s < n_; s++)
if (ii[s] != -1)
dd[s * n_ + 1] = ll[ii[s]], qu[0][cnt[0]++] = s * n_ + 1;
while (hh[0] < cnt[0] || hh[1] < cnt[1]) {
st = hh[1] == cnt[1] || hh[0] < cnt[0] && dd[qu[0][hh[0]]] < dd[qu[1][hh[1]]] ? qu[0][hh[0]++] : qu[1][hh[1]++], s = st / n_, t = st % n_, d = dd[st] + 1;
if (dd[st] >= d_)
break;
if (s == 1 && t == 1) {
printf("%d\n", dd[st]);
return 0;
}
if ((s_ = tt[s][0]) && (t_ = tt[t][0])) {
if (dd[s_ * n_ + t_] > d)
dd[s_ * n_ + t_] = d, qu[1][cnt[1]++] = s_ * n_ + t_;
if (ii[t_] != -1 && dd[s_ * n_ + 1] > d)
dd[s_ * n_ + 1] = d, qu[1][cnt[1]++] = s_ * n_ + 1;
if (ii[s_] != -1 && dd[1 * n_ + t_] > d)
dd[1 * n_ + t_] = d, qu[1][cnt[1]++] = 1 * n_ + t_;
if (ii[s_] != -1 && ii[t_] != -1 && dd[1 * n_ + 1] > d)
dd[1 * n_ + 1] = d, qu[1][cnt[1]++] = 1 * n_ + 1;
}
if ((s_ = tt[s][1]) && (t_ = tt[t][1])) {
if (dd[s_ * n_ + t_] > d)
dd[s_ * n_ + t_] = d, qu[1][cnt[1]++] = s_ * n_ + t_;
if (ii[t_] != -1 && dd[s_ * n_ + 1] > d)
dd[s_ * n_ + 1] = d, qu[1][cnt[1]++] = s_ * n_ + 1;
if (ii[s_] != -1 && dd[1 * n_ + t_] > d)
dd[1 * n_ + t_] = d, qu[1][cnt[1]++] = 1 * n_ + t_;
if (ii[s_] != -1 && ii[t_] != -1 && dd[1 * n_ + 1] > d)
dd[1 * n_ + 1] = d, qu[1][cnt[1]++] = 1 * n_ + 1;
}
}
printf("%d\n", d_ != INF ? d_ : -1);
return 0;
}
Compilation message (stderr)
Main.c: In function 'main':
Main.c:37:42: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
37 | st = hh[1] == cnt[1] || hh[0] < cnt[0] && dd[qu[0][hh[0]]] < dd[qu[1][hh[1]]] ? qu[0][hh[0]++] : qu[1][hh[1]++], s = st / n_, t = st % n_, d = dd[st] + 1;
| ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.c:16:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
16 | scanf("%d%*d", &n);
| ^~~~~~~~~~~~~~~~~~
Main.c:20:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
20 | scanf("%s", cc), ll[i] = strlen(cc);
| ^~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |