时间限制:10000ms
单点时限:1000ms
内存限制:256MB
描述
Given a string S and an integer K, your task is to find the lexicographically smallest string T which satisfies:
1. T is a subsequence of S
2. The length of T is K.
输入
The first line contain an integer K. (1 <= K <= 100000)
The second line contains a string of lowercase letters. The length of S is no more than 100000.
输出
The string T.
样例输入
4
cacbbac
样例输出
abac
package Daily;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;
public class Main_230 {
public static void main(String [] args){
Scanner scanner = new Scanner(System.in);
int T = scanner.nextInt();
String s = scanner.next();
PriorityQueue<Node> queue = new PriorityQueue<>(Math.max(s.length() - T, 1), new Comparator<Node>() {
@Override
public int compare(Node o1, Node o2) {
if(o1.c == o2.c) return o1.index - o2.index;
return o1.c - o2.c;
//return 0;
}
public boolean equals(Object obj){
return false;
}
});
for(int i =0; i< s.length() -T ; i ++){
queue.add(new Node(s.charAt(i) , i));
}
int ind = -1;
int len = s.length() -T;
while(T -- != 0){
queue.add(new Node(s.charAt(len),len ++));
Node node;
do{
node = queue.remove();
}while(node.index <= ind);
ind = node.index;
System.out.println(node.c);
}
}
static class Node{
private char c;
private int index;
public Node(char c, int index){
this.c = c;
this.index = index;
}
}
}