[문제풀이] 7432번: 디스크 트리
문제유형
TRIE
Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | #include <stdio.h> #include <malloc.h> int N; typedef struct st { int check; int isWord; struct st* next; struct st* child[80]; }NODE; char idx[100] = "!#$%&'()-0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZ^_`{}~"; NODE root; char str[80]; char print_str[500*800][10]; int index(char ch) { int i; for (i = 0; idx[i] != ch; i++); return i; } void append_node(char* str, NODE* addr) { NODE* cur = addr; for(;;) { int num = index(*str); if (cur->child[num] == NULL) { NODE* node = (NODE*)calloc(1, sizeof(NODE)); cur->child[num] = node; } str++; cur = cur->child[num]; if (*str == '\0') { cur->isWord = 1; break; } else if (*str == '\\') { cur->isWord = 1; if (cur->next == NULL) { NODE* node = (NODE*)calloc(1, sizeof(NODE)); cur->next = node; append_node(str + 1, cur->next); } else if (cur->next) { append_node(str + 1, cur->next); } break; } } return; } void print_node(int cnt, int n, int dir, NODE* addr) { int i; NODE* cur = addr; if (cur != 0 && cur->isWord) { for (register int d = 0; d < dir; d++) printf(" "); printf("%s\n", print_str[cnt]); if (cur->next) { print_node(cnt + 1, 0, dir + 1, cur->next); } } if (cur == 0) return; for (i = 0; i < 80; i++) { if (cur->child[i]) { print_str[cnt][n] = idx[i]; print_str[cnt][n + 1] = 0; print_node(cnt, n + 1, dir, cur->child[i]); } } } void inputData() { scanf("%d", &N); for (register int i = 0; i < N; i++) { scanf("%s", str); append_node(str, &root); } } int main() { inputData(); print_node(0, 0, 0, &root); return 0; } | cs |
'한국으로 > 알고리즘 문제풀이' 카테고리의 다른 글
[문제풀이] 1406번: 에디터 (0) | 2018.07.05 |
---|---|
[문제풀이] 2776번: 암기왕 (0) | 2018.07.04 |
[문제풀이] 1966번: 프린터 큐 (0) | 2018.07.04 |
[문제풀이] 5926번: Cow Lineup (0) | 2018.07.04 |
[문제풀이] 1395번: 스위치 (0) | 2018.07.04 |
[문제풀이] 14706번: 구간 합 최대 (0) | 2018.07.04 |
[문제풀이] 5052번: 전화번호 목록 (0) | 2018.07.04 |
[문제풀이] 6593번: 상범 빌딩 (0) | 2018.07.04 |