홍시홍의 프로그래밍

[2017 카카오 코딩테스트] 뉴스 클러스터링 본문

알고리즘 문제풀이/카카오

[2017 카카오 코딩테스트] 뉴스 클러스터링

홍시홍 2020. 8. 4. 23:35

분류 

시뮬레이션

요구사항

자카드 유사도 구하기

1. 주어진 문자열 중 두 개의 연속된 문자로 구성된 문자 구하기

2. 합집합 구하기

3. 교집합 구하기

풀이

1. 주어진 문자열 중 두 개의 연속된 문자로 구성된 문자 구하기

-> 문자일 경우 추가, 문자가 아닐 경우 clear를 해주어서 2개가 연속된 문자일 경우 map에 포함 시킨다

2. 합집합 구하기

-> map1에는 없고 map2에는 존재하면 합집합

3. 교집합 구하기

-> map1에도 있고 map2에도 존재하면 교집합

-> 교집합은 동일한 여러개의 교집합이 존재할 수 있으므로 예외 처리해준다

 

#include <string>
#include <vector>
#include <iostream>
#include <queue>
#include <string.h>
#include <algorithm>
#include <deque>
#include <map>

using namespace std;
map<string,int> Map1;
map<string,int> Map2;
map<string,int> sum;
string str1= "FRANCE";
string str2= "french";
deque<char> dq1;
deque<char> dq2;
char Get_ch(char ch){
    if(ch=='a' || ch=='A')  return 'A';
    else if(ch=='b' || ch=='B') return 'B';
    else if(ch=='c' || ch=='C') return 'C';
    else if(ch=='d' || ch=='D') return 'D';
    else if(ch=='e' || ch=='E') return 'E';
    else if(ch=='f' || ch=='F') return 'F';
    else if(ch=='g' || ch=='G') return 'G';
    else if(ch=='h' || ch=='H') return 'H';
    else if(ch=='i' || ch=='I') return 'I';
    else if(ch=='j' || ch=='J') return 'J';
    else if(ch=='k' || ch=='K') return 'K';
    else if(ch=='l' || ch=='L') return 'L';
    else if(ch=='n' || ch=='N') return 'N';
    else if(ch=='m' || ch=='M') return 'M';
    else if(ch=='o' || ch=='O') return 'O';
    else if(ch=='p' || ch=='P') return 'P';
    else if(ch=='q' || ch=='Q') return 'Q';
    else if(ch=='r' || ch=='R') return 'R';
    else if(ch=='s' || ch=='S') return 'S';
    else if(ch=='t' || ch=='T') return 'T';
    else if(ch=='u' || ch=='U') return 'U';
    else if(ch=='v' || ch=='V') return 'V';
    else if(ch=='w' || ch=='W') return 'W';
    else if(ch=='x' || ch=='X') return 'X';
    else if(ch=='y' || ch=='Y') return 'Y';
    else if(ch=='z' || ch=='Z') return 'Z';
}
int main(){
    /*
    for(int i=0 ; i <str1.size() ; i++){
        dq1.push_back(str1[i]);
    }
    for(int i=0 ; i <str2.size() ; i++){
        dq2.push_back(str2[i]);
    }
    */
    double Ccnt=0;
    double Scnt=0;
    for(int i=0 ; i< str1.size() ; i++){
        if(str1[i] >=97 && str1[i]<=122){
            dq1.push_back(Get_ch(str1[i]));
        }
        else if(str1[i] >=65 && str1[i]<=90){
            dq1.push_back(Get_ch(str1[i]));
        }
        else{
            dq1.clear();
        }

        if(dq1.size()==2){
            string temp;
            for(auto j:dq1){
                temp+=j;
            }   
            cout<<temp<<endl;
            Map1[temp]++;
            Scnt++;
            dq1.pop_front();
        }

    }
    cout<<endl;
    for(int i=0 ; i< str2.size() ; i++){
        if(str2[i] >=97 && str2[i]<=122){
            dq2.push_back(Get_ch(str2[i]));
        }
        else if(str2[i] >=65 && str2[i]<=90){
            dq2.push_back(Get_ch(str2[i]));
        }
        else{
            dq2.clear();
        }
        if(dq2.size()==2){
            string temp;
            for(auto j:dq2){
                temp+=j;
            }   
            cout<<temp<<endl;
            //맵1에는 없는게 추가 되면 전체 집합 1개 증가
            if(Map1[temp] == 0){
                Map2[temp]++;
                Scnt++;
            }
            //현재 temp가 맵1에 존재하는 데이터이다.
            else{
                Map1[temp]--;
                Map2[temp]++;
                Ccnt++;
            }
            
            dq2.pop_front();
        }
    }
    if(Map1.size() ==0 && Map2.size()==0){
        cout<<65536<<endl;
        return 0;
    }
    double ans=Ccnt/Scnt;

    cout<<ans<<endl;
    cout<<ans*65536<<endl;
    cout<<Scnt<<endl;
    cout<<Ccnt<<endl;
}

 

 

Comments