#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef struct {
char s[100];
char re_s[100];
}word;
word data[30005];
int cnt = 0;
typedef struct {
int num;
int start;
}pos;
pos group[30005];
int g_cnt = 0;
word temp;
bool cmp(word a, word b) {
if (strcmp(a.re_s, b.re_s) != 0) return strcmp(a.re_s, b.re_s) < 0;
return strcmp(a.s, b.s) < 0;
}
bool cmp1(pos a, pos b) {
if (a.num != b.num) return a.num > b.num;
return strcmp(data[a.start].s, data[b.start].s) < 0;
}
int main() {
char ss[100];
while (scanf("%s", ss) != EOF) {
strcpy(data[cnt].s, ss);
sort(ss , ss+strlen(ss));
strcpy(data[cnt].re_s, ss);
cnt ++;
}
sort(data, data+cnt, cmp);
int g_cnt = 1;
group[0].num = 1;
group[0].start = 0;
for (int i = 1; i < cnt; i ++) {
if (strcmp(data[i].re_s , data[i-1].re_s) == 0)
group[g_cnt-1].num ++;
else if (strcmp(data[i].re_s , data[i-1].re_s) != 0){
group[g_cnt].num = 1;
group[g_cnt].start = i;
g_cnt ++;
}
}
sort(group, group+g_cnt, cmp1);
for (int i = 0; i < 5 && i < g_cnt; i ++) {
printf("Group of size %d: ", group[i].num);
printf("%s ", data[group[i].start].s);
for (int j = group[i].start+1; j < cnt; j ++) {
if (strcmp(data[j].re_s , data[j-1].re_s)==0 && strcmp(data[j].s , data[j-1].s) != 0) {
printf("%s ", data[j].s);
} else if (strcmp(data[j].re_s , data[j-1].re_s) != 0) {
break;
}
}
printf(".\n");
}
return 0;
}
http://poj.org/problem?id=2408
最新推荐文章于 2019-04-10 21:17:50 发布