코딩/알고리즘
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);
}
}