Smallest Substring

 

时间限制: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;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值