회원가입
로그인
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
글쓰기
질문 도움말
자주묻는 질문
4%에서 출력초과도 뜨고 뭐 바꾸다보니 틀렸습니다도 뜨네요
1764번 - 듣보잡
bncandy
2년 전
0
왜 그런건지 아무리 고민해봐도 답을 찾기 어렵습니다. ㅠ
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_STRING_LENGTH 20 char no_see[500000][MAX_STRING_LENGTH]; short int mapping[500000] = { 0, }; char no_hear[500000][MAX_STRING_LENGTH]; char common[500000][MAX_STRING_LENGTH]; void merge(char initlist[][MAX_STRING_LENGTH], char mergedlist[][MAX_STRING_LENGTH], int i, int m, int n) { int j, k; j = m + 1; k = i; while (i <= m && j <= n) { if (strcmp(initlist[i], initlist[j]) < 0) strcpy(mergedlist[k++], initlist[i++]); else strcpy(mergedlist[k++], initlist[j++]); } while (i <= m) strcpy(mergedlist[k++], initlist[i++]); while (j <= n) strcpy(mergedlist[k++], initlist[j++]); } void mergePass(char initlist[][MAX_STRING_LENGTH], char resultlist[][MAX_STRING_LENGTH], int n, int s) { int i, j; for (i = 0; i <= n - 2 * s; i += 2 * s) merge(initlist, resultlist, i, i + s - 1, i + 2 * s - 1); if (i + s < n) merge(initlist, resultlist, i, i + s - 1, n - 1); else for (j = i; j < n; j++) strcpy(resultlist[j], initlist[j]); } void mergeSort(char setup[][MAX_STRING_LENGTH], int n) { int s = 1; char(*extra)[MAX_STRING_LENGTH] = malloc(n * MAX_STRING_LENGTH); while (s < n) { mergePass(setup, extra, n, s); s *= 2; mergePass(extra, setup, n, s); s *= 2; } free(extra); } main() { int i, j, N, M; int min_index = 0; unsigned long hash; char* p = NULL; int cnt = 0; scanf("%d %d", &N, &M); getchar(); for (i = 0; i < N; i++) { fgets(no_see[i], MAX_STRING_LENGTH, stdin); no_see[i][strcspn(no_see[i], "\n")] = '\0'; } for (i = 0; i < M; i++) { fgets(no_hear[i], MAX_STRING_LENGTH, stdin); no_hear[i][strcspn(no_hear[i], "\n")] = '\0'; } /*for (i = 0; i < N - 1; i++) { char temp[21] = { 0, }; min_index = i; for (j = i + 1; j < N; j++) { if (strcmp(no_see[j], no_see[min_index]) < 0) min_index = j; } strcpy(temp, no_see[min_index]); strcpy(no_see[min_index], no_see[i]); strcpy(no_see[i], temp); }*/ mergeSort(no_see, N); for (i = 0; i < M; i++) { hash = 5381; for (p = no_hear[i]; *p; p++) { hash = ((hash << 5) + hash) + *p; } mapping[hash % 500000] = 1; } for (i = 0; i < N; i++) { hash = 5381; for (p = no_see[i]; *p; p++) hash = ((hash << 5) + hash) + *p; if (mapping[hash % 500000]) { strcpy(common[cnt], no_see[i]); cnt++; } } printf("%d\n", cnt); for (i = 0; i < cnt; i++) { if (i == cnt - 1) printf("%s", common[i]); else printf("%s\n", common[i]); } }
bncandy
2년 전
0
제가 쓰는 해시가 잘 못되었나봅니다.
이분 탐색하니 풀렸어요.
댓글을 작성하려면
로그인
해야 합니다.
bncandy 2년 전
왜 그런건지 아무리 고민해봐도 답을 찾기 어렵습니다. ㅠ