트라이 기본 연습 문제
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <malloc.h>
struct tree {
int flag;
struct tree* child[10];
}root;
int main() {
int t, N, ans;
char val[11];
scanf("%d\n", &t);
for (int test_case = 1; test_case <= t; test_case++) {
scanf("%d\n", &N);
for (int i = 0; i < 10; i++) {
root.child[i] = NULL;
}
ans = 1;
for (int i = 0; i < N; i++) {
scanf("%s", &val);
if (ans == 1) {
struct tree* t = &root;
for (int j = 0; val[j]; j++) {
if (t->child[val[j] - '0'] == NULL) {
t->child[val[j] - '0'] = (struct tree*)calloc(1,sizeof(struct tree));
t = t->child[val[j] - '0'];
}
else {
t = t->child[val[j] - '0'];
if (val[j + 1] == 0) {
ans = 0;
break;
}
}
if (t->flag == 1) {
ans = 0;
break;
}
else if (val[j + 1] == 0) {
t->flag = 1;
}
}
}
}
if (ans) printf("YES\n");
else printf("NO\n");
}
return 0;
}
'컴퓨터 > Problem Solving' 카테고리의 다른 글
(BOJ, 1062) 가르침 (0) | 2020.01.28 |
---|---|
(BOJ, 1806) 부분합 (0) | 2020.01.27 |
(BOJ, 1753) 최단경로 (No Library) (0) | 2020.01.26 |
Expert 코드 (0) | 2020.01.02 |
좀비 (0) | 2019.11.17 |