对于给定由小写字母构成的字符串,定义字符串的“漂亮度”为该字符串中所有字母“漂亮度”的总和。
每一个字母的“漂亮度”将由你来确定,具体规则如下:
每一个字母的“漂亮度”为 到 之间的整数;
没有两个字母的“漂亮度”相同。
现在,你需要确定每个字母的“漂亮度”,以使得字符串的“漂亮度”最大。
每个测试文件均包含多组测试数据。第一行输入一个整数 代表数据组数,每组测试数据描述如下:
在一行上输入一个长度为 、仅由小写字母构成的字符串 。
对于每一组测试数据,输出一个整数,表示字符串的最大“漂亮度”。
2 zhangsan lisi
192 101
对于第一组测试数据,其中一种最优的分配方案是:
将字符 的漂亮度分配为 ;
将字符 的漂亮度分配为 ;
将字符 的漂亮度依次分配为 ;
其余字符随意分配;
最终,得到字符串的“漂亮度”为 。
对于第二组测试数据,其中一种最优的分配方案是:
将字符 的漂亮度分配为 ;
将字符 的漂亮度分配为 ;
将字符 的漂亮度分配为 ;
其余字符随意分配;
最终,得到字符串的“漂亮度”为 。
//朋友们这就是一个大根堆的问题啊,统计字母个数存在大根堆,然后ans += top*(26-i); #include<iostream> #include<string> #include<queue> using namespace std; string nameBeauty[1001]; int aphaCout(string& rhs); int main(){ int _N; while(cin>>_N){ for(int i=0;i<_N;i++){ cin>>nameBeauty[i]; } for(int i=0;i<_N;i++){ cout<<aphaCout(nameBeauty[i])<<endl; } } return 0; } int aphaCout(string& rhs){ priority_queue<int> mypq; int aph[26] = {0}; int ans = 0; for(int i=0;i<rhs.size();i++){ ++aph[rhs[i]-'a']; } for(int i=0;i<26;i++){ if(aph[i] != 0 ){ mypq.push(aph[i]); } } int _size = mypq.size(); for(int i=0;i<_size;i++){ ans += (26-i) * mypq.top(); mypq.pop(); } return ans; }
#include <iostream> #include <algorithm> #include <map> #include <vector> #include <string> using namespace std; bool isBigger(const pair<int, int> &lhs, const pair<int, int> &rhs) { return lhs.second > rhs.second; } int main() { int test; while (cin >> test) { while (test--) { string st; cin >> st; int i, k = 26, res = 0; map<int, int> m; for (i = 0; i < st.length(); ++i) { if (st[i] >= 'a' && st[i] <= 'z') m[st[i] - 'a']++; else m[st[i] - 'A']++; } vector<pair<int, int> > pvec(m.begin(), m.end()); sort(pvec.begin(), pvec.end(), isBigger); //int size = pvec.size(); for (i = 0; i <= pvec.size() - 1; i++) res += pvec[i].second * k--; cout << res << endl; } } return 0; }
#include <iostream> #include <vector> #include <algorithm> #include <string> using namespace std; void Beauty(string str) { int alpha[26] = {0}; for (int i = 0; i < str.length(); i++) { if (str[i] >= 'a' && str[i] <= 'z') { alpha[str[i] - 97] ++; } else if (str[i] >= 'A' && str[i] <= 'Z') { alpha[str[i] - 65] ++; } } sort(alpha, alpha+26); int sum = 0; for (int i = 26; i > 0; i--) { sum += alpha[i-1] * i; } cout << sum << endl; } int main() { int num; while (cin >> num) { while (num--) { string tmp; cin >> tmp; Beauty(tmp); } } return 0; }
//#include "stdafx.h"
package main import ( "bufio" "fmt" "os" "sort" ) func main() { scanner := bufio.NewScanner(os.Stdin) scanner.Scan() for scanner.Scan() { input := scanner.Text() if input == "" { return } charMap := map[byte]int{} for i := 0; i < len(input); i++ { charMap[input[i]]++ } beautyList := []int{} for _, v := range charMap { beautyList = append(beautyList, v) } sort.Ints(beautyList) power := 26 res := 0 for i := len(beautyList) - 1; i >= 0; i-- { res += beautyList[i] * power power-- } fmt.Println(res) } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int n = scanner.nextInt(); for (int i=0; i<n; i++) { String str = scanner.next(); // map集合统计字符 Map<Character, Integer> map = new TreeMap<>(); char c; for (int j=0; j<str.length(); j++) { c = str.charAt(j); if (!map.containsKey(c)) { map.put(c, 1); } else { map.put(c, map.get(c) + 1); } } // 提取数据为数组,并排序 int[] nums = new int[map.size()]; int index = 0; for (Map.Entry<Character, Integer> entry : map.entrySet()) { nums[index] = entry.getValue(); index ++; } Arrays.sort(nums); // 计算最大的漂亮度 int beautiful = 26; int sum = 0; for (int k=nums.length-1 ; k >= 0; k--) { sum += beautiful * nums[k]; beautiful --; } System.out.println(sum); } } } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); in.nextLine();//跳到第二行 for(int i=0;i<n;i++){ System.out.println(getScore(in.nextLine())); } } public static int getScore(String name){ int score = 0; char[] cha = name.toCharArray(); int[] count = new int[26]; for(int i=0;i<cha.length;i++){ count[Character.toLowerCase(cha[i]) - 'a']++;//统计每个字母出现的次数,忽略大小写 } Arrays.sort(count);//升序排列 for(int i=1;i<=26;i++){//计算漂亮度 score += i * count[i-1]; } return score; } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int beauty=0; while(sc.hasNext()){ int n = sc.nextInt(); for(int i=0; i<n; i++){ beauty = getBeauty(sc.next()); System.out.println(beauty); } } } private static int getBeauty(String name){ if(name==null || name.length()==0) return 0; //统计单词 int[] arr = new int[26]; name = name.toUpperCase(); for(int i=0; i<name.length(); i++){ arr[name.charAt(i)-'A']++; } //排序统计 Arrays.sort(arr); int beauty=0, temp=26; for(int i=arr.length-1; i>=0; i--){ if(arr[i]==0) break; beauty += arr[i]*temp; temp--; } return beauty; } }
public static void main(String[] args){ Scanner in = new Scanner(System.in); while(in.hasNext()){ int num = in.nextInt(); in.nextLine(); for(int i = 0;i < num;i++){ String str = in.nextLine(); HashMap<Character,Integer> map = new HashMap<>(); for(int m = 0;m < str.length();m++){ char c = str.charAt(m); if(map.containsKey(c)){ map.put(c,map.get(c) + 1); }else{ map.put(c,1); } } PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(); for(Character key : map.keySet()){ priorityQueue.offer(map.get(key)); } int sum = 0; while(!priorityQueue.isEmpty()){ sum += priorityQueue.poll() * (26 - priorityQueue.size()); } System.out.println(sum); } } }
from collections import defaultdict while True: try: n = int(input()) for i in range(n): name = input().strip() counter = defaultdict(lambda: 0) for c in name: counter[c] += 1 sorted_tuple = sorted(counter.items(), key=lambda x: -x[1]) # 按字符的计数降序排列 max_beautiful, char_beautiful = 0, 26 for _, cnt in sorted_tuple: max_beautiful += cnt * char_beautiful char_beautiful -= 1 print(max_beautiful) except: break
import java.util.*; public class Main{ public static void main(String[] args){ Scanner scanner = new Scanner(System.in); while (scanner.hasNext()){ int num = scanner.nextInt(); for (int i = 0; i < num; i++){ int value = 26; int totalBeauty = 0; List<String> list = Arrays.asList(scanner.next().split("")); Map<String, Integer> alpFreq = new HashMap<>(); for (int j = 0; j < list.size(); j++){ if (alpFreq.containsKey(list.get(j).toLowerCase())){ alpFreq.put(list.get(j).toLowerCase(),alpFreq.get(list.get(j).toLowerCase())+1); }else{ alpFreq.put(list.get(j).toLowerCase(),1); } } List<Map.Entry<String, Integer>> sortedList = new ArrayList<>(alpFreq.entrySet()); Collections.sort(sortedList, new Comparator<Map.Entry<String, Integer>>(){ public int compare(Map.Entry<String, Integer> entry1, Map.Entry<String, Integer> entry2){ return entry2.getValue() - entry1.getValue(); } }); Iterator<Map.Entry<String, Integer>> iter = sortedList.iterator(); while (iter.hasNext()){ totalBeauty += alpFreq.get(iter.next().getKey())*value; value -= 1; } System.out.println(totalBeauty); } } } }
#include <stdio.h> #include <string.h> #include <ctype.h> int cmp(const void*a,const void*b) { return *(int*)b-*(int*)a; //大到小 } int func (char *a) { int len=strlen(a); int an[30]={0},t=0; for (int i=0;i<len;i++) { if (isupper(a[i])) a[i]=a[i]+32; } for (int i=0;i<len;i++) { int k=a[i]-'a'; an[k]++; } qsort(an,26,sizeof(int),cmp); for (int i=0;i<26;i++) t=t+an[i]*(26-i); return t; } int main () { int num; while (scanf("%d",&num)!=EOF) { int bn[1000]={0}; char str[3000] = {0}; for(int i=0; i<num; i++) { scanf("%s",str); bn[i]=func(str); } for (int i=0; i<num; i++) printf("%d\r\n",bn[i]); } }
#include <iostream> #include <string> #include <algorithm> int main() { using namespace std; string st; // int s[26] = {0}; int n,i; while(cin >> n) { while(n) { int s[26] = {0}; int k = 26,beauty = 0; cin >> st; for(i=0;i<st.size();i++) { if(st[i]>='A'&&st[i]<='Z') s[st[i]-'A']++; else s[st[i]-'a']++; } sort(s,s+26); for(i=25;i>=0;i--) { beauty += k*s[i]; k--; } cout << beauty << endl; n--; } } return 0; }
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Scanner; import java.util.TreeMap; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int n = sc.nextInt(); String[] str = new String[n]; for(int i=0;i<n;i++){ str[i] = sc.next(); } for(int i=0;i<str.length;i++){ String str2 = str[i]; TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>(); for(int j=0;j<str2.length();j++){ if(tm.containsKey(str2.charAt(j))){ tm.put(str2.charAt(j), tm.get(str2.charAt(j))+1); }else{ tm.put(str2.charAt(j), 1); } } List<Map.Entry<Character,Integer>> list = new ArrayList<Map.Entry<Character, Integer>>(tm.entrySet()); Collections.sort(list,new Comparator<Map.Entry<Character,Integer>>(){ @Override public int compare(Entry<Character, Integer> o1, Entry<Character, Integer> o2) { double q1 = o1.getValue(); double q2 = o2.getValue(); double p = q2-q1; if(p>0){ return 0; }else{ return -1; } } }); int sum=0; int[] arr = {26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1}; for(int k=0;k<list.size();k++){ sum+=list.get(k).getValue()*arr[k]; } System.out.println(sum); } } } }