每个测试文件均包含多组测试数据。第一行输入一个整数
代表数据组数,每组测试数据描述如下:
在一行上输入一个长度为
、仅由小写字母构成的字符串
。
对于每一组测试数据,输出一个整数,表示字符串的最大“漂亮度”。
2 zhangsan lisi
192 101
对于第一组测试数据,其中一种最优的分配方案是:
将字符
的漂亮度分配为
;
将字符
的漂亮度分配为
;
将字符
的漂亮度依次分配为
;
其余字符随意分配;
最终,得到字符串的“漂亮度”为
。
对于第二组测试数据,其中一种最优的分配方案是:
将字符
的漂亮度分配为
;
将字符
的漂亮度分配为
;
将字符
的漂亮度分配为
;
其余字符随意分配;
最终,得到字符串的“漂亮度”为
。
import java.util.Arrays;
import java.util.Scanner;
import java.util.TreeMap;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = Integer.valueOf(in.nextLine()) ;
while(in.hasNextLine()){
String str = in.nextLine();
// System.out.println(str);
TreeMap<Character, Integer> map = new TreeMap<>();
char[] ch = str.toCharArray();
int count =0;
for(int i=0; i<ch.length; i++){
map.put(ch[i], map.getOrDefault(ch[i], 0)+1);
}
int[] chs = new int[map.size()];
int j =0;
for(int k : map.values()){
chs[j] = k;
j++;
}
Arrays.sort(chs);
int k =26;
for(int i=chs.length-1; i>=0; i--){
count = chs[i]*k+count;
// System.out.println(chs[i]*k);
k--;
}
System.out.println(count);
// System.out.println();
}
}
} 用sum保存控制台输入的数据组数,注意nextInt不消耗换行符,所以需要用nextLine消耗换行符,从第一组数据开始遍历,定义一个长度为26的数组用来保存26个字母出现的次数,随后给数组从小到大排序,从后到前遍历数组,知道数组元素为0,用26 * 出现次数最多的元素,后随之递减即可7.17
import java.util.Scanner;
import java.util.Arrays;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
int sum = in.nextInt();
in.nextLine();
for (int i = 0; i < sum; i++){
String str = in.nextLine();
int[] charcount = new int[26];
for (int j = 0; j < str.length(); j++){
charcount[str.charAt(j) - 'a']++;
}
Arrays.sort(charcount);
int maxtomin = 26;
int res = 0;
for (int k = charcount.length - 1; k >= 0 && charcount[k] != 0; k--){
res += charcount[k] * maxtomin;
maxtomin--;
}
System.out.println(res);
}
}
}
} import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while (t-- > 0) {
String s = in.next();
// 字母key, 出现次数value
Map<Character, Integer> map = new HashMap<>();
for (char c : s.toCharArray()) {
if (map.get(c) == null) {
map.put(c, 1);
} else {
map.put(c, map.get(c) + 1);
}
}
// 排序:按value次数逆序
List<Map.Entry<Character, Integer>> list = new ArrayList<>(map.entrySet());
Collections.sort(list, (o1, o2)-> {return o2.getValue().compareTo(o1.getValue());});
// value越大, 分配数字越大
int d = 26, res = 0;
for (Map.Entry<Character, Integer> entry : list) {
res += (entry.getValue() * (d--));
}
System.out.println(res);
}
}
} import java.util.Scanner;
import java.util.HashMap;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
for (int i = 0; i < n; i++) {
HashMap<Character, Integer> m = new HashMap<>();
char[] ch = in.next().toCharArray();
for (int j = 0; j < ch.length; j++) {
if (m.containsKey(ch[j])) {
m.put(ch[j], m.get(ch[j])+1);
} else {
m.put(ch[j], 1);
}
}
int[] l = m.values().stream().sorted((a,b)->b-a).mapToInt(Integer::intValue).toArray();
int sum = 0;
for (int k = 0; k < l.length; k++) {
sum += (26-k)*l[k];
}
System.out.println(sum);
}
}
}
import java.util.*;
// 本题的思路在于将出现次数最多的字母分配最高值,因此分两步,统计字母出现次数,然后将其排序
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
int n = in.nextInt();
in.nextLine();
for (int i = 0; i < n; i++) {
String s = in.nextLine();
System.out.println(solve(s));
}
}
}
public static int solve(String s) {
int[] count = new int[26]; // 统计每个字母的出现顺序
for (int i = 0; i < s.length(); i++) {
int ch = s.charAt(i) - 'a';
count[ch]++;
}
Arrays.sort(count); //将每个字母出现次数由小到大排序
int ans = 0;
for (int i = 26; i > 0; i--) { //从出现次数最多的字母*最大权值,依次递减
if (count[i - 1] == 0) break;
ans += i * count[i - 1];
}
return ans;
}
} import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNext()) { // 注意 while 处理多个 case
int num = in.nextInt();
in.nextLine();
String[] strArr = new String[num];
for (int i = 0; i < num; i++) {
strArr[i] = in.nextLine();
}
for (int i = 0; i < strArr.length; i++) {
Map<Character, Integer> map = new HashMap<>();
char[] charArray = strArr[i].toCharArray();
for (char c : charArray) {
if (!map.containsKey(c)) {
map.put(c, 1);
} else {
map.put(c, map.get(c) + 1);
}
}
List<Integer> list = new ArrayList<>();
for (Map.Entry<Character, Integer> entry : map.entrySet()) {
list.add(entry.getValue());
}
//降序排列单词个数
list.sort((o1, o2) -> o2 - o1);
//遍历输出
int totalBeautiful = 0;
for (int j = 0; j < map.size(); j++) {
totalBeautiful += (26 - j) * list.get(j);
}
System.out.println(totalBeautiful);
}
}
}
} import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = Integer.parseInt(in.nextLine());
HashMap<Character, Pair> map = null;
for (int i = 0; i < n; i++) {
String s = in.nextLine();
map = new HashMap<>();
for (int j = 0; j < s.length(); j++) {
char c = s.charAt(j);
if (map.containsKey(c)) {
map.get(c).add();
} else {
map.put(c, new Pair(c));
}
}
ArrayList<Pair> list = new ArrayList<>(map.values());
Collections.sort(list);
int count = 0;
int currentWeight = 26;
for (Pair p : list) {
count += p.count * currentWeight;
currentWeight--;
}
System.out.println(count);
}
}
}
class Pair implements Comparable<Pair> {
char c;
int count;
public Pair(char c) {
this.c = c;
this.count = 1;
}
public Pair(char c, int count) {
this.c = c;
this.count = count;
}
public Pair add(){
this.count++;
return this;
}
public int compareTo(Pair p) {
if (this.count == p.count) {
return this.c - p.c;
}
return p.count - this.count;
}
} import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
int num = Integer.valueOf(in.nextLine());
for(int i= 0;i< num;i++){
String s = in.nextLine();
Map<Character,Integer> map = new HashMap<>();
for(int j = 0;j< s.length();j++){
char c = s.charAt(j);
map.put(c,map.getOrDefault(c,0)+1);
}
List<Integer> sortList = new ArrayList();
for(Map.Entry<Character,Integer> entrySet: map.entrySet()){
sortList.add(entrySet.getValue());
}
//降序排序
Collections.sort(sortList,Collections.reverseOrder());
int count = 26;
int res = 0;
for(Integer sort : sortList){
res += sort*count;
count--;
}
System.out.println(res);
}
}
}
} import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
in.nextLine();
while (n-- > 0) {
char [] chars = in.nextLine().toCharArray();
int [] a = new int[26];
for (int i = 0; i < chars.length; i++) {
a[chars[i] - 'a']++;
}
Arrays.sort(a);
int sum = 0;
for (int i = 0; i < a.length; i++) {
sum += a[i] * (i + 1);
}
System.out.println(sum);
}
}
} import java.util.Scanner;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
int n = in.nextInt();
in.nextLine();
for (int i = 0; i < n; i++) {
String str = in.nextLine();
Map<Character, Integer> map = new HashMap<>();
for (char c : str.toCharArray()) {
map.put(c, map.getOrDefault(c, 0)+1);
}
Collection<Integer> c = map.values();
Object[] arr = c.toArray();
Arrays.sort(arr, Collections.reverseOrder());
int val = 0;
int init = 26;
for (Object o : arr) {
val += (int)o * init;
init--;
}
System.out.println(val);
}
}
}
} import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
// 输入的字符串个数
int n = Integer.parseInt(line);
ArrayList<String> list = new ArrayList<>();
// 存储输入的字符串
for (int i = 0; i < n; i++) {
list.add(br.readLine());
}
for (int i = 0; i < list.size(); i++) {
// 计算每个字符串中每个字符出现的次数
HashMap<Character, Integer> map = new HashMap<>();
String s = list.get(i);
for (int j = 0; j < s.length(); j++) {
char c = s.charAt(j);
map.put(c, map.getOrDefault(c, 0) + 1);
}
// 排序
Collection<Integer> values = map.values();
ArrayList<Integer> list1 = new ArrayList<>(values);
// 字符出现次数排序,降序排列
Collections.sort(list1, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2.compareTo(o1);
}
});
// 计算漂亮分
int sum = 0;
int score = 26;
for (int j = 0; j < list1.size(); j++) {
sum += score * list1.get(j);
score--;
}
System.out.println(sum);
}
}
} import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
//思路;用HashMap保存每个字母出现的次数,然后对其根据次数从大到小进行漂亮度从大到小进行累计
//最后将每行字符串的真正情况保存到result
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
int n=Integer.valueOf(in.nextLine());
ArrayList<String> list=new ArrayList<>();
for(int i=0;i<n;i++){
list.add(in.nextLine());
}
//保存计算结果
ArrayList<Integer> result=new ArrayList<>();
for(int i=0;i<list.size();i++){
//保存出现的次数
HashMap<Character,Integer> map=new HashMap<>();
//遍历添加当前行字符串的字符出现次数添加到map
for(int j=0;j<list.get(i).length();j++){
Character c=list.get(i).charAt(j);
if(map.get(c)==null||map.get(c)==0){
map.put(c,1);
}else{
map.put(c,map.get(c)+1);
}
}
//获取漂亮度
result.add(getResult(map));
}
for(int i=0;i<result.size();i++){
System.out.println(result.get(i));
}
}
public static int getResult(HashMap<Character,Integer> map){
//将每个字符出现次数保存到list,进行排序,然后根据次数对漂亮都进行计算
ArrayList<Integer> list=new ArrayList<>();
for(Character c:map.keySet()){
int sum=map.get(c);
list.add(sum);
}
//排序:顺序
Collections.sort(list);
int sum=0;
int index=list.size()-1;
//计算漂亮都
for(int i=26;i>26-list.size();i--){
sum+=i*list.get(index);
index--;
}
return sum;
}
} import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNext 的区别
while (in.hasNext()) { // 注意 while 处理多个 case
int total = in.nextInt();
for (int i = 0; i < total; i++) {
String s = in.next();
// 全部转换成小写
s = s.toLowerCase();
// 定义一个字母数组,
int[] letters = new int[26];
char[] arr = s.toCharArray();
for (int k = 0; k < arr.length; k++) {
letters[arr[k] - 'a'] += 1;
}
Arrays.sort(letters);
int res = 0;
int curScore = 26;
for (int k = 25; k >= 0; k--) {
if (letters[k] != 0) {
res += letters[k] * curScore;
curScore--;
}
}
System.out.println(res);
}
}
}
} import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
int num = Integer.parseInt(in.nextLine());
for (int i = 0; i < num; i++) {
String str = in.nextLine();
int[] a = new int[26];
int j = 0;
while (!"".equals(str)) {
String temp = str.replace(String.valueOf(str.charAt(0)), "");
a[j] = str.length() - temp.length();
str = temp;
j++;
}
Arrays.sort(a);
int beautiful = 0;
for (int k = a.length - 1; k >= 0; k--) {
beautiful += a[k] * (k + 1);
}
System.out.println(beautiful);
}
}
}