|
Posted on 2008-03-28 10:22 王大头 阅读(253) 评论(0) 编辑 收藏 引用
Anagram Groups Time Limit:1000MS Memory Limit:65536K Total Submit:882 Accepted:252 Description World-renowned Prof. A. N. Agram's current research deals with large anagram groups. He has just found a new application for his theory on the distribution of characters in English language texts. Given such a text, you are to find the largest anagram groups. A text is a sequence of words. A word w is an anagram of a word v if and only if there is some permutation p of character positions that takes w to v. Then, w and v are in the same anagram group. The size of an anagram group is the number of words in that group. Find the 5 largest anagram groups. Input The input contains words composed of lowercase alphabetic characters, separated by whitespace(or new line). It is terminated by EOF. You can assume there will be no more than 30000 words. Output Output the 5 largest anagram groups. If there are less than 5 groups, output them all. Sort the groups by decreasing size. Break ties lexicographically by the lexicographical smallest element. For each group output, print its size and its member words. Sort the member words lexicographically and print equal words only once. Sample Input
undisplayed trace tea singleton eta eat displayed crate cater carte caret beta beat bate ate abet
Sample Output
Group of size 5: caret carte cater crate trace . Group of size 4: abet bate beat beta . Group of size 4: ate eat eta tea . Group of size 1: displayed . Group of size 1: singleton .
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SEP " \n"
struct _STR {
char i[32];
char s[32];
} str[32 * 1024];
struct _STAT {
char s[32];
int c;
int p;
} stat[32 * 1024];
int cmp_char(const void *a, const void *b) {
return(*(char *)a - *(char *)b);
}
int cmp_str(const void *a, const void *b) {
int ret;
ret = strcmp((*(struct _STR *)a).s, (*(struct _STR *)b).s);
if(!ret)
ret = strcmp((*(struct _STR *)a).i, (*(struct _STR *)b).i);
return(ret);
}
int cmp_cnt(const void *a, const void *b) {
int ret;
ret = (*(struct _STAT *)b).c - (*(struct _STAT *)a).c;
if(!ret)
ret = strcmp(str[(*(struct _STAT *)a).p].i, str[(*(struct _STAT *)b).p].i);
return(ret);
}
int main() {
int N = 0, n, M = 0, m, found, high, low, ret;
char buf[128], *p;
memset(str, 0, sizeof(str));
memset(stat, 0, sizeof(stat));
memset(buf, 0, sizeof(buf));
while(fgets(buf, 128, stdin) != NULL) {
for(p = strtok(buf, SEP); p != NULL; p = strtok(NULL, SEP)) {
strcpy(str[N].i, p);
qsort(p, strlen(p), sizeof(char), cmp_char);
strcpy(str[N].s, p);
N++;
}
memset(buf, 0, sizeof(buf));
}
qsort(str, N, sizeof(str[0]), cmp_str);
for(n = 0; n < N; n++) {
high = M;
low = 0;
found = -1;
do {
m = (high + low) / 2;
if(0 == (ret = strcmp(str[n].s, stat[m].s))) {
found = m;
stat[m].c++;
break;
}
else if(ret > 0)
low = m;
else
high = m;
} while(high - low > 1);
if(found == -1) {
strcpy(stat[M].s, str[n].s);
stat[M].c = 1;
stat[M].p = n;
M++;
}
}
qsort(stat, M, sizeof(stat[0]), cmp_cnt);
for(m = 0; m < M && m < 5; m++) {
printf("Group of size %d: ", stat[m].c);
for(n = stat[m].p; ;n++) {
if(n > 1)
if(strcmp(str[n - 1].i, str[n].i) == 0)
continue;
if(strcmp(stat[m].s, str[n].s) == 0)
printf("%s ", str[n].i);
else
break;
}
printf(".\n");
}
}
sf上面的这个wordlist不错: http://wordlist.sourceforge.net/
|