首页 > 试题广场 >

名字的漂亮度

[编程题]名字的漂亮度
  • 热度指数:154371 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
\hspace{15pt}对于给定由小写字母构成的字符串,定义字符串的“漂亮度”为该字符串中所有字母“漂亮度”的总和。
\hspace{15pt}每一个字母的“漂亮度”将由你来确定,具体规则如下:
\hspace{23pt}\bullet\,每一个字母的“漂亮度”为 126 之间的整数;
\hspace{23pt}\bullet\,没有两个字母的“漂亮度”相同。
\hspace{15pt}现在,你需要确定每个字母的“漂亮度”,以使得字符串的“漂亮度”最大。

输入描述:
\hspace{15pt}每个测试文件均包含多组测试数据。第一行输入一个整数 T\left(1\leqq T\leqq 10\right) 代表数据组数,每组测试数据描述如下:

\hspace{15pt}在一行上输入一个长度为 1 \leqq {\rm len}(s) \leqq 10^4 、仅由小写字母构成的字符串 s


输出描述:
\hspace{15pt}对于每一组测试数据,输出一个整数,表示字符串的最大“漂亮度”。
示例1

输入

2
zhangsan
lisi

输出

192
101

说明

\hspace{15pt}对于第一组测试数据,其中一种最优的分配方案是:
\hspace{23pt}\bullet\,将字符 \texttt{`a'} 的漂亮度分配为 26
\hspace{23pt}\bullet\,将字符 \texttt{`n'} 的漂亮度分配为 25
\hspace{23pt}\bullet\,将字符 \texttt{`g'}, \texttt{`z'}, \texttt{`h'}, \texttt{`s'} 的漂亮度依次分配为 24 \sim 21
\hspace{23pt}\bullet\,其余字符随意分配;
\hspace{15pt}最终,得到字符串的“漂亮度”为 (26 + 25) \times 2 + (24 + 23 + 22 + 21) = 192

\hspace{15pt}对于第二组测试数据,其中一种最优的分配方案是:
\hspace{23pt}\bullet\,将字符 \texttt{`i'} 的漂亮度分配为 26
\hspace{23pt}\bullet\,将字符 \texttt{`l'} 的漂亮度分配为 25
\hspace{23pt}\bullet\,将字符 \texttt{`s'} 的漂亮度分配为 24
\hspace{23pt}\bullet\,其余字符随意分配;
\hspace{15pt}最终,得到字符串的“漂亮度”为 26 \times 2 + (25 + 24) = 101
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);
        }
    }
}

发表于 2025-01-27 19:37:02 回复(0)
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);
        }
    }
}
发表于 2024-09-27 14:43:54 回复(0)
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 a = in.nextInt();
            in.nextLine();
            for (int i = 0; i < a; i++) {
                String s = in.nextLine();
                System.out.println(toResult(s));
            }
        }
    }
    public static int toResult(String s) {
        int result = 0;
        int[] count = new int[26];
        for (char c : s.toCharArray()) {
            c = Character.toLowerCase(c);
            count[c - 'a']++;
        }
        Arrays.sort(count);
        for (int j = 1; j <= 26; j++) {
            if (count[j - 1] > 0) {
                result += j * count[j - 1];
            }
        }
        return result;
    }

}
发表于 2024-09-15 19:08:01 回复(0)
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;
    }
}

发表于 2024-07-13 20:47:46 回复(0)
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);
            }
        }
    }
}

发表于 2024-07-06 17:12:56 回复(0)
留言,实在不会写……
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;
    }
}


发表于 2024-06-23 18:48:38 回复(0)
import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        //默认26个字符累计,排序后对26个字符分别赋值计算,累计最终结果
        int n = in.nextInt();
        String[] str = new String[n];
        int[][] a = new int[n][26];
        int[] b = new int[n];
        for(int i=0;i<n;i++){
            str[i]=in.next();
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<str[i].length();j++){
              a[i][str[i].charAt(j)-97] = a[i][str[i].charAt(j)-97]+1;
            }
            Arrays.sort(a[i]);
        }
        for(int i=0;i<n;i++){
            int t = 1;
            for(int j=0;j<26;j++){
                b[i]=b[i]+a[i][j]*t;
                t++;
            }
        }
        for(int i=0;i<n;i++){
            System.out.println(b[i]);
        }
    }
}
编辑于 2024-04-23 11:50:37 回复(0)
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);
            }

        }
    }
}

编辑于 2024-01-22 14:25:57 回复(0)
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);
        }
    }
}

