728x90
728x90
메모리: 29,080 KB, 시간: 123 ms
사용 알고리즘: Trie
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Solution {
static class Dictionary {
char c;
int count;
LinkedList child;
Dictionary front;
Dictionary next;
Dictionary() {};
Dictionary(char c) {
this.c = c;
count = 0;
child = new LinkedList();
}
void add(char[] list, int s) {
count++;
if(s == list.length) return;
Dictionary d = child.get(list[s]);
d.add(list, s + 1);
}
ArrayList<Character> get(int k) {
if(k > count) return new ArrayList<>();
Dictionary d = child.head;
for (int i = 0; i < child.size; i++) {
d = d.next;
if(d.count < k) k -= d.count;
else break;
}
ArrayList<Character> ret = d.get(k);
ret.add(c);
return ret;
}
}
static class LinkedList {
Dictionary head;
int size;
LinkedList() {
head = new Dictionary();
size = 0;
}
Dictionary get(char c) {
Dictionary now = head;
for (int i = 0; i < size; i++) {
now = now.next;
if(now.c == c) break;
else if(now.c > c) {
size++;
Dictionary d = new Dictionary(c);
now.front.next = d;
d.front = now.front;
now.front = d;
d.next = now;
now = d;
break;
}
}
if(now.c != c) {
size++;
Dictionary d = new Dictionary(c);
now.next = d;
d.front = now;
now = d;
}
return now;
}
}
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
int K;
char[] string;
Dictionary root;
ArrayList<Character> ret;
StringBuilder answer;
for (int tc = 1; tc <= T; tc++) {
K = Integer.parseInt(br.readLine());
string = br.readLine().toCharArray();
if(string.length < K) {
System.out.println("#" + tc + " none");
}
root = new Dictionary('R');
for (int i = 0; i < string.length; i++) {
root.add(string, i);
}
ret = root.get(K);
answer = new StringBuilder();
answer.append("#" + tc + " ");
for (int i = ret.size() - 2; i >= 0; i--) {
answer.append(ret.get(i));
}
System.out.println(answer);
}
}
}
728x90
'Problem Solving > SWEA' 카테고리의 다른 글
[SW Expert Academy, SWEA 15941] 평행사변형 (java) (0) | 2024.02.20 |
---|---|
[SW Expert Academy, SWEA 1257] [S/W 문제해결 응용] 6일차 - K번째 문자열 (java) (0) | 2024.02.20 |
[SW Expert Academy, SWEA 7091] 은기의 아주 큰 그림 (java) (0) | 2024.02.04 |
[SW Expert Academy, SWEA 1249] 보급로 (java) (0) | 2024.01.24 |
[SW Expert Academy, SWEA 8898] 3차원 농부 (java) (0) | 2024.01.23 |