5052 전화번호 목록 문제 풀기

준준준맨·2021년 9월 2일

https://www.acmicpc.net/problem/5052

#include <stdio.h>

int triechild[100001][10];
int terminal[100001];
int tsize;

int insertTrie(char* input) {

int root = 0;
while (*input) { //(*str)은 \0이 될때까지 돔
	int num = *input - '0'; // 문자를 정수로 변환
	if (triechild[root][num] == 0) {
		triechild[root][num] = ++tsize;
	}
	terminal[root] = 2;
	root = triechild[root][num];


	if (terminal[root] == 1) {
		return 1;
	}
	input += 1;

}


if (terminal[root] == 2) return 1;

terminal[root] = 1;
return 0;

}

int main() {
int T; // 반복할 수를 저장한는 변수
scanf("%d", &T);

while (T--) {
	int nphon;
	scanf("%d", &nphon);// 전화번호 수를 저장하는 변수

	for (int i = 0; i <= tsize; i++) {
		terminal[i] = 0;
		for (int j = 0; j < 26; j++) {
			triechild[i][j] = 0;
		}
	}// 배열들을 0으로 한다.

	tsize = 0;
	char number[11];
	int flag = 0;
	while (nphon--) {
		scanf("%s", number);

		if (flag) continue;
		if (insertTrie(number)) {
			flag = 1;
		}
	}

	printf("%s\n", flag ? "NO": "YES");
}

}
이번 문제는 어려웠습니다
그래서 다른 사람의 소스를 참고하고 이해하려고 노력했습니다.

profile
대학생

0개의 댓글