本题使用递归可以求解,每一次找出字典序最大的字母,,再从当前位置进行,再从index+1继续递归处理到字符串末尾即可 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader reader=new BufferedReader(new InputStreamReader(System.in)); String str; while((str=reader.readLine())!=null){ StringBuffer sb=new StringBuffer(); dfs(sb,0,str,str.charAt(0)); System.out.println(new String(sb)); } } public static void dfs(StringBuffer sb,int index,String str,char c){ for (int i = index+1; i <str.length() ; i++) { if(str.charAt(i)>c){ c=str.charAt(i); index=i; } } sb.append(c); if(index<str.length()-1) dfs(sb,index+1,str,str.charAt(index+1)); } }