87 lines
1.9 KiB
C
87 lines
1.9 KiB
C
#include <stdio.h>
|
|
#include <string.h>
|
|
#define MIN(i, j) (((i) < (j)) ? (i) : (j))
|
|
|
|
char ss[1005];
|
|
int x, dk;
|
|
|
|
struct word {
|
|
char s[1005];
|
|
int k;
|
|
} wd[1005];
|
|
|
|
void swap(struct word* a, struct word* b) {
|
|
struct word c;
|
|
c = *a;
|
|
*a = *b;
|
|
*b = c;
|
|
}
|
|
|
|
int cmp(char s1[], char s2[]) {
|
|
int l1 = strlen(s1), l2 = strlen(s2);
|
|
for (int i = 0; i < MIN(l1, l2); i++) {
|
|
if (s1[i] > s2[i])
|
|
return 1;
|
|
else if (s1[i] < s2[i])
|
|
return -1;
|
|
}
|
|
if (l1 == l2)
|
|
return 0;
|
|
else if (l1 > l2)
|
|
return 1;
|
|
else
|
|
return -1;
|
|
}
|
|
|
|
int xz(char s1[]) {
|
|
for (int i = 0; i < dk; i++) {
|
|
if (cmp(wd[i].s, s1) == 0)
|
|
return i;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
void sort() {
|
|
for (int i = 0; i < dk; i++) {
|
|
for (int j = 0; j < dk - i - 1; j++) {
|
|
if (wd[j].k < wd[j + 1].k || (wd[j].k == wd[j + 1].k && cmp(wd[j].s, wd[j + 1].s) == 1)) {
|
|
swap(&wd[j], &wd[j + 1]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
gets(ss);
|
|
for (int i = 0; i <= strlen(ss); i++) {
|
|
if (ss[i] == ' ' && ss[i - 1] == ' ') {
|
|
x++;
|
|
}
|
|
if (i == strlen(ss) || (ss[i] == ' ' && ss[i - 1] != ' ')) {
|
|
char ls[1005] = "";
|
|
int flag = 1;
|
|
for (int k = x; k < i; k++) {
|
|
if (ss[k] >= 'A' && ss[k] <= 'Z') {
|
|
ss[k] += 32;
|
|
}
|
|
ls[k - x] = ss[k];
|
|
}
|
|
x = i + 1;
|
|
if (xz(ls) != -1) {
|
|
wd[xz(ls)].k++;
|
|
} else {
|
|
for (int j = 0; j < strlen(ls); j++)
|
|
wd[dk].s[j] = ls[j];
|
|
wd[dk].k = 1;
|
|
dk++;
|
|
}
|
|
}
|
|
}
|
|
sort();
|
|
for (int i = 0; i < MIN(dk, 10); i++) {
|
|
printf("%s:%d\n", wd[i].s, wd[i].k);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
// 有点抽象,之后再优化
|