考察内容的实质是排序。
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define ALPHABET_NUM 26
void count_alphabet(char str[], int counter[]) {
int len = strlen(str);
int i, j;
for (i = 0; i < len; i++) {
j = (int)(str[i] - 'a');
counter[j] = counter[j] + 1;
}
}
int count_beauty(int counter[]) {
int i, j;
int temp;
bool status = 1;
int beauty = 0;
for (i = 0; i < ALPHABET_NUM && status; i++) {
status = 0;
for (j = ALPHABET_NUM - 1; j > i; j--) {
if (counter[j] > counter[j-1]) {
temp = counter[j-1];
counter[j-1] = counter[j];
counter[j] = temp;
status = 1;
}
}
}
for (i = 0; i < ALPHABET_NUM; i++) {
beauty = beauty + counter[i] * (ALPHABET_NUM - i);
}
return beauty;
}
int main() {
int n;
char str[10002] = {0};
int counter[ALPHABET_NUM];
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%s", str);
memset(counter, 0, sizeof(counter));
count_alphabet(str, counter);
printf("%d\n", count_beauty(counter));
}
return 0;
}