hwooo
프로그래머스 (C/C++) 17677 : 2018 KAKAO BLIND RECRUITMENT [1차] 뉴스 클러스터링 본문
Study/Algorithm
프로그래머스 (C/C++) 17677 : 2018 KAKAO BLIND RECRUITMENT [1차] 뉴스 클러스터링
hwooo 2024. 6. 11. 15:40https://school.programmers.co.kr/learn/courses/30/lessons/17677
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr



풀이
26 * 26 크기의 2차원 배열을 선언 후 2자리 부분문자열의 개수를 센다.
str1, str2 의 부분문자열의 개수가 저장된 2차원 배열을 순회하며 교집합과 합집합의 개수를 구한다.
코드
#include <string>
#include <algorithm>
#include <ctype.h>
using namespace std;
int solution(string str1, string str2) {
double unionCnt = 0, inter = 0;
int substrCnt1[26][26] = {0, }, substrCnt2[26][26] = {0, };
int lenStr1 = str1.size(), lenStr2 = str2.size();
// str1 순회하며 서브 문자열 생성 및 해당 문자열 cnt++
for (int i = 0; i < lenStr1 - 1; i++) {
if(isalpha(str1[i]) && isalpha(str1[i + 1])) {
substrCnt1[tolower(str1[i]) - 'a'][tolower(str1[i + 1]) - 'a']++;
}
}
// str2 순회하며 서브 문자열 생성 및 해당 문자열 cnt++
for (int i = 0; i < lenStr2 - 1; i++) {
if(isalpha(str2[i]) && isalpha(str2[i + 1])) {
substrCnt2[tolower(str2[i]) - 'a'][tolower(str2[i + 1]) - 'a']++;
}
}
for (int i = 0; i < 26; i++) {
for (int j = 0; j < 26; j++) {
unionCnt += (substrCnt1[i][j] + substrCnt2[i][j]);
// str1, str2 둘 다 값이 있다면 더 적은 쪽의 문자열 cnt가 교집합
if (substrCnt1[i][j] && substrCnt2[i][j]) {
inter += min(substrCnt1[i][j], substrCnt2[i][j]);
unionCnt -= min(substrCnt1[i][j], substrCnt2[i][j]); // 합집합 - 교집합
}
}
}
return !unionCnt ? 65536 : (inter * 65536 / unionCnt);
}
'Study > Algorithm' 카테고리의 다른 글
| LeetCode (C/C++) 1277. Count Square Submatrices with All Ones (0) | 2024.10.15 |
|---|---|
| 프로그래머스 (C/C++) 258712 : 가장 많이 받은 선물 (0) | 2024.06.14 |
| LeetCode (C/C++) 1314. Matrix Block Sum (0) | 2024.06.07 |
| LeetCode (C/C++) 279. Perfect Squares (0) | 2024.06.06 |
| LeetCode (C/C++) 86. Partition List (0) | 2024.06.04 |