답안 #485190

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
485190 2021-11-06T13:53:38 Z rainboy CSS (COI14_css) C
60 / 100
211 ms 41108 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N	5000
#define M	5000000
#define K	5000
#define Q	5
#define L	8192

int *ej[N], eo[N], ll[N], rr[N], pp[N]; char *ss[N];
int ll_[Q][K], rr_[Q][K], kk[Q]; char direct[Q][K];
char *tt[M]; int idx[M]; char used[M];

unsigned int X = 12345;

int rand_() {
	return (X *= 3) >> 1;
}

void append(int i, int j) {
	int o = eo[i]++;

	if (o >= 2 && (o & o - 1) == 0)
		ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
	ej[i][o] = j;
}

void sort(int *hh, int l, int r) {
	while (l < r) {
		int i = l, j = l, k = r, h = hh[l + rand_() % (r - l)], tmp;

		while (j < k) {
			int c = strcmp(tt[hh[j]], tt[h]);

			if (c == 0)
				j++;
			else if (c < 0) {
				tmp = hh[i], hh[i] = hh[j], hh[j] = tmp;
				i++, j++;
			} else {
				k--;
				tmp = hh[j], hh[j] = hh[k], hh[k] = tmp;
			}
		}
		sort(hh, l, i);
		l = k;
	}
}

char dp[N][K]; int qu[N], cnt, g_;

int check(int l, int r) {
	int h;

	for (h = l; h < r; h++)
		if (!used[idx[h]])
			return 0;
	return 1;
}

void dfs(int p, int i) {
	int o, h;

	for (h = ll[i]; h < rr[i]; h++)
		used[idx[h]] = 1;
	memset(dp[i], 0, kk[g_] * sizeof *dp[i]);
	for (h = kk[g_] - 1; h >= 0; h--)
		if ((p == -1 ? h == 0 : dp[p][h])) {
			if (!direct[g_][h])
				dp[i][h] = 1;
			if (check(ll_[g_][h], rr_[g_][h])) {
				if (h + 1 == kk[g_])
					qu[cnt++] = i;
				else
					dp[i][h + 1] = 1;
			}
		}
	for (h = ll[i]; h < rr[i]; h++)
		used[idx[h]] = 0;
	for (o = 0; o < eo[i]; o++) {
		int j = ej[i][o];

		dfs(i, j);
	}
}

int main() {
	static char buf[L];
	static int hh[M];
	int n, n_, m, q, g, h, i, i_, j, j_;

	fgets(buf, L, stdin), sscanf(buf, "%d", &n);
	n_ = 0, m = 0, cnt = 0;
	while (n--) {
		fgets(buf, L, stdin);
		if (buf[1] == 'd') {
			i = j = 9;
			while (buf[j] != '\'')
				j++;
			buf[j] = 0, ss[n_] = strdup(buf + i);
			i = j + 1;
			while (buf[i] != '\'')
				i++;
			ll[n_] = m;
			for (j = i + 1; buf[j] != '>'; j++)
				if (buf[j] == ' ' || buf[j] == '\'') {
					buf[j] = 0, tt[m++] = strdup(buf + i + 1);
					i = j;
				}
			rr[n_] = m;
			if (cnt)
				pp[n_] = qu[cnt - 1], append(qu[cnt - 1], n_);
			else
				pp[n_] = -1;
			ej[n_] = (int *) malloc(2 * sizeof *ej[n_]);
			qu[cnt++] = n_++;
		} else
			cnt--;
	}
	n = n_;
	fgets(buf, L, stdin), sscanf(buf, "%d", &q);
	for (g = 0; g < q; g++) {
		fgets(buf, L, stdin);
		for (i = -1, j = 0; buf[j]; j++)
			if (buf[j] == ' ' || buf[j] == '\n') {
				if (buf[i + 1] == '>')
					direct[g][kk[g]] = 1;
				else {
					ll_[g][kk[g]] = m;
					for (i_ = i + 1, j_ = i + 2; j_ <= j; j_++)
						if (j_ == j || buf[j_] == '.') {
							buf[j_] = 0, tt[m++] = strdup(buf + i_ + 1);
							i_ = j_;
						}
					rr_[g][kk[g]] = m;
					kk[g]++;
				}
				i = j;
			}
	}
	for (h = 0; h < m; h++)
		hh[h] = h;
	sort(hh, 0, m);
	for (h = 0, i = 0; h < m; h++)
		idx[hh[h]] = h + 1 == m || strcmp(tt[hh[h + 1]], tt[hh[h]]) != 0 ? i++ : i;
	for (g = 0; g < q; g++) {
		g_ = g, cnt = 0;
		for (i = 0; i < n; i++)
			if (pp[i] == -1)
				dfs(-1, i);
		printf("%d", cnt);
		for (h = 0; h < cnt; h++)
			printf(" %s", ss[qu[h]]);
		printf("\n");
	}
	return 0;
}

Compilation message

css.c: In function 'append':
css.c:24:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   24 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
css.c: In function 'main':
css.c:93:2: warning: ignoring return value of 'fgets' declared with attribute 'warn_unused_result' [-Wunused-result]
   93 |  fgets(buf, L, stdin), sscanf(buf, "%d", &n);
      |  ^~~~~~~~~~~~~~~~~~~~
css.c:96:3: warning: ignoring return value of 'fgets' declared with attribute 'warn_unused_result' [-Wunused-result]
   96 |   fgets(buf, L, stdin);
      |   ^~~~~~~~~~~~~~~~~~~~
css.c:122:2: warning: ignoring return value of 'fgets' declared with attribute 'warn_unused_result' [-Wunused-result]
  122 |  fgets(buf, L, stdin), sscanf(buf, "%d", &q);
      |  ^~~~~~~~~~~~~~~~~~~~
css.c:124:3: warning: ignoring return value of 'fgets' declared with attribute 'warn_unused_result' [-Wunused-result]
  124 |   fgets(buf, L, stdin);
      |   ^~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 2380 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 21580 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 97 ms 26700 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Correct 128 ms 40132 KB Output is correct
2 Correct 63 ms 26308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 123 ms 40276 KB Output is correct
2 Correct 64 ms 26280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 141 ms 40112 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 149 ms 40176 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 121 ms 40352 KB Output is correct
2 Correct 116 ms 26716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 211 ms 41108 KB Output is correct
2 Correct 81 ms 26284 KB Output is correct