뉴스 클러스터링 (자바)

뉴스 클러스터링 (자바)

728x90

package prog; import java.util.HashMap; public class 뉴스클러스터링 { public static void main(String[] args) { Solution_뉴스클러스터링 s = new Solution_뉴스클러스터링(); System.out.println(s.solution("FRANCE", "french")); } } class Solution_뉴스클러스터링{ public int solution(String str1, String str2) { int answer = 0; HashMap map1 = new HashMap<>(); HashMap map2 = new HashMap<>(); makeMultiMap(map1,str1); makeMultiMap(map2,str2); HashMap sumMap = new HashMap<>(); HashMap diffMap; makeSumMap(map1, sumMap); makeSumMap(map2, sumMap); diffMap =makeDiffMap(map1, map2); int sumCount = getCount(sumMap); int diffCount = getCount(diffMap); if (sumCount == 0) { return 65536; } answer = (int)((double)diffCount/sumCount*65536); return answer; } private int getCount(HashMap map) { int count=0; for (String key : map.keySet()) { count+= map.get(key); } return count; } private HashMap makeDiffMap(HashMap map1,HashMap map2) { HashMap diffMap = new HashMap<>(); for (String key : map1.keySet()) { if(map2.containsKey(key)){ diffMap.put(key, Math.min(map1.get(key), map2.get(key))); } } return diffMap; } private void makeSumMap(HashMap map,HashMap sumMap ) { for (String key : map.keySet()) { if(sumMap.containsKey(key)){ sumMap.put(key, Math.max(map.get(key), sumMap.get(key))); } else sumMap.put(key, map.get(key)); } } public void makeMultiMap(HashMap map, String str){ for (int i = 0; i < str.length()-1; i++) { String tmpStr = str.substring(i,i+2); tmpStr = tmpStr.toUpperCase(); if (charCheck(tmpStr.charAt(0)) && charCheck(tmpStr.charAt(1))) { if (map.containsKey(tmpStr)) { map.put(tmpStr, map.get(tmpStr) + 1); } else map.put(tmpStr, 1); } } } public boolean charCheck(char c){ if('A'<=c && c<='Z'){ return true; } return false; } }

728x90

from http://arch1tect.tistory.com/132 by ccl(A) rewrite - 2021-09-12 16:01:25