프로그래밍 알고리즘

[정올 3143] 생명체 분류

꾸준한사람 2023. 1. 10. 05:30
반응형

http://www.jungol.co.kr/bbs/board.php?bo_table=pbank&wr_id=2425&sca=99&sfl=wr_hit&stx=3143 

 

JUNGOL

 

www.jungol.co.kr

#include <stdio.h>

enum {
	A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, MAX
};
struct nd {
	int num;
	nd* pWord;
	nd() : num(0), pWord(nullptr) {}
	~nd() {
		if (pWord)
			for (int i = 0; i < MAX; i++)
				if (pWord[i].pWord) delete [] pWord[i].pWord;
	}
} Root[MAX];
int TC, NUM;
char word[14];

int strlen(char* s, int len = 0) {
	while (s[len]) len++;
	return len;
}

int main(void)
{
	scanf("%d", &TC);

	for (int t = 0; t < TC; t++) {
		scanf(" %d ", &NUM);
		for (int n = 0; n < NUM; n++) {
			scanf("%s", &word[0]);
			word[1] = ' ';
			scanf("%s", &word[2]);

			int len = strlen(word);
			word[len] = 0;
			nd* pCur = Root;

			if (word[0] == 'I') {
				for (int i = 2; i < len; i++) {
					int idx = word[i] - 'A';
					pCur[idx].num++;
					if (word[i + 1] != 0 && pCur[idx].pWord == nullptr) {
						pCur[idx].pWord = new nd[MAX];
					}
					pCur = pCur[idx].pWord;
				}
			}
			else { // O인 경우
				int ResultNum = 0, idx = 0;
				for (int i = 2; i < len - 1; i++) {
					idx = word[i] - 'A';
					if (pCur == nullptr) break;
					pCur = pCur[idx].pWord;
				}
				idx = word[len - 1] - 'A';
				if (pCur != nullptr) ResultNum = pCur[idx].num;
				printf("%d ", ResultNum);
			}
		}
		printf("\n");
		for (int i = 0; i < MAX; i++) {
			if (Root[i].pWord) delete[] Root[i].pWord;
			Root[i].num = 0;
			Root[i].pWord = nullptr;
		}
	}

	return 0;
}
반응형