Notice
Recent Posts
Recent Comments
Link
250x250
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Effective Java
- log4j2
- spring cloud
- Regular expression
- Chunk
- Node
- expire_logs_days
- regex
- spring
- MySQL
- 정규표현식
- JavaScript
- migration
- current_date
- Spring Batch
- Effective Java 3/e
- Webpack
- Express
- update
- REACT
- 퀵소트
- REACTJS
- log_bin
- git
- npm
- mysql 5.5
- nodejs
- upgrade
- eslint
- java
Archives
- Today
- Total
내 세상
[Algorithms] Hashtable / Probing / Hash Chaining / DJB2 본문
728x90
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
const int LM = 5e5 + 100;
const int MOD = 65537;
int strcmp(const char* a, const char *b){
while (*a && *a == *b){
a++;
b++;
}
return *a - *b;
}
int bCnt;
struct Node {
char id[14];
int isLogin;
Node* next;
Node* alloc(Node* np){
isLogin = 0, next = np;
return this;
}
}buf[LM], htab[MOD];
int N, cmd;
int memberCnt, loginCnt;
unsigned int djb2(char* s){
unsigned int h = 5381;
for (; *s; s++){
h = (h * 33) + *s;
}
return h % MOD;
}
Node* probing(int hidx, char* s){
Node* p = &htab[hidx];
//p->next를 확인했음.
for (; p->next; p = p->next){
if (strcmp(p->next->id, s) == 0) return p;
}
return NULL;
}
int main()
{
scanf("%d", &N);
for (int i = 0; i < N; i++){
scanf("%d %s", &cmd, buf[bCnt].id);
int hidx = djb2(buf[bCnt].id);
Node* p = probing(hidx, buf[bCnt].id);
if (cmd == 1){ // 가입 여부
printf("%d\n", p != NULL);
}
else if (cmd == 2){ // 로그인 여부
printf("%d\n", p && p->next->isLogin);
}
else if (cmd == 3){
if (p == NULL){
htab[hidx].next = buf[bCnt++].alloc(htab[hidx].next);
memberCnt++;
}
printf("%d\n", memberCnt);
}
else if (cmd == 4){ // 탈퇴
if (p){ // 회원이라면
if (p->next->isLogin == 1) loginCnt--;
p->next = p->next->next;
memberCnt--;
}
printf("%d\n", memberCnt);
}
else if (cmd == 5){ // 로그인
if (p){
if (p->next->isLogin == 0) {
p->next->isLogin = 1;
loginCnt++;
}
}
printf("%d\n", loginCnt);
}
else { // 로그아웃
if (p){
if (p->next->isLogin == 1){
p->next->isLogin = 0;
loginCnt--;
}
}
printf("%d\n", loginCnt);
}
}
return 0;
}
728x90
'Coding > Algorithms' 카테고리의 다른 글
[Algorithms] 오름차순 데이터 저장 기반 double linked list (0) | 2021.03.21 |
---|---|
[Algorithms] Double Linked List 구현 (0) | 2021.03.10 |
[Algorithms] Datastructure Queue 구현-Linked List (0) | 2021.03.10 |
[Algorithms] Disjoint sets (0) | 2021.02.26 |
[Algorithms] Merge Sort (0) | 2021.02.19 |