import java.util.*; 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); String str = in.nextLine(); int n = Integer.parseInt(str); while (in.hasNextLine()) { for (int i = 0; i < n; i++) { String name = in.nextLine().toLowerCase(); int result = solution.count(name); System.out.println(result); } } } } 这是修改后的代码,已经运行通过
点赞

相关推荐

球球别再泡了:坏,我单9要了14
点赞 评论 收藏
分享
牛客网
牛客企业服务