题解 | #最大子序列#
最大子序列
http://www.nowcoder.com/practice/17ba5b5df1fc49ca8d6cf8ea407b1972
import java.util.Arrays; import java.util.Scanner; /** * BD12 最大子序列 * @Author zhuyq * @Date 2021-09-17 */ public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println(f(in.nextLine(), "")); } //获取字符串字典序最大的子串 public static String f(String s, String t){ //获取字符串最大字典序的字符 char[] chars = s.toCharArray(); Arrays.sort(chars); char maxChar = chars[chars.length-1]; int pos = s.indexOf(String.valueOf(maxChar)); t += maxChar; if(pos==s.length()-1){ return t; } else{ s = s.substring(pos+1, s.length()); return f(s, t); } } }