发表于 2023-11-24 22:14:24 回复(0)
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);
            }
        }
    }
}

发表于 2023-11-10 10:48:48 回复(0)
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);

        }
    }
}

发表于 2023-08-10 08:45:24 回复(0)
import java.util.Arrays;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
            int n=in.nextInt();
            int index=0;
            for(int i=0;i<n;i++)
            {
                String str=in.next();
                int count=26;
                int [] core=new int[128];
                for(int j=0;j<str.length();j++)
                {
                    core[str.charAt(j)]++;
                }
                Arrays.sort(core);
                for(int x=127;x>0;x--)
                {
                    if(core[x]==0)break;
                    index+=count*core[x];
                    count--;  
                }
                System.out.println(index);
                index=0;

            }

    }
}
//有没有可能数组就可以了
发表于 2023-07-10 16:54:23 回复(0)
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;
    }
}

发表于 2023-05-30 10:41:30 回复(0)
public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            for (int i = 0; i < n; i++) {
                String str = in.next();
                char[] strs = str.toCharArray();
                TreeMap<Character, Integer> treeMap = new TreeMap<Character, Integer>();
                for (int j = 0; j < strs.length; j++) {
                    if (treeMap.containsKey(strs[j])) {
                        treeMap.put(strs[j],treeMap.get(strs[j])+1);
                    }else{
                        treeMap.put(strs[j],1);
                    }
                }
   
                List<Map.Entry<Character,Integer>> list = new ArrayList<Map.Entry<Character,Integer>>();
                list.addAll(treeMap.entrySet());
                list.sort(new Comparator<Map.Entry<Character,Integer>>(){
                    @Override
                    public int compare(Map.Entry<Character,Integer> o1,Map.Entry<Character,Integer> o2){
                    return o2.getValue() - o1.getValue();
                    }
                });
     
                int total = 0;
                int index = 26;
                for(int j=0;j<list.size();j++){
                   int num = list.get(j).getValue();//从多到少的字母个数
                   total += num*index;
                   index--;
                }
                System.out.println(total);
            }        
        }
    }
发表于 2023-04-19 00:43:14 回复(0)
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);
            }
        }
    }
}

发表于 2023-03-19 16:40:16 回复(0)
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);
        }


    }
}

发表于 2023-03-05 14:22:18 回复(0)
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 单词个数
        int count = in.nextInt();
        for (int i = 0; i < count; i++) {
            countBeautiful(in.next());
        }
    }

    private static void countBeautiful(String inputStr) {
        // 统计字符串个数
        List<Integer> countList = new ArrayList<>();
        char[] chars = inputStr.toCharArray();
        int startLen = inputStr.length();
        Map<Character, Boolean> map = new HashMap<Character, Boolean>();
        for (char c : chars) {
            int replaceLen = inputStr.replace(String.valueOf(c), "").length();
            int countLen = startLen - replaceLen;
            if (countLen != 0 && !map.containsKey(c)) {
                // 为了防止单个字母重复统计
                map.put(c, true);
                countList.add(countLen);
            }
        }
        // 降序排列
        Collections.sort(countList, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
        int beautiful = 0;
        for (int i = 0; i < countList.size(); i++) {
            // 最多的个数字符串漂亮度为26,其他依次递减
            beautiful += (26 - i) * countList.get(i);
        }
        System.out.println(beautiful);
    }
}

发表于 2023-02-15 22:47:36 回复(0)
理解错了题目意思,还以为分配完字符漂亮度之后,采用统一的漂亮度保证第一行字符串和第N行字符串同时都取得最大漂亮度
发表于 2023-02-13 18:40:43 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int a = in.nextInt();
        for (int i = 0; i < a; i++) {
            List<Character> chars = new LinkedList<>();
            List<Integer> ints = new LinkedList<>();
            String b = in.next();
            b = b.toLowerCase();
            for (int j = 0; j < b.length(); j++) {
                if (chars.contains(b.charAt(j))) {
                    int index = chars.indexOf(b.charAt(j));
                    int c = ints.get(index);
                    c++;
                    ints.set(index, c);
                    continue;
                }
                chars.add(b.charAt(j));
                ints.add(1);
            }
            Collections.sort(ints);
            int d = 26;
            int total = 0;
            for (int z = ints.size() - 1; z >= 0; z--) {
                total += (ints.get(z) * (d--));
            }
            System.out.println(total);
        }

    }
}

发表于 2023-01-07 01:22:25 回复(0)