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; }
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int dd[N_ * N_];
void sort(int *ii, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;
while (j < k)
if (dd[ii[j]] == dd[i_])
j++;
else if (dd[ii[j]] < dd[i_]) {
tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
i++, j++;
} else {
k--;
tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
}
sort(ii, l, i);
l = k;
}
}
int main() {
static char cc[L + 1];
static int ll[N], tt[N_][2], ii[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;
sort(qu[0], 0, cnt[0]);
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:65:42: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
65 | 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:43:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
43 | scanf("%d%*d", &n);
| ^~~~~~~~~~~~~~~~~~
Main.c:47:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | 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... |