题解 | #名字的漂亮度#
名字的漂亮度
http://www.nowcoder.com/practice/02cb8d3597cf416d9f6ae1b9ddc4fde3
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); while(n > 0){ n--; String s = in.next(); int ans = 0; //统计s中各字符的词频 词频越高的漂亮滴越高 int[] arr = new int[26]; for(int i = 0; i < s.length(); i++){ char ch = Character.toLowerCase(s.charAt(i)); arr[ch-'a']++; if(i == s.length()-1){ Arrays.sort(arr); for(int j = 0; j < arr.length; j++){ ans += (j+1) * arr[j]; } } } System.out.println(ans); for(int j = 0; j < arr.length; j++){//清空统计数组 arr[j] = 0; } } } }