题解 | #名字的漂亮度# 常用的字符串转化 词频算法

名字的漂亮度

https://www.nowcoder.com/practice/02cb8d3597cf416d9f6ae1b9ddc4fde3

import java.util.Scanner;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Collections;
import java.io.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) throws IOException{
        //Scanner in = new Scanner(System.in);
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        // 注意 hasNext 和 hasNextLine 的区别
        String s1 = in.readLine();
        int n = Integer.valueOf(s1);
        //ArrayList<Integer> list = new ArrayList<>();
        
        //遍历每个字符串
        for (int i = 0; i < n; i++) {
            Integer[] cs1 = new Integer[26];
            int [] cs = new int[26];
            String str = in.readLine();
            char[] chars = str.toCharArray();
            int m = 26;
            for (int j = 0; j < chars.length; j++) {

                int index = Integer.valueOf(chars[j] - 'a');
                cs[index]++;
            }
            for(int l=0;l<26;l++){
                cs1[l] = cs[l];
            }
            Arrays.sort(cs1, Collections.reverseOrder());
            //System.out.print(Arrays.toString(cs1));

            int ans = 0;
            for (int k = 0; k < 26; k++) {
                if(cs1[k]==0){
                    break;
                }
                ans += m * cs1[k];
                m--;
            }
            System.out.println(ans);

        }
    }

    public static class mycomp implements Comparator<Integer> {

        @Override
        public int compare(Integer o1, Integer o2) {
            return o2 - o1;
        }
    }

}

全部评论

相关推荐

04-06 16:59
已编辑
河南工业大学 Java
牛牛牛的牛子:最好扔了,实在没有选择的选择
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务