Java 代码
import java.util.LinkedList;
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
String num = input.nextLine();
int k = input.nextInt();
if(k == num.length()){
System.out.println(0);return;
}
LinkedList<Integer> stack = new LinkedList<>();
int cnt = 0;
for(int i = 0 ; i < num.length() ; i++){
while(!stack.isEmpty() && cnt < k && stack.peek() > (num.charAt(i) - '0')){
cnt++;
stack.pop();
}
stack.push(num.charAt(i) - '0');
}
while(cnt < k){
stack.pop();cnt++;
}
StringBuilder sb = new StringBuilder();
while(!stack.isEmpty()){
sb.append(stack.pop());
}
String res = sb.reverse().toString();
int i = 0;
for(; i < res.length() ; i++){
if(res.charAt(i) != '0')break;
}
System.out.println(res.substring(Math.min(i,res.length() - 1)));
}
}
厉害啊 采用了单调栈写法 我怎么想不到呢
nb
妙