-
5658. [모의 SW 역량테스트] 보물상자 비밀번호코딩/알고리즘 2019. 4. 12. 23:04
솔직히 다른사람코드보다 훨씬 어렵게 풀은거같다...
...
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Deque; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.StringTokenizer; public class Solution { static HashMap<String, Integer> map = new HashMap<String, Integer>(); static Deque q = new LinkedList(); static Deque tempQ = new LinkedList(); static int T; static int N; static int K; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine(),""); T = Integer.parseInt(st.nextToken()); for(int i = 0 ; i < T; i++) { tempQ.clear(); map.clear(); st = new StringTokenizer(br.readLine()," "); while(st.hasMoreTokens()) { N = Integer.parseInt(st.nextToken()); K = Integer.parseInt(st.nextToken()); } //Q add String[] arr = br.readLine().split(""); for(int j = 0 ; j < arr.length; j++) { q.add(arr[j]); tempQ.add(arr[j]); } makeOct(i+1); } } public static void makeOct(int testCase) { int cnt = N / 4; int idx = 0 ; for(int d = 0; d < cnt; d++) { StringBuilder st = new StringBuilder(); //복사 Deque q = new LinkedList(); Iterator it = tempQ.iterator(); while(it.hasNext()) { q.add(it.next()); } while(!q.isEmpty()) { String popStr = q.pop(); st.append(popStr); idx += 1; //컨버팅 if( idx == cnt ) { String str = st.toString(); int number = converting(str); map.put(str, number); idx = 0; st.delete(0, st.length()); } } //전환 String last = tempQ.pollLast(); tempQ.addFirst(last); } Iterator it = sortByValue(map).iterator(); int count = 0; while(it.hasNext()) { String temp = (String) it.next(); if(count == K-1) { System.out.println("#"+testCase +" "+map.get(temp)); } count+=1; } } //별도의 스태틱 함수로 구현 public static ArrayList sortByValue(HashMap map) { ArrayList list = new ArrayList(); list.addAll(map.keySet()); Collections.sort(list,new Comparator() { @Override public int compare(Object o1, Object o2) { Object v1 = map.get(o1); Object v2 = map.get(o2); return ((Comparable) v2).compareTo(v1); } }); //Collections.reverse(list); // 주석시 오름차순 return list; } //8진수 10진수 전환 public static Integer converting(String str) { return Integer.parseInt(str,16); } }
'코딩 > 알고리즘' 카테고리의 다른 글
[ 백준 ] [ 자바 ][ 숨박꼭질] 1697 (0) 2019.06.16 [백준][자바][17144] 미세먼지 안녕! (0) 2019.04.21 [ 백준 ][ 자바][ 16234 ] 인구이동 (0) 2019.04.05 [ 백준 [[ 자바 ][ 6603 ] 로또 (0) 2019.03.23 [백준][ 자바 ][ 15683 ] 감시 (0) 2019.03.23