对于给定的由小写字母和数字构成的字符串 ,统计出现的每一个字符的出现次数,按出现次数从多到少排序后依次输出。特别地,如果出现次数相同,则按照 ASCII 码由小到大排序。
在一行上输入一个长度为 、由小写字母和数字构成的字符串 。
在一行上输出一个字符串,代表按频次统计后的结果。
aaddccdc
cda
在这个样例中,字符 和 出现的次数最多,均为 次,而前者 ASCII 码更小,因此先输出。
importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;importjava.util.HashMap;importjava.util.Map;importjava.util.TreeMap;publicclassMain{publicstaticvoidmain(String[] args)throwsIOException{BufferedReader bf = newBufferedReader(newInputStreamReader(System.in));String str;while((str=bf.readLine()) != null){Map<Character, Integer> map = newHashMap<Character, Integer>();for(inti=0; i<str.length(); i++){charc = str.charAt(i);if(map.containsKey(c))map.put(c, map.get(c)+1);elsemap.put(c, 1);}Map<Integer,Character> res = newTreeMap<Integer,Character>();for(Character c: map.keySet()){res.put(map.get(c)*128+128-c, c);}StringBuffer sb = newStringBuffer();for(Integer i: res.keySet()){sb.append(res.get(i));}System.out.println(sb.reverse().toString());}bf.close();}}
#include <iostream> #include <string> #include <algorithm> #include <vector> #include <map> #include <cctype> using namespace std; int main() { string str; while (cin >> str) { map<char, int> mci; vector<string> vs; string temp = ""; int maxcount = 0; for (int i = 0; i < str.size(); i++) { if (isalpha(str[i]) || isalnum(str[i]) || isspace(str[i])) { mci[str[i]] = count(str.begin(),str.end(),str[i]); maxcount = (maxcount > mci[str[i]]) ? maxcount : mci[str[i]]; } } for (int i = maxcount; i > 0; i--) { map<char, int>::iterator it; for (it = mci.begin(); it != mci.end(); it++) { if (i == it -> second) { temp = temp + it -> first; } if (!temp.empty()) { sort(temp.begin(), temp.end()); vs.push_back(temp); temp = ""; } } } temp = ""; for (int i = 0; i < vs.size(); i++) { temp += vs[i]; } cout << temp << endl; } return 0; }
/*
字符统计:大小写字母,数字,空格,并按照出现的次数由大到小输出 思路:先数组统计出现的字符及个数,然后用multimap存储,输出时先存储到栈中在输出
以此来保证字符出现次数相同时按照ASCII顺序输出
@Author ZW
*/
#include<iostream>
#include<map>
#include<stack>
#include<string>
using namespace std;
int main() {
string input;
multimap<int,char> map_count;
stack<pair<int,char> > s_count;
while (getline(cin,input)) {
int flag[128] = { 0 };
map_count.clear();
for (int i = 0; input[i] != '\0'; i++) {
if (isalpha(input[i]) || isdigit(input[i]) || isspace(input[i]))
flag[(int)input[i]]++;
}
for (int j = 0; j < 128; j++)
{
if (flag[j] != 0)
map_count.insert(pair<int,char>(flag[j],(char)j));
}
while (!s_count.empty()) s_count.pop();
for (auto it = map_count.rbegin(); it != map_count.rend(); it++)
{
if(s_count.empty())
s_count.push(pair<int, char>(it->first, it->second));
else if(it->first == s_count.top().first)
s_count.push(pair<int, char>(it->first, it->second));
else {
while (!s_count.empty())
{
cout << s_count.top().second;
s_count.pop();
}
s_count.push(pair<int, char>(it->first, it->second));
}
}
while (!s_count.empty())
{
cout << s_count.top().second;
s_count.pop();
}
cout << endl;
}
return 0;
}
public static void main(String[] args){ Scanner in = new Scanner(System.in); while(in.hasNext()){ String str = in.next(); HashMap<Character,Integer> map = new HashMap<>(); for(int i = 0;i < str.length();i++){ if(map.containsKey(str.charAt(i))){ map.put(str.charAt(i),map.get(str.charAt(i)) + 1); }else{ map.put(str.charAt(i),1); } } Character[] arr = new Character[map.size()]; int i = 0; for(Character c : map.keySet()){ arr[i++] = c; } Arrays.sort(arr,new Comparator<Character>(){ public int compare(Character o1,Character o2){ if(map.get(o1) == map.get(o2)){ return o1 - o2; }else{ return map.get(o2) - map.get(o1); } } }); StringBuilder res = new StringBuilder(); for(int j = 0;j < arr.length;j++){ res.append(arr[j]); } System.out.println(res); } }
#include<iostream> #include<string> #include<map> #include<vector> #include<algorithm> using namespace std; int main(){ string str; while(cin>>str){ map<char, int>m; string res; for(int i = 0; i < str.size(); i++){ m[str[i]]++; } map<char,int>::iterator it = m.begin(); map<int, string> m2; while(it!=m.end()){ m2[it->second].push_back(it->first); ++it; } map<int, string>::iterator it2 = m2.begin(); while(it2!=m2.end()){ sort(it2->second.begin(), it2->second.end(),greater<char>()); res += it2->second; ++it2; } reverse(res.begin(),res.end()); cout<<res<<endl; } return 0; }
}
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner (System.in); String str = scanner.next(); // 0-9,对应ASCII码值48-57;a-z,对应ASCII码值97-122 char[] arr1 = str.toCharArray(); int[] arr2 = new int[10 + 26]; // 数字加上小写字母一共只有36个 int flag = 150; // 题里要求按照ASCII码由小到大排序,所以需要定义一个常数(大于122且小于1048均可)用来保证升序排列 String s = ""; // 遍历字符串,对数组2进行填充 for (int i = 0; i < arr1.length; i++) { // 通过字符的ASCII码值找到对应下标,然后开始计数 // 除第一次外,每次加1000,这样可以保证根据次数递增,且对1000取余后可还原ASCII码值 if (arr1[i] < 58) { arr2[arr1[i] - 48] += arr2[arr1[i] - 48] == 0 ? flag - arr1[i] : 1000; } else { arr2[arr1[i] - 87] += arr2[arr1[i] - 87] == 0 ? flag - arr1[i] : 1000; } } // 数组2排序 Arrays.sort(arr2); // 排序以后是升序,题里要求按照个数降序,所以从后向前遍历即可 for (int i = arr2.length - 1; i >= 0; i--) { if (arr2[i] == 0) break; s = s + (char) (flag - arr2[i] % 1000); } System.out.println(s); } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNextLine()) { String line = sc.nextLine(); order(line); } } private static void order(String str) { HashMap<Character,Integer> map = new HashMap<>(); char[] arr = str.toCharArray(); for (int i = 0; i < arr.length; i++) { map.put(arr[i], map.getOrDefault(arr[i], 0) + 1); } ArrayList<Map.Entry<Character,Integer>> list = new ArrayList<>(map.entrySet()); list.sort((o1, o2) -> { int i = o2.getValue() - o1.getValue(); if (i == 0) { return o1.getKey() - o2.getKey(); } return i; }); StringBuilder sb = new StringBuilder(); for (int i = 0; i < list.size(); i++) { sb.append(list.get(i).getKey()); } System.out.println(sb); } }
C 一直嵌套 觉得不错点个赞 #define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> int main() { char arr[1001]={0}; int cnt[128]={0}; int snt[128]={0}; int i,j; scanf("%s",arr); int len=strlen(arr); for(i=0;i<len;i++) { cnt[arr[i]]++; } for(i=0;i<128;i++) { if(cnt[i]!=0) snt[cnt[i]]=1; } for(i=len;i>0;i--) { if(snt[i]==1) { for(j='0';j<='z';j++) { if(cnt[j]==i) printf("%c",j); } } } return 0; }
#include<bits/stdc++.h> using namespace std; bool cmp(pair<char,int> a, pair<char,int> b){ return a.second>b.second; } bool cmp1(pair<char,int> a, pair<char,int> b){ return a.first<b.first; } int main(){ string s; while(cin>>s){ vector<int> table(128); for(int i=0;i<s.size();i++){ table[s[i]]++; } vector<pair<char,int>> count; set<char> myset(s.begin(),s.end()); for(auto it=myset.begin();it!=myset.end();it++){ count.emplace_back(*it,table[*it]); } sort(count.begin(),count.end(),cmp); int i = 0; while(i<count.size()){ int start = i; while(count[i].second==count[i+1].second){ i++; } i++; sort(count.begin()+start,count.begin()+i,cmp1); } for(int i=0;i<count.size();i++){ cout<<count[i].first; } cout<<endl; } return 0; }
while True: try: s=list(input()) ls=list(set(s)) lc=[] for i in ls: lc.append(s.count(i)) dic={i:j for i,j in zip(ls,lc)} ls.sort() while True: for i in ls: if dic[i]==max(lc): print(i,end='') ls.remove(i) lc.remove(dic[i]) break if lc==[]: break print() except: break
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ String s = sc.nextLine(); //统计字符 int[] arr = new int[36]; for(int i=0; i<s.length(); i++){ char c=s.charAt(i); if(c>='a' &&c<='z'){//字母 arr[c-'a']++; }else{//数字 arr[c-'0'+26]++; } } //统计结果存入List集合 List<Node> list = new ArrayList<>(); for(int i=0; i<36; i++){ if(arr[i]!=0){ if(i<26){//字母 list.add(new Node((char)('a'+i), arr[i])); }else{//数字 list.add(new Node((char)('0'+i-26), arr[i])); } } } //排序 Collections.sort(list, new Comparator<Node>(){ public int compare(Node a, Node b){ if(a.v!=b.v){//根据值排序 return b.v-a.v; }else{//值相等根据ASCII码排序 return a.k-b.k; } } }); //输出结果 for(int i=0; i<list.size(); i++){ System.out.print(list.get(i).k); } System.out.println(); } } } class Node{ char k; int v; public Node(char k, int v){ this.k=k; this.v=v; } }
#include <iostream> #include <vector> #include <unordered_map> #include <algorithm> using namespace std; bool compare(const pair<char,int> & lhs, const pair<char,int> & rhs) { if(lhs.second == rhs.second) { return lhs.first < rhs.first; } else { return lhs.second > rhs.second; } } int main() { string s; while(cin >> s) { unordered_map<char,int> m; vector<pair<char,int>> v; string ans; for(auto & c : s) { ++m[c]; } for(auto it = m.begin(); it != m.end(); ++it) { v.push_back(*it); } sort(v.begin(), v.end(), compare); for(auto & elem : v) { ans += elem.first; } cout << ans << endl; } return 0; }
#include<iostream> #include<string> #include<vector> #include<map> using namespace std; int main(){ string input; while(getline(cin,input)){ vector<vector<char> >res(input.size()+1); map<char,int> m; for(int i=0;i<input.size();++i){ if('a'<=input[i]&&input[i]<='z'||'0'<=input[i]&&input[i]<='9') ++m[input[i]]; } for(auto itor=m.begin();itor!=m.end();++itor){ res[itor->second].push_back(itor->first); } for(int i=res.size()-1;i>=0;--i){ for(int j=0;j<res[i].size();++j){ cout<<res[i][j]; } } cout<<endl; } return 0; }
public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()) { String input = sc.nextLine(); Map<Character,Integer> map = new HashMap<>(); // 1.放入hashmap,统计频率 for (int i = 0; i < input.length(); i ++) { if (!map.containsKey(input.charAt(i))) { map.put(input.charAt(i), 1); } else { int freq = map.get(input.charAt(i)) + 1; map.put(input.charAt(i), freq); } }// 2.HashMap转成ArrayList List<Entry<Character, Integer>> list = new ArrayList<Map.Entry<Character,Integer>>(map.entrySet()); // 3.改写sort中compare()方法Collections.sort(list, new Comparator<Entry<Character, Integer>>(){ public int compare(Entry<Character, Integer> e1, Entry<Character, Integer> e2) { // 1)个数大的首先交换位置 if (e2.getValue() > e1.getValue()) { return 1; } else if (e1.getValue() == e2.getValue()) {// 2)个数一样,对比ASCII码顺序 return e1.getKey() - e2.getKey(); } else {// 3) 个数小的,不交换位置 return -1; } } });// 打印ArrayList排序后的结果 for (Entry<Character, Integer> entry : list) { System.out.print(entry.getKey()); } System.out.println(); } sc.close(); }
while True: try: str1 = input()#读取字符串 str2 = list(str1)#转换list转存入str2 str2 = set (str2)#去除重复项 str2 = list(str2)#变回list a=[] for i in range (len(str2)):#计算每一项个数 num =str1.count(str2[i]) a.append((ord(str2[i]),num))#以(asc码,count个数)的形式添加入a里(a为list)用ord()转换为ASCII码 a.sort(key=lambda x:x[0])#先ASCII 码升序排列 a.sort(key=lambda x:x[1],reverse=True)#再出现次数降序排列,遇到相同的排序会预设按输入次序也就是上行ASCII的sort排序顺序 str3="" for i in range (len(a)): str3 = str3+str(chr(a[i][0])) #用chr()换回字母数字,打印结果 print(str3) except: break
# 思路:关键在于两次排序:给出现次数不同的字符按次数排序(次数由多——少) # 给出现次数相同的字符按ascii大小来排序(小_大) while True: try: s = input().strip() dic = {} for ch in s: if ch in dic: dic[ch] += 1 else: dic[ch] = 1 dic = sorted(dic.items(), key=lambda x: x[0]) # 先按字符ASC排 dic = sorted(dic, key=lambda x: x[1], reverse=True) # 再按统计数目排 print(''.join([k for (k, v) in dic])) except: break
def sort(s): if len(s) == 1: return s for i in range(len(s)-1): for j in range(i+1, len(s)): if s[i]>s[j]: s[j],s[i] = s[i],s[j] return s def process(s): tmp = [[] for _ in range(len(s)+1)] count= {} for i in s: cnt = 0 if i in count: cnt = count[i] tmp[cnt] = [o for o in tmp[cnt] if o != i] cnt+=1 count[i] = cnt tmp[cnt] = tmp[cnt]+[i] res='' for i in tmp: res=''.join(sort(i))+res return res while True: try: print(process(raw_input())) except: break
用索引的思路做的