자기개발/코딩테스트

C++ (코딩테스트) - [1차] 뉴스 클러스터링(프로그래머스)

pi92 2022. 9. 30. 13:06

문제

 

https://school.programmers.co.kr/learn/courses/30/lessons/17677

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

내가 푼 답

#include <string>
#include <vector>
using namespace std;

void make_vec(vector<string>& vec, string str)
{
	for (int i = 0; i < str.size(); i++)
	{
		string temp;
		if (str[i] >= 'A' && str[i] <= 'Z') str[i] += 'a' - 'A';
		if (str[i+1] >= 'A' && str[i+1] <= 'Z') str[i+1] += 'a' - 'A';
		if (str[i] >= 'a' && str[i] <= 'z' && str[i + 1] >= 'a' && str[i + 1] <= 'z')
		{
			temp.push_back(str[i]);
			temp.push_back(str[i+1]);
			vec.push_back(temp);
		}
	}
}

int solution(string str1, string str2) {
	int answer = 0;
	vector<string> s1, s2;
	make_vec(s1, str1);
	make_vec(s2, str2);
	int tot_size = s1.size() + s2.size();
	int inter_cnt = 0;
	for (int i = 0; i < s1.size(); i++)
	{
		for (int j = 0; j < s2.size(); j++)
		{
			if (s1[i] == s2[j])
			{
				inter_cnt++;
				s2.erase(s2.begin() + j);
				break;
			}
		}
	}
	if (s1.size() == 0 && s2.size() == 0) return 65536;
	answer = inter_cnt * 65536 / (tot_size - inter_cnt);

	return answer;
}

다른사람의 답

 

#include <bits/stdc++.h>
using namespace std;
short a, b, C[676], D[676];
int solution(string A, string B) {
    for(int i=1; i<A.size(); i++)
        if(isalpha(A[i-1]) && isalpha(A[i]))
            C[(A[i-1]&31)*26+(A[i]&31)]++;
    for(int i=1; i<B.size(); i++)
        if(isalpha(B[i-1]) && isalpha(B[i]))
            D[(B[i-1]&31)*26+(B[i]&31)]++;
    for(int i=0; i<676; i++) a+=min(C[i], D[i]), b+=max(C[i], D[i]);
    return b ? a*65536/b : 65536;
}