5
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
char[] chars=sc.nextLine().toCharArray();
//经验,一般是用hashmap来统计出现次数的,
//但本题是对字符统计次数,可以专门用一个counts数组来统计次数
int[] counts=new int[26];
for(int i=0;i
counts[chars[i]-'a']++;
}
for(int i=0;i<26;i++){
char c=(char)('a'+i);
int count=counts[i];
if(count==0){
continue;
}else{
System.out.print(c+""+count);
}
}
}
}
发表于 2019-08-05 21:59:53
回复(0)
4
string = input()
for i in sorted(list(set(string))):
item = string.count(i)
print(i + str(item), end = '')
发表于 2019-10-10 22:00:53
回复(2)
3
#include
using namespace std;
int main()
{
string s;
while(cin>>s)
{
sort(s.begin(),s.end());
string res="";
int n=1;
cout<
for(int i=1;i
{
if(s[i]==s[i-1])
n++;
else
{
cout<
cout<
n=1;
}
}
cout<
}
return 0;
}
发表于 2019-06-30 18:55:06
回复(0)
2
#include
#include
using namespace std;
int main(){
int count[26] = {0}; //记录每个字母的个数
string s;
cin >> s;
for(int i = 0; i
count[s[i]-'a']++;
}
char c = 'a';
for(int i = 0; i
if(count[i]){
cout <
}
c++;
}
cout <
return 0;
}
发表于 2019-09-03 23:03:31
回复(0)
3
#include
using namespace std;
int main(){
string s;
while(cin>>s){
int l = s.length();
int cnt[26] = {0};
for(int i=0;i
cnt[s[i]-'a']++;
for(int i=0;i<26;i++){
if(cnt[i]>=1)
cout<
}
cout<
}
return 0;
}
发表于 2019-07-07 22:55:35
回复(0)
1
#include
#include
#include
#include
using namespace std;
int main(){
char ch;
map m;
while(cin>>ch){
m[ch]++;
}
map::iterator it;
for(it=m.begin();it!=m.end();it++){
cout<first<second;
}cout<
return 0;
}
编辑于 2020-04-13 14:46:28
回复(0)
1
Java解法
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] count = new int[256];
String s = scanner.nextLine();
char[] array = s.toCharArray();
for (char c : array) {
count[c]++;
}
StringBuilder builder = new StringBuilder();
for (int i = 'a'; i <= 'z'; i++) {
if (count[i]!=0){
builder.append((char)i);
if (count[i]!=1)
builder.append(count[i]);
}
}
System.out.println(builder.toString());
}
}
发表于 2020-03-01 20:03:35
回复(0)
1
#include
using namespace std;
const int SIZE = 128;
int main()
{
int num[26] = {0}; //26个字母
char buf[SIZE]; //定义输入字符的buf大小
fgets(buf,SIZE,stdin); //键盘输入
int i = 0;
while(buf[i]!='\n') //循环检测相同的字母
{
++num[buf[i++]-'a']; //检测到相同的字母,++操作
if(i==SIZE) // 考虑输入太多的字母的条件
{
i = 0;
fgets(buf,SIZE,stdin);
}
}
i = 0;
while(i<26) //循环输出
{
if(num[i]!=0) //判断字母存在不,没有则不用输入
{
//printf("%c%d",i+'a',num[i]);
char p;
p = i+'a'; //按照字符的顺序输出
cout<
}
++i;
}
cout<
return 0;
}
发表于 2019-10-09 17:30:40
回复(0)
1
#include
#include
using namespace std;
int main()
{ int a[26] = { 0 }; string str; cin >> str; for (int i = 0; i < str.size(); i++) { a[str[i] - 'a']++; }
char ch = 'a';
for(int j = 0;j<26;j++)
{
if(a[j])
cout << ch << a[j];
ch++;
} return 0;
}
编辑于 2019-10-07 21:34:59
回复(0)
1
#include
#include
#include
using namespace std;
int main(){
string s;
getline(cin,s);
map m;//map自动排序
for(int i=0;i
m[s[i]]++;
}
auto it=m.begin();
while(it!=m.end()){
cout<first<second;
it++;
}
return 0;
}
发表于 2019-08-23 20:02:20
回复(2)
1
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Solution6_字符串归一化 {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String str = bf.readLine();
//存储每个字母出现的次数,0~a,1~b
int[] nums = new int[26];
for (int i = 0; i < str.length(); i++) {
nums[str.charAt(i) - 'a']++;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 26; i++) {
if (nums[i] > 0) sb.append((char) (i + 'a')).append(nums[i]);
}
System.out.println(sb.toString());
}
}
发表于 2019-08-03 19:57:30
回复(0)
1
"""
使用Counter计数器
"""
import sys
import collections
if __name__ == "__main__":
# sys.stdin = open("input.txt", "r")
s = input().strip()
obj = collections.Counter(s)
d = sorted(obj.items(), key=lambda c: c[0])
ans = ""
for i in range(len(d)):
ans += str(d[i][0]) + str(d[i][1])
print(ans)
发表于 2019-07-06 21:25:13
回复(0)
1
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String str = sc.nextLine();
int [] array=new int [26];
for (int i = 0; i < str.length(); i++)
array[str.charAt(i)-'a']++;
for (int i = 0; i < 26; i++)
{
if(array[i]>=1)
System.out.print((char)('a'+i));
if(array[i]>1)
System.out.print(array[i]);
}
}
}
编辑于 2019-07-11 17:00:37
回复(0)
1
fromcollections importCounter
s =input()
c =Counter(s)
ans =''
fork insorted(c.keys()):
ans +=k
ans +=str(c[k])
print(ans)
发表于 2019-03-30 14:30:40
回复(1)
1
n = input()
n_dict = {}
for i in n:
if i not in n_dict: # 若dict中不包含此元素
n_dict.update({i: 1}) # 将元素以 {元素:出现次数} 的格式,添加到dict中
else:
n_dict[i] += 1 # 若存在元素,则将对应元素的出现次数+1
# 格式化输出
res_list = []
for i in n_dict:
res_list.append(str(i) + str(n_dict[i]))
res_list.sort()
for i in res_list:
print(i, end='') # 输出不换行
发表于 2019-09-20 15:52:28
回复(0)
0
//TreeMap存储
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
while(scan.hasNext()){
String str = scan.nextLine();
StringBuilder result = new StringBuilder();
TreeMap map = new TreeMap<>();
for(char ch : str.toCharArray()){
if(map.containsKey(ch)){
int count = map.get(ch);
map.put(ch,++count);
}else{
map.put(ch,1);
}
}
for(Map.Entry entry : map.entrySet()){
result.append(entry.getKey());
result.append(entry.getValue());
}
System.out.print(result);
}
}
}
发表于 2020-08-15 15:15:58
回复(0)
0
import sys
messages = sys.stdin.readline().strip().split()
in_str = messages[0]
res = {}
for i in set(in_str):
res[i] = in_str.count(i)
final_str = ''
final_res = sorted(res.items(),key = lambda x:x[0])
for k,v in final_res:
final_str+=k
final_str+=str(v)
print(final_str)
发表于 2020-06-09 11:41:42
回复(0)
0
import java.util.*;
public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
char[] chars = scanner.next().toCharArray();
int[] counts = new int[26];
for (int i=0; i
counts[chars[i] - 'a'] ++;
}
for (int j=0; j
char c = (char) (j + 'a');
int count = counts[j];
if (count != 0){
System.out.print(c+""+count);
}
}
}
}
发表于 2020-05-19 20:50:10
回复(0)
0
用一个数字记录即可
#include
(720)#include
#include
using namespace std;
int main(){
int ch[255]={0};//作标记
char cc[100005];
scanf("%s",cc);
for(int i=0;i
ch[cc[i]]++;
}
for(int i='a';i<='z';i++){
if(ch[i])printf("%c%d",i,ch[i]);
}
printf("\n");
return 0;
}
发表于 2020-05-17 13:42:27
回复(0)