提出 #66046051


ソースコード 拡げる

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define Q_MAX 212345

int Q;
int T[Q_MAX];
char* S[Q_MAX];

char S_buf[512345];

struct node_s {
	int pos;
	struct node_s* next[26];
};

void ki_add(struct node_s** node, const char* str, int value) {
	if (*node == NULL) {
		*node = calloc(1, sizeof(**node));
		if (*node == NULL) exit(2);
		(*node)->pos = Q + 1;
	}
	if (*str < 'a' || 'a' + 26 <= *str) {
		if ((*node)->pos > value) (*node)->pos = value;
	} else {
		ki_add(&(*node)->next[*str - 'a'], str + 1, value);
	}
}

int ki_get(const struct node_s* node, const char* str) {
	if (node == NULL) {
		return Q + 1;
	} else {
		int ans = node->pos;
		if ('a' <= *str && *str < 'a' + 26) {
			int candidate = ki_get(node->next[*str - 'a'], str + 1);
			if (candidate < ans) ans = candidate;
		}
		return ans;
	}
}

int bit_table[Q_MAX];

void bit_add(int pos, int value) {
	pos++;
	while (pos <= Q_MAX) {
		bit_table[pos - 1] += value;
		pos += pos & (-pos);
	}
}

int bit_sum(int pos) {
	int sum = 0;
	pos++;
	while (pos > 0) {
		sum += bit_table[pos - 1];
		pos -= pos & (-pos);
	}
	return sum;
}

int main(void) {
	int i;
	struct node_s* root = NULL;
	if (scanf("%d", &Q) != 1) return 1;
	for (i = 0; i < Q; i++) {
		if (scanf("%d%512344s", &T[i], S_buf) != 2) return 1;
		S[i] = malloc(strlen(S_buf) + 1);
		if (S[i] == NULL) return 2;
		strcpy(S[i], S_buf);
	}
	/* X への追加を先に全て登録する */
	for (i = 0; i < Q; i++) {
		if (T[i] == 1) {
			ki_add(&root, S[i], i);
		}
	}
	/* 「どこで X に追加されるか」を管理し、それが現在かそれより前のものは引く */
	for (i = 0; i < Q; i++) {
		if (T[i]  == 2) {
			bit_add(ki_get(root, S[i]), 1);
		}
		printf("%d\n", bit_sum(Q + 10) - bit_sum(i));
	}
	return 0;
}

提出情報

提出日時
問題 E - Forbidden Prefix
ユーザ mikecat
言語 C (gcc 12.2.0)
得点 500
コード長 1859 Byte
結果 AC
実行時間 52 ms
メモリ 112052 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 500 / 500
結果
AC × 2
AC × 57
セット名 テストケース
Sample 00_sample_01.txt, 00_sample_02.txt
All 00_sample_01.txt, 00_sample_02.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt, 01_random_35.txt, 01_random_36.txt, 01_random_37.txt, 01_random_38.txt, 01_random_39.txt, 01_random_40.txt, 01_random_41.txt, 02_handmade_01.txt, 02_handmade_02.txt, 02_handmade_03.txt, 02_handmade_04.txt, 02_handmade_05.txt, 02_handmade_06.txt, 02_handmade_07.txt, 02_handmade_08.txt, 02_handmade_09.txt, 02_handmade_10.txt, 02_handmade_11.txt, 02_handmade_12.txt, 02_handmade_13.txt, 02_handmade_14.txt
ケース名 結果 実行時間 メモリ
00_sample_01.txt AC 1 ms 1732 KiB
00_sample_02.txt AC 1 ms 1740 KiB
01_random_01.txt AC 2 ms 2248 KiB
01_random_02.txt AC 18 ms 41928 KiB
01_random_03.txt AC 19 ms 41452 KiB
01_random_04.txt AC 29 ms 64180 KiB
01_random_05.txt AC 41 ms 92428 KiB
01_random_06.txt AC 48 ms 111588 KiB
01_random_07.txt AC 2 ms 2148 KiB
01_random_08.txt AC 13 ms 24952 KiB
01_random_09.txt AC 22 ms 44176 KiB
01_random_10.txt AC 32 ms 66132 KiB
01_random_11.txt AC 40 ms 89304 KiB
01_random_12.txt AC 49 ms 109320 KiB
01_random_13.txt AC 3 ms 2344 KiB
01_random_14.txt AC 50 ms 16132 KiB
01_random_15.txt AC 51 ms 20820 KiB
01_random_16.txt AC 52 ms 24088 KiB
01_random_17.txt AC 47 ms 50996 KiB
01_random_18.txt AC 48 ms 31264 KiB
01_random_19.txt AC 8 ms 12932 KiB
01_random_20.txt AC 24 ms 40620 KiB
01_random_21.txt AC 28 ms 31648 KiB
01_random_22.txt AC 9 ms 14488 KiB
01_random_23.txt AC 29 ms 31220 KiB
01_random_24.txt AC 49 ms 17168 KiB
01_random_25.txt AC 33 ms 12932 KiB
01_random_26.txt AC 38 ms 13780 KiB
01_random_27.txt AC 18 ms 5392 KiB
01_random_28.txt AC 47 ms 16900 KiB
01_random_29.txt AC 38 ms 21500 KiB
01_random_30.txt AC 39 ms 14824 KiB
01_random_31.txt AC 27 ms 12544 KiB
01_random_32.txt AC 29 ms 10172 KiB
01_random_33.txt AC 15 ms 12428 KiB
01_random_34.txt AC 44 ms 16024 KiB
01_random_35.txt AC 32 ms 12500 KiB
01_random_36.txt AC 34 ms 12416 KiB
01_random_37.txt AC 39 ms 13488 KiB
01_random_38.txt AC 41 ms 24220 KiB
01_random_39.txt AC 40 ms 31456 KiB
01_random_40.txt AC 38 ms 35776 KiB
01_random_41.txt AC 36 ms 39184 KiB
02_handmade_01.txt AC 47 ms 112052 KiB
02_handmade_02.txt AC 2 ms 2712 KiB
02_handmade_03.txt AC 36 ms 10140 KiB
02_handmade_04.txt AC 35 ms 10212 KiB
02_handmade_05.txt AC 6 ms 2872 KiB
02_handmade_06.txt AC 6 ms 2816 KiB
02_handmade_07.txt AC 4 ms 2260 KiB
02_handmade_08.txt AC 4 ms 2344 KiB
02_handmade_09.txt AC 5 ms 2620 KiB
02_handmade_10.txt AC 5 ms 2700 KiB
02_handmade_11.txt AC 12 ms 8016 KiB
02_handmade_12.txt AC 12 ms 8100 KiB
02_handmade_13.txt AC 34 ms 60956 KiB
02_handmade_14.txt AC 34 ms 61068 KiB