通过键盘输入一串小写字母 (a~z) 组成的字符串。
请编写一个字符串归一化程序,统计字符串中相同字符出现的次数,并按字典序输出字符及其出现次数。
例如字符串"babcc"归一化后为"a1b2c2"
数据范围:输入的字符串长度满足 ,保证输入中仅包含小写的英文字母
每个测试用例每行为一个字符串,以'\n'结尾,例如cccddecca
输出压缩后的字符串ac5d2e
dabcab
a2b2c1d1
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<chars.length;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); } } } }
#include<iostream> #include<string> using namespace std; int main(){ int count[26] = {0}; //记录每个字母的个数 string s; cin >> s; for(int i = 0; i < s.length(); i++){ count[s[i]-'a']++; } char c = 'a'; for(int i = 0; i < 26; i++){ if(count[i]){ cout << c << count[i]; } c++; } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s; while(cin>>s) { sort(s.begin(),s.end()); string res=""; int n=1; cout<<s[0]; for(int i=1;i<s.size();i++) { if(s[i]==s[i-1]) n++; else { cout<<n; cout<<s[i]; n=1; } } cout<<n<<endl; } return 0; }
#include <iostream> #include <cstdio> #include <map> #include <string> using namespace std; int main(){ char ch; map<char,int> m; while(cin>>ch){ m[ch]++; } map<char,int>::iterator it; for(it=m.begin();it!=m.end();it++){ cout<<it->first<<it->second; }cout<<endl; return 0; }
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()); } }
#include<iostream> 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<<p<<num[i]; } ++i; } cout<<endl; return 0; }
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()); } }
""" 使用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)
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]);
}
}
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='') # 输出不换行
#include <iostream> using namespace std; int main() { string s, res; cin >> s; int a[26] = {}; for(char c : s) a[c - 'a']++; for(int i = 0; i < 26; i++){ if(a[i] > 0){ res += i + 'a'; string temp; while(a[i]){ char t = a[i] % 10 + '0'; temp = t + temp; a[i] /= 10; } res += temp; } } cout << res; return 0; }