import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 String s = in.nextLine(); System.out.println(deal(s)); } public static String deal(String s) { int n = s.length(); HashMap<Character, Integer> map = new HashMap<>(); boolean[] visited = new boolean[128]; char[] cs = s.toCharArray(); Stack<Character> stack = new Stack<>(); //统计数字 for (int i = 0; i < n; i++) { map.put(cs[i], map.getOrDefault(cs[i], 0) + 1); } for (int i = 0; i < n; i++) { char c = cs[i]; map.put(c, map.get(c) - 1); if (visited[c]) continue; //没访问过的 while (!stack.isEmpty() && stack.peek() > c && map.get(stack.peek()) > 0) { visited[stack.pop()] = false; } //当前字符计进入 stack.push(c); visited[c] = true; } StringBuilder sb = new StringBuilder(); while (!stack.isEmpty()) { sb.insert(0, stack.pop()); } return sb.toString(); } }
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = br.readLine(); System.out.println(remove(str)); } private static String remove(String str) { if(str == null || str.length() < 2) return str; int[] termFreq = new int[26]; for(int i = 0; i < str.length(); i++) termFreq[str.charAt(i) - 'a'] ++; int minAsciiIndex = 0; for(int i = 0; i < str.length(); i++){ if(--termFreq[str.charAt(i) - 'a'] == 0){ break; }else{ minAsciiIndex = str.charAt(i) < str.charAt(minAsciiIndex)? i: minAsciiIndex; } } String minAlpha = String.valueOf(str.charAt(minAsciiIndex)); return minAlpha + remove(str.substring(minAsciiIndex + 1).replaceAll(minAlpha, "")); } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); while(sc.hasNext()){ String str=sc.nextLine(); solve(str,""); } sc.close(); } public static void solve(String str,String res) { if(str.isEmpty()) { System.out.println(res); return; } String s=str.substring(0,1); if(res.contains(s)) { String tmp=res.replace(s, "").concat(s); if(tmp.compareTo(res)>0) { str=str.replace(s, ""); }else { res=tmp; str=str.substring(1); } }else { res=res.concat(s); str=str.substring(1); } solve(str,res); } }
public static String RemoveDup(String s){ if (s.length() < 2) { return s; } Map<Character,Integer> m = new HashMap<Character,Integer>(); char[] chs = s.toCharArray(); List<Integer> list = new ArrayList<Integer>(); int tmp = -1; ll: for(int i = chs.length-1; i >= 0; i--){ if (m.containsKey(chs[i])){ if (chs[i] >= tmp){ list.add(i); continue ll; }else{ list.add(m.get(chs[i])); m.put(chs[i], i); tmp = (int)chs[i]; continue ll; } } m.put(chs[i], i); tmp = chs[i]; } for (Integer num : list) { chs[num] = '-'; } String str = new String(chs); return str.replaceAll("-", ""); }