【编程题目 |100分】字符统计及重排【2021 H1, H2 考试题】


时间限制:C/C++ 1秒,其他语言 2秒

空间限制:C/C++262144K,其他语言524288K

64bit IO Format:%lld


本题可使用本地IDE编码,不能使用本地已有代码,无跳出限制,

编码后请点击”保存并调试“按钮进行代码提交。


题目描述

【字符统计及重排】

  • 给出一个仅包含字母的字符串,不包含空格,统计字符串中各个字母(区分大小写)出现的次数,
  • 并按照字母出现次数从大到小的顺序。输出各个字母及其出现次数。
  • 如果次数相同,按照自然顺序进行排序,且小写字母在大写字母之前。

输入描述:

  • 输入一行,为一个仅包含字母的字符串。

输出描述:

  • 按照字母出现次数从大到小的顺序输出各个字母和字母次数,用英文分号分隔,注意末尾的分号;
  • 字母和次数间用英文冒号分隔。

示例1 输入输出示例仅供调试,后台判题数据一般不包含示例

输入

xyxyXX

输出

x:2;y:2;X:2;


代码实现


Golang


Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package main
import (
"fmt"
"sort"
"strings"
)
type strs struct {
name string
number int
}
type strsslice []strs
package main import ( "fmt" "sort" "strings" ) type strs struct { name string number int } type strsslice []strs
package main

import (
    "fmt"
    "sort"
    "strings"
)

type strs struct {
    name   string
    number int
}
type strsslice []strs

剩余50%内容,订阅会员后查看


隐藏内容

此处内容需要权限查看

  • 普通用户特权:11金币
  • 会员用户特权:免费
  • 永久会员用户特权:免费推荐
会员免费查看

C++


Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
string CharacterStatistics(string &s)
{
vector<int>array(256,0);
#include <bits/stdc++.h> using namespace std; class Solution { public: string CharacterStatistics(string &s) { vector<int>array(256,0);
#include <bits/stdc++.h>

using namespace std;

class Solution {
public:
    string CharacterStatistics(string &s)
    {
        vector<int>array(256,0);

剩余50%内容,订阅会员后查看


隐藏内容

此处内容需要权限查看

  • 普通用户特权:11金币
  • 会员用户特权:免费
  • 永久会员用户特权:免费推荐
会员免费查看

JAVA 解法一


Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
int[] a = new int[256];
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); int[] a = new int[256];
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        int[] a = new int[256];

剩余50%内容,订阅会员后查看


隐藏内容

此处内容需要权限查看

  • 普通用户特权:11金币
  • 会员用户特权:免费
  • 永久会员用户特权:免费推荐
会员免费查看

JAVA 解法二


Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.util.*;
public class Main {
public static void main(String[] args) {
List<Word> list = new ArrayList<>();
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
import java.util.*; public class Main { public static void main(String[] args) { List<Word> list = new ArrayList<>(); Scanner sc = new Scanner(System.in); String input = sc.nextLine();
import java.util.*;

public class Main {
    public static void main(String[] args) {
        List<Word> list = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();

剩余50%内容,订阅会员后查看


隐藏内容

此处内容需要权限查看

  • 普通用户特权:11金币
  • 会员用户特权:免费
  • 永久会员用户特权:免费推荐
会员免费查看

Python解法一


Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import functools
s = input()
res = []
resStr = ""
import functools s = input() res = [] resStr = ""
import functools

s = input()
res = []
resStr = ""

剩余50%内容,订阅会员后查看


隐藏内容

此处内容需要权限查看

  • 普通用户特权:11金币
  • 会员用户特权:免费
  • 永久会员用户特权:免费推荐
会员免费查看


Python解法二


Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import collections
while 1:
try:
nums = input()
nums = collections.Counter(nums)
import collections while 1: try: nums = input() nums = collections.Counter(nums)
import collections

while 1:
    try:
        nums = input()

        nums = collections.Counter(nums)

剩余50%内容,订阅会员后查看


隐藏内容

此处内容需要权限查看

  • 普通用户特权:11金币
  • 会员用户特权:免费
  • 永久会员用户特权:免费推荐
会员免费查看


JavaScript


Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let s = readline();
let map = new Map();
let s = readline(); let map = new Map();
let s = readline();  

let map = new Map();

剩余50%内容,订阅会员后查看


隐藏内容

此处内容需要权限查看

  • 普通用户特权:11金币
  • 会员用户特权:免费
  • 永久会员用户特权:免费推荐
会员免费查看