AcWing4664.字符统计——学习笔记

目录

题目

代码

AC结果

思路:

一、获取数据

二、建立字典

三、计数

四、输出


题目

4664. 字符统计 - AcWing题库https://www.acwing.com/problem/content/4667/


代码


import java.util.HashMap;
import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        //获取数据
        Scanner input = new Scanner(System.in);
        String str = input.nextLine();
        char[] ch = str.toCharArray();
        //建立字典
        HashMap<Character,Integer> getNum = new HashMap<Character, Integer>();
        getNum.put('A',0);
        getNum.put('B',1);
        getNum.put('C',2);
        getNum.put('D',3);
        getNum.put('E',4);
        getNum.put('F',5);
        getNum.put('G',6);
        getNum.put('H',7);
        getNum.put('I',8);
        getNum.put('J',9);
        getNum.put('K',10);
        getNum.put('L',11);
        getNum.put('M',12);
        getNum.put('N',13);
        getNum.put('O',14);
        getNum.put('P',15);
        getNum.put('Q',16);
        getNum.put('R',17);
        getNum.put('S',18);
        getNum.put('T',19);
        getNum.put('U',20);
        getNum.put('V',21);
        getNum.put('W',22);
        getNum.put('X',23);
        getNum.put('Y',24);
        getNum.put('Z',25);

        HashMap<Integer,String> getLet = new HashMap< Integer,String>();
        getLet.put(0,"A");
        getLet.put(1,"B");
        getLet.put(2,"C");
        getLet.put(3,"D");
        getLet.put(4,"E");
        getLet.put(5,"F");
        getLet.put(6,"G");
        getLet.put(7,"H");
        getLet.put(8,"I");
        getLet.put(9,"J");
        getLet.put(10,"K");
        getLet.put(11,"L");
        getLet.put(12,"M");
        getLet.put(13,"N");
        getLet.put(14,"O");
        getLet.put(15,"P");
        getLet.put(16,"Q");
        getLet.put(17,"R");
        getLet.put(18,"S");
        getLet.put(19,"T");
        getLet.put(20,"U");
        getLet.put(21,"V");
        getLet.put(22,"W");
        getLet.put(23,"X");
        getLet.put(24,"Y");
        getLet.put(25,"Z");

        //计数
        int[] appear = new int[26];
        int max = 0;
        for(int i = 0; i < ch.length; i++){
            int index = getNum.get(ch[i]);
            appear[index] += 1;
            if(appear[index] >= max){
                max = appear[index];
            }
        }
        //输出
        StringBuilder ans = new StringBuilder();
        for(int i = 0; i < appear.length; i++){
            if(appear[i] == max){
                ans.append(getLet.get(i));
            }
        }
        System.out.println(ans.toString());

    }
}

AC结果


思路:

这是一道统计题。需要两个字典,一个是通过【字母】找字母的【序号】,一个是通过字母的【序号】找到对应的【字母】。获取输入数据,然后每个字母遍历,某个字母每出现一次就记录下来,并用max变量记录当前出现过最多次数是多少次。当遍历完成时,可知出现过最多次数是多少次,然后再遍历appear,appear的值若为max则以为着当前位置对应的字母是需要输出的,则将其添加到ans中。

一、获取数据

        //获取数据
        Scanner input = new Scanner(System.in);
        String str = input.nextLine();
        char[] ch = str.toCharArray();

获取题目输入的数据,并将其转为char数组,方便后续遍历。

二、建立字典

        //建立字典
        HashMap<Character,Integer> getNum = new HashMap<Character, Integer>();
        getNum.put('A',0);
        getNum.put('B',1);
        getNum.put('C',2);
        getNum.put('D',3);
        getNum.put('E',4);
        getNum.put('F',5);
        getNum.put('G',6);
        getNum.put('H',7);
        getNum.put('I',8);
        getNum.put('J',9);
        getNum.put('K',10);
        getNum.put('L',11);
        getNum.put('M',12);
        getNum.put('N',13);
        getNum.put('O',14);
        getNum.put('P',15);
        getNum.put('Q',16);
        getNum.put('R',17);
        getNum.put('S',18);
        getNum.put('T',19);
        getNum.put('U',20);
        getNum.put('V',21);
        getNum.put('W',22);
        getNum.put('X',23);
        getNum.put('Y',24);
        getNum.put('Z',25);

        HashMap<Integer,String> getLet = new HashMap< Integer,String>();
        getLet.put(0,"A");
        getLet.put(1,"B");
        getLet.put(2,"C");
        getLet.put(3,"D");
        getLet.put(4,"E");
        getLet.put(5,"F");
        getLet.put(6,"G");
        getLet.put(7,"H");
        getLet.put(8,"I");
        getLet.put(9,"J");
        getLet.put(10,"K");
        getLet.put(11,"L");
        getLet.put(12,"M");
        getLet.put(13,"N");
        getLet.put(14,"O");
        getLet.put(15,"P");
        getLet.put(16,"Q");
        getLet.put(17,"R");
        getLet.put(18,"S");
        getLet.put(19,"T");
        getLet.put(20,"U");
        getLet.put(21,"V");
        getLet.put(22,"W");
        getLet.put(23,"X");
        getLet.put(24,"Y");
        getLet.put(25,"Z");

此处有两个字典,一个(getNum)是通过【字母】找出该字母在26个字母排序中的【序号】,另外一个字典(getLet)则相反,通过其在26个字母排序的【序号】找回对应的【字母】。

三、计数

        //计数
        int[] appear = new int[26];
        int max = 0;
        for(int i = 0; i < ch.length; i++){
            int index = getNum.get(ch[i]);
            appear[index] += 1;
            if(appear[index] >= max){
                max = appear[index];
            }
        }

appear数组有26个位置,每一个位置表示对应字母出现过的次数。max表示出现过最多次的次数。读到某个字母,便让该字母出现次数+1。若该字母的出现次数已经超过max,则更新max的值。

四、输出

        //输出
        StringBuilder ans = new StringBuilder();
        for(int i = 0; i < appear.length; i++){
            if(appear[i] == max){
                ans.append(getLet.get(i));
            }
        }
        System.out.println(ans.toString());

题目要求:输出出现过次数最多的字母,若有多个字母,则按字母表顺序依次输出。只需要根据appear中记录各个字母出现过的次数即可判断某个字母需不需要输出。遍历appear,当appear的值与max相等,则根据getLet字典找到对应的字母加到ans中。最后将ans转为String类型输出。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hokachi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值