https://www.nowcoder.com/practice/02cb8d3597cf416d9f6ae1b9ddc4fde3?tpId=37&tqId=21268&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title= 其实不难发现,只要把出现次数最多的字符,出现次数记录下来,让这个个数*26,第二多的个数*25,依次*24.......
//beauty
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int com(void* p1, void* p2)
{
return (strcmp((int*)p2, (int*)p1)); //accordance the number it appeared,descending
}
int main() {
int N = 0; //line number
int map[128] = { 0 }; //store character ASCII to compute the number it appear
int count = 26; //it should be descending,the largest number multiply 26,second multiply 25...
int i = 0; //record the number of character strings
int j = 0; //index to search in each character
char* tmp = NULL; //record the place of the character in map
int ans[10000] = { 0 }; //store the ans number ,finally printf
char* name[1000];//store character string
while (scanf("%d", &N) != EOF)
{
for (i = 0; i < N; i++)
{
name[i] = malloc(sizeof(char) * 10000);
if (!name[i])
{
return -1;
}
scanf("%s", name[i]);
}
for (i = 0; i < N; i++)
{
memset(map, 0, sizeof(map));
count = 26;
tmp = name[i];
int len = strlen(tmp);
for (j = 0; j < len; j++)
{
map[tmp[j]]++;
}
qsort(map, 128, sizeof(int), com);
for (j = 0; j < 26; j++)
{
ans[i] += (count * map[j]);
count--;
}
free(name[i]); //Don't forget!!
name[i] = NULL;
}
for (i = 0; i < N; i++)
{
printf("%d\n", ans[i]);
}
}
return 0;
}