题解 | #名字的漂亮度#
名字的漂亮度
http://www.nowcoder.com/practice/02cb8d3597cf416d9f6ae1b9ddc4fde3
让出现次数越多的字符,权重越大即可。
import java.util.Scanner; import java.util.Arrays; public class Main { private final int N = 26; // public Main() { // } public int count(String name) { int[] arr = new int[N]; for (int i = 0; i < name.length(); i++) { char ch = name.charAt(i); arr[ch - 'a']++; } Arrays.sort(arr); int result = 0; for (int weight = N, i = N - 1; i >= 0; i--) { if (arr[i] != 0) { result += weight * arr[i]; weight--; } } return result; } public static void main(String[] args) { Main solution = new Main(); Scanner in = new Scanner(System.in); while (in.hasNextLine()) { int n = Integer.parseInt(in.nextLine()); for (int i = 0; i < n; i++) { String name = in.nextLine().toLowerCase(); int result = solution.count(name); System.out.println(result); } } } }