회원가입
로그인
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
글쓰기
질문 도움말
자주묻는 질문
제 코드가 무엇이 문제인지 모르겠습니다. 예제 출력도 잘 되구요.
14425번 - 문자열 집합
jwj1199
1년 전
0
counting sort + binary search로 set 라이브러리 없이 구현을 했는데, 이게 문제일려나요?
#include <iostream> #include <string> int binary_search (std::string* arr, int size, std::string target) { int left = 0; int right = size-1; while(left <= right) { int mid = (left + right)/2; if(arr[mid]==target) { return 1; } else if(arr[mid] < target) { left = mid + 1; } else { right = mid - 1; } } return 0; } int main() { int n, m; std::cin >> n >> m; std::string* arr_n = new std::string[n]; std::string* arr_m = new std::string[m]; std::string* arr_n_raw = arr_n; //arr_n 입력 for (int i = 0; i < n; i++) { std::cin >> arr_n[i]; } //arr_m 입력 for (int i = 0; i < m; i++) { std::cin >> arr_m[i]; } //버킷 생성. 2중 배열의 이유는 [길이][단어의 인덱스] 이런 식으로 구현을 위해 //len_count는 카운트 정렬의 변형을 만들기 위해 사용 std::string** bucket = new std::string*[501]; int len_count[501] = {0}; for (int i = 0; i < 501; i ++) { bucket[i] = new std::string[n]; } //버킷 내부에 삽입 for(int i = 0; i < n; i++) { int len = arr_n[i].length(); bucket[len][len_count[len]++] = arr_n[i]; } //사전순 정렬 수행 for (int i = 1; i <= 50; i++) { for (int j = 0; j < len_count[i] - 1; j++) { for (int k = j + 1; k < len_count[i]; k++) { if (bucket[i][j] > bucket[i][k]) { std::string temp = bucket[i][j]; bucket[i][j] = bucket[i][k]; bucket[i][k] = temp; } } } } //버켓에 있는 정렬된 단어들을 arr_n에 삽입 및 중복 처리 // bucket 정렬된 데이터 → arr_n에 다시 저장 int idx = 0; for (int len = 1; len <= 500; len++) { for (int i = 0; i < len_count[len]; i++) { arr_n[idx++] = bucket[len][i]; } } n = idx; int unique_idx = 0; for (int i = 1; i < n; i++) { if (arr_n[i] != arr_n[unique_idx]) { unique_idx++; arr_n[unique_idx] = arr_n[i]; } } n = unique_idx + 1; int cnt = 0; for (int i = 0; i < m; i++) { cnt += binary_search(arr_n, n, arr_m[i]); } std::cout << cnt << '\n'; for (int i = 0; i < 501; i++) { delete[] bucket[i]; } delete[] bucket; delete[] arr_m; delete[] arr_n_raw; }
댓글을 작성하려면
로그인
해야 합니다.
jwj1199 1년 전
counting sort + binary search로 set 라이브러리 없이 구현을 했는데, 이게 문제일려나요?