반응형
http://www.jungol.co.kr/bbs/board.php?bo_table=pbank&wr_id=2425&sca=99&sfl=wr_hit&stx=3143
#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;
}
반응형
'프로그래밍 알고리즘' 카테고리의 다른 글
[정올 3239] 구간의 최소값 (1) | 2023.01.10 |
---|---|
[정올 3137] 전화번호 검색 (0) | 2023.01.10 |
[정올 3136] const구간의 합 구하기(2D) (0) | 2023.01.10 |
[정올 3135] const구간의 합 구하기(1D) (0) | 2023.01.10 |
[정올 3123] 키로거(Keylogger) (0) | 2023.01.10 |