회원가입
로그인
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
글쓰기
질문 도움말
자주묻는 질문
해시 구현이 틀렸을까요?
1764번 - 듣보잡
pungsock
2년 전
0
문자열 해시를 djb2를 참고해서 체인형태로 구현해보려했습니다.
틀렸습니다가 뜨는데 체인이 제대로 구현이 안된걸까요?
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <algorithm> #include <vector> using namespace std; typedef char s8; typedef unsigned short u16; typedef unsigned int u32; typedef unsigned long u64; #define TABLE_SIZE 500009 #define mod 5381 typedef struct bucket{ s8 *str; struct bucket *next; }bucket; bucket h[TABLE_SIZE]; void init_table() { int i = TABLE_SIZE; while(i--) { h[i].str = NULL; h[i].next = NULL; } } u64 Hash(s8 *str) { u64 key = 5381; while(*str != '\0') { key = (((key<<5) + key) + *str) % TABLE_SIZE; str++; } return key; } static inline void insert_bucket(s8 *str) { s8 len = strlen(str)+1; //null포함하기 위해 s8 *newstr = (s8 *)malloc(len); memmove(newstr, str, len); u64 key = Hash(str); bucket *b = &h[key]; if(b->str) { b = (bucket *)malloc(sizeof(bucket)); while(b->next) b = b->next; } b->str = newstr; b->next = NULL; } static inline bucket *find_str(char *str) { u64 key = Hash(str); bucket *b = &h[key]; if(!b->str) return NULL; do { if(!strcmp(str, b->str)) return b; b = b->next; } while(b); return NULL; } bool cmp(const bucket* s1, const bucket* s2) { return strcmp(s1->str, s2->str) > 0; } int main() { init_table(); u32 n, m; scanf("%d %d", &n, &m); s8 input[21]; while(n--) { scanf("%s", input); insert_bucket(input); } vector<bucket *> v; while(m--) { scanf("%s", input); bucket *ptr = find_str(input); if(ptr) { v.push_back(ptr); } } sort(v.begin(), v.end(), cmp); printf("%ld\n", v.size()); while(!v.empty()) { printf("%s\n", v.back()->str); v.pop_back(); } return 0; }
댓글을 작성하려면
로그인
해야 합니다.
pungsock 2년 전
문자열 해시를 djb2를 참고해서 체인형태로 구현해보려했습니다.
틀렸습니다가 뜨는데 체인이 제대로 구현이 안된걸까요?