회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
추가된 문제
문제 순위
문제
푼 사람이 한 명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
채점 현황
게시판
그룹
더 보기
재채점 기록
블로그
도움말
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
C언어 해쉬 구현해서 풀었는데 뭐가 틀렸는지 모르겠습니다
1269번 - 대칭 차집합
gsejwno131
2년 전
0
해쉬로 구현했는데 틀렸다고 나오는데 왜 틀렸는지 모르겠네요 반례좀 찾아주세요
#include <stdio.h> #include <string.h> #include <stdlib.h> #define MAX_TBL 900001 typedef unsigned int (Hashc)(int value); typedef struct _slot{ int key; int status; // 1이면 존재 0이면 존재하지 않음 } Slot; typedef struct _table{ Slot * tbl; Hashc * hf; } Table; void TBLInit(Table * pt, Hashc * hf){ // 테이블 초기화 pt->hf=hf; pt->tbl=(Slot *)malloc(sizeof(Slot)*MAX_TBL); for(int i=0; i<MAX_TBL; i++) pt->tbl[i].status=0; } unsigned int Hash(int value){ // 곱셈 해쉬 함수 double A=0.618033987; double hv=value*A; return (int)(MAX_TBL * (hv-(int)hv)); } void TBLInsert(Table * pt, int value){ // 데이터 삽입 int hv=pt->hf(value); if(pt->tbl[hv].status==0){ // 존재하지 않으면 삽입 pt->tbl[hv].key=value; pt->tbl[hv].status=1; } else{ while(pt->tbl[hv].status!=0){ // 선형탐사로 자리 찾고 삽입 hv=(hv+1)%MAX_TBL; } pt->tbl[hv].key=value; pt->tbl[hv].status=1; } } int TBLSearch(Table * pt, int value){ int hv=pt->hf(value); while(pt->tbl[hv].status!=0){ // 존재하면 1반환 if(pt->tbl[hv].key==value) return 1; hv=(hv+1)%MAX_TBL; } return 0; // 존재 하지 않는다면 0반환 } void TBLDelete(Table * pt, int value){ int hv=pt->hf(value); while(pt->tbl[hv].status!=0){ // 선형탐사로 찾고 종료 if(pt->tbl[hv].key==value){ pt->tbl[hv].status=0; return; } hv=(hv+1)%MAX_TBL; } } int main(void){ int i, A, B; int num, countResult=0; Table numtbl; TBLInit(&numtbl, Hash); scanf("%d %d", &A, &B); for(i=0; i<A; i++){ // A 집합의 데이터에 삽입 scanf("%d", &num); TBLInsert(&numtbl, num); countResult++; // 결과값에 +A } for(i=0; i<B; i++){ // B 집합의 데이터 삽입 scanf("%d", &num); if(TBLSearch(&numtbl, num)){ // 중복되면 결과값에 -1 TBLDelete(&numtbl, num); countResult--; } else{ // 중복이 아니라면 결과값 +1 TBLInsert(&numtbl, num); countResult++; } } printf("%d", countResult); return 0; }
댓글을 작성하려면
로그인
해야 합니다.
gsejwno131 2년 전
해쉬로 구현했는데 틀렸다고 나오는데 왜 틀렸는지 모르겠네요 반례좀 찾아주세요