#include <stdio.h> #include <string.h> #include <stdlib.h> #define MAXLEN 100001 char *removeDuplicateLetter(char *str) { int map[26], len = (int) strlen(str); char *res = (char *) malloc(len + 1); memset(map, 0, sizeof(int) * 26); for (int i = 0; i < len; i++) { map[str[i] - 'a']++; } int l = 0, r = 0, index = 0; while (r < len) { if (map[str[r] - 'a'] == -1 || --map[str[r] - 'a'] > 0) { r++; continue; } int pick = -1; for (int i = l; i <= r; i++) { if (map[str[i] - 'a'] != -1 && (pick == -1 || str[i] < str[pick])) pick = i; } res[index++] = str[pick]; for (int i = pick + 1; i <= r; i++) { if (map[str[i] - 'a'] != -1) map[str[i] - 'a']++; } map[str[pick] - 'a'] = -1; l = pick + 1; r = l; } res[index] = '\0'; return res; } int main(void) { char str[MAXLEN], *res; scanf("%s", str); res = removeDuplicateLetter(str); printf("%s", res); free(res); return 0; }
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.*; // 注意类名必须为 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(); } }
# 寻找字符串中字典序最小的字符 def getChar(string): string_length = len(string) if string_length == 0: return '' else: result = string[0] for i in range(1, string_length): if string[i] < result: result = string[i] return result # 构建字符串的词频表 def getCharCount(string): result = {} for i in string: if i not in result.keys(): result[i] = 1 else: result[i] += 1 return result # 删除掉字符串中某个指定的字符 def deleteChar(string, char): result = '' for i in string: if i == char: continue else: result += i return str(result) # 输入字符串 string = input() # 获取字符串词频分布表 char_count = getCharCount(string) char_count_length = len(char_count.keys()) result = [] while len(result) != char_count_length: # 当前遍历位置 i = 0 # 当前字符串长度 string_length = len(string) # 遍历字符串 for i in range(string_length): char_count[string[i]] -= 1 # 需要进行选择的位置 if char_count[string[i]] == 0: break # 如果当前位置是字符串最后一个位置 if i + 1 == string_length: result_char = getChar(string) result.append(result_char) string = deleteChar(string, result_char) else: result_char = getChar(string[:i + 1]) result.append(result_char) string = deleteChar(string[i:], result_char) # 重新构建词频表 char_count = getCharCount(string) print(''.join(result))
#include<bits/stdc++.h> using namespace std; int main() { string str; set<char> set; while (cin >> str) { for (char c : str) { set.emplace(c); } } for (auto iter = set.begin(); iter != set.end(); iter++) { cout << *iter; } }
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); } }
if __name__ == '__main__': test_data = input() # test_data = 'dbcacbca' output = '' visual = [0, ] * 256 record = [0, ] * 256 # build record for vl in test_data: record[ord(vl)] = record[ord(vl)] + 1 # filter for vl in test_data: record[ord(vl)] = record[ord(vl)] - 1 if visual[ord(vl)] == 1: continue while len(output) > 0 and output[-1] > vl and record[ord(output[-1])] > 0: visual[ord(output[-1])] = 0 output = output[:-1] output = output + vl visual[ord(vl)] = 1 print(output)
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("-", ""); }
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner scanner = new Scanner(System.in); while(scanner.hasNext()){ String input = scanner.next(); System.out.println(del(input)); } } public static String del(String input){ char[] str = input.toCharArray(); int[] map = new int[26]; for(int i=0;i<str.length;i++){ // 字母出现次数统计,记得要减去'a' map[str[i] - 'a']++; } // 结果 char[] res = new char[26]; // res的目前最后一位 int index = 0; int l = 0; int r = 0; while(r != str.length){ // 若此字符已挑选过,或是后面还会出现,则跳过 if(map[str[r] - 'a'] == -1 || --map[str[r] - 'a']>0){ r++; }else{ // 挑选出字典序最小的字符,遍历一遍从左到右依次比较即可 int pick = -1; for(int i=l;i<=r;i++){ if(map[str[i] - 'a'] != -1 && (pick == -1 || str[i]<str[pick])){ pick = i; } } // 把挑选结果放入结果 res[index++] = str[pick]; // 把后面的还要考虑的字符数加回来 for(int i=pick+1;i<=r;i++){ if(map[str[i] - 'a'] != -1){ map[str[i] - 'a']++; } } // 标记为已挑选过的字符 map[str[pick] - 'a'] = -1; l = pick+1; r = pick+1; } } return String.valueOf(res); } }