题解 | #名字的漂亮度# 常用的字符串转化 词频算法
名字的漂亮度
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; } } }