POJ 2408 Anagram Groups 字母排序

来源:互联网 发布:网络零售总额 编辑:程序博客网 时间:2024/05/14 11:18
点击打开链接
Anagram Groups
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 3807 Accepted: 1015

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

undisplayedtraceteasingletonetaeatdisplayedcratecatercartecaretbetabeatbateateabet

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 .

Source

Ulm Local 2000

给你一些单词,让你无论它们的顺序如何,找出相同的单词,将这些相同的单词归为一组,最后输出单词相同的最多的前五组,如果有组数相同的则按照字典序输出。
//2328K94MS#include<stdio.h>#include<algorithm>#include<string.h>#include<string>using namespace std;struct sa{    char st[37];//输入的单词    char end[37];//排序之后的单词}s[30007];struct G{    char st[37];    int size;//大小    int start;}group[30007];int cmp(sa a,sa b){    if(strcmp(a.end,b.end)!=0)        return (strcmp(a.end,b.end)<0);    return (strcmp(a.st,b.st)<0);}int cmp_group(G a,G b){    if(a.size!=b.size)return a.size>b.size;    else return strcmp(a.st,b.st)<0;}int main(){    int k=0,len;    while(scanf("%s",s[k].st)!=EOF)    {        strcpy(s[k].end,s[k].st);        len=strlen(s[k].end);        sort(s[k].end,s[k].end+len);//将这个单词从小到大排序        k++;    }    sort(s,s+k,cmp);//将所有单词按照字典序排序    int a=1;    group[0].size=1;    group[0].start=0;    strcpy(group[0].st,s[0].st);    for(int i=1;i<k;i++)        if(strcmp(s[i].end,s[i-1].end)==0)            group[a-1].size++;        else        {            group[a].size=1;            group[a].start=i;            strcpy(group[a].st,s[i].st);            a++;        }    sort(group,group+a,cmp_group);//将group里面存的单词数量按照从大到小排序    for(int i=0;i<5;i++)    {        printf("Group of size %d:",group[i].size);        int p=group[i].start;        for(int j=0;j<group[i].size;j++)        {            for(k=j-1;k>=0;k--)                if(strcmp(s[k].st,s[j].st)==0)                    break;            if(k<0||j==0)printf(" %s",s[p+j].st);        }        printf(" .\n");    }    return 0;}


0 0
原创粉丝点击