给出一个字符串,该字符串仅由小写字母组成,定义这个字符串的“漂亮度”是其所有字母“漂亮度”的总和。
每个字母都有一个“漂亮度”,范围在1到26之间。没有任何两个不同字母拥有相同的“漂亮度”。字母忽略大小写。
给出多个字符串,计算每个字符串最大可能的“漂亮度”。
本题含有多组数据。
数据范围:输入的名字长度满足
给出一个字符串,该字符串仅由小写字母组成,定义这个字符串的“漂亮度”是其所有字母“漂亮度”的总和。
每个字母都有一个“漂亮度”,范围在1到26之间。没有任何两个不同字母拥有相同的“漂亮度”。字母忽略大小写。
第一行一个整数N,接下来N行每行一个字符串
每个字符串可能的最大漂亮程度
2 zhangsan lisi
192 101
对于样例lisi,让i的漂亮度为26,l的漂亮度为25,s的漂亮度为24,lisi的漂亮度为25+26+24+26=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; }
# str转换成list def str2list(s): l=[v for v in s] return l def beautiful_degree(s): l=str2list(s)#['l','i','s','i'] ans=0 dic={} for i in l: if i in dic: dic[i]+=1 else: dic[i]=1 # dic={'l':1,'i':2,'s':1} lst=sorted(dic.items(), key=lambda x: x[1], reverse=True) # 按字典的value排序(倒序) # lst=[(i,2),(s,1),(l,1)] abc = [k for k in range(26, 0, -1)]#[26,25,24....] for j in range(len(lst)): ans += lst[j][1]*abc[j] return ans while True: try: n=int(input())# 多少个名字 li=[] for i in range(n): name=input() li.append(name) #li=['zhangsan','lisi'] for j in range(len(li)): print(beautiful_degree(li[j])) except: break很多人题意看不明白,其实就是出现次数最多的字母漂亮度是26,第二多的是25...以此类推。最后累计加和,这样整个单词的漂亮度最高,附代码。
#include <bits/stdc++.h> using namespace std; int count(string str){ int result = 0; map<char,int> helper; for(auto ch:str) ++helper[ch]; vector<pair<char,int> > vec(helper.begin(),helper.end()); stable_sort(vec.begin(), vec.end(), [](const pair<char,int>& p1, const pair<char,int>& p2) {return p1.second > p2.second;}); for(int i=0;i < vec.size();i++) result += (26-i)*vec[i].second; return result; } int main(){ int n;string name; while(cin >> n) for(int i=0;i < n;++i){ cin >> name; cout << count(name) << endl; } return 0; }
while True: try: times = int(input().strip()) for i in range(times): name = input().strip() char_dict = {} for key in name: if key in char_dict.keys(): char_dict[key] += 1 else: char_dict[key] = 1 value_list = [] for key in char_dict.keys(): value_list.append(char_dict[key]) beauty_value = 26 total_value = 0 for num in sorted(value_list, reverse=True): total_value += beauty_value * num beauty_value -= 1 print(total_value) except: break找出每个字母,形成列表,按照出现的次数非升序排列。然后最大的给26权重,次之的给25权重,以此类推,直至遍历完列表,得出总漂亮值。
#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); } } } }