字符串的完美度
题目详情:
我们要给每个字母配一个1-26之间的整数,具体怎么分配由你决定,但不同字母的完美度不同,
而一个字符串的完美度等于它里面所有字母的完美度之和,且不在乎字母大小写,也就是说字母F和f的完美度是一样的。
现在给定一个字符串,输出它的最大可能的完美度。
例如:dad,你可以将26分配给d,25分配给a,这样整个字符串最大可能的完美度为77。
函数头部
C
int perfect(const char *s);
C++
int perfect(const string &s);
java
public static int perfect(String s);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 取反
{
class Program
{
static void Main(string[] args)
{
string str = "JKazUEjLPrDimtFeFOZovjrFWKAZalYMPHJcJOXMItqWqRzwHcGfghlRTMrYqvVCdBskRvwnpLjiTQaVVRlbKDuqQkIjsiJHoMjNEGtBXcUGBubDewFYtHzosbyftICKmsKjoRqIkzOSmFiGaNATDZmFQzepyoSknhUYGdzeGFLZBCxuToqgtaGmomgUtIPHtnGkyjxxiJmKYtBzdmmcZfrsMvObuaDTYablCjENLFkOwLvCcnRikIlTHxiPwrvuodhyJZGkcIfLpVLsUxqOTpiZGqhyEiziccOnGNRztDrJybzjEOsOOdWZXqijWpcLYphkRErWQOfRIfkRAOMPxDhcOEdAEgukYfsYMNgovZuXUDbFRhfUjZUukLhnhQipfZIIaCBpzSewFownOMvJrxNFCtUTkufGGLiWkwzVLebIYpgYPEPCJSswmIyhHAaELIMdgZQODYOlVBTPYjBQNPgcEMLHhlcGqmktLLqewkAtpivEYvFASYaLljlbKpWwozfYFKAJUXcYBWZDhhYwyZCHNJkKisSguSlkcmgWTUHYfesNRJXgnDFVzgNRUhlxQSopCFLhhzOMHGeugGGNNrZmUDeytfAiIWwPPwKVMGckPrvqWnRsKxEaTbyNPQHhHoRFzpVIYNBvKUwfLITtIdWXPMDHAKoTAqrDtKYaspbYrTcVeHbCuUpyDuJnjMRiXePfdMUvkzRdYyJIXfvapIXgSuKzZhHkVBjmRCoDDwkyGFlbPtkXxqjPcFsrGcfRUIcLAkoqbSMDChFUrrGbmDTTJDjumQmRSfjcztstaJtPMRuYdXTQfrjXiJYxkqMwqsYiMYTUFkwBqcaybAcumSIuIfdmjtzVtcbMCpMw";
Console.WriteLine(perfect(str));
Console.ReadKey();
}
static int perfect(string s)
{
List<int> list = new List<int>();
s= s.ToLower();
Dictionary<char, int> dic = new Dictionary<char, int>();
for (int i = 0; i < s.Length; i++)
{
if (!dic.ContainsKey(s[i]))
{
dic.Add(s[i], 1);
}
else
{
dic[s[i]] += 1;
}
}
int res = 0;
int num = 26;
foreach (var value in dic.Values)
{
list.Add(value);
}
int[] nums = list.ToArray();
BubSort(nums);
foreach (var item in nums)
{
res += item * num--;
}
return res;
}
/// <summary>
/// 冒泡排序
/// </summary>
/// <param name="nums"></param>
public static void BubSort(int[] nums)
{
for (int i = 0; i < nums.Length - 1; i++)
{
for (int j = 0; j < nums.Length - i - 1; j++)
{
if (nums[j + 1] > nums[j])
{
int temp = 0;
temp = nums[j + 1];
nums[j + 1] = nums[j];
nums[j] = temp;
}
}
}
}
}
}