poj——2408——Anagram Groups

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 <iostream>
#include <cstring>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;

struct world
{
    char s[40];
    char sed[40];
}str[30005];
struct node
{
    char s[40];
    int num;
    int start;
}Node[30005];

int cmp_world(world a,world b)
{
    if (strcmp(a.sed,b.sed)==0)
        return strcmp(a.s,b.s)<0;
    else
        return strcmp(a.sed,b.sed)<0;
}

int cmp_node(node a,node b)
{
    if (a.num==b.num)
        return strcmp(a.s,b.s)<0;
    else
        return a.num>b.num;
}
int main()
{
    int t=0;
    while (scanf("%s",&str[t].s)!=EOF)
    {
        strcpy(str[t].sed,str[t].s);
        int L=strlen(str[t].sed);
        sort(str[t].sed,str[t].sed+L);
        t++;
    }
    sort(str,str+t,cmp_world);

    int a=1;
    strcpy(Node[0].s,str[0].s);
   /*cout<<"s:"<<endl;
    for (int i=0;i<t;i++)
        cout<<str[i].s<<endl;*/
    Node[0].start=0;
    Node[0].num=1;
    for (int i=1;i<t;i++)
    {
        if (strcmp(str[i].sed,str[i-1].sed)==0)
        {
            Node[a-1].num++;
        }
        else
        {
            strcpy(Node[a].s,str[i].s);
            Node[a].start=i;
            Node[a].num=1;
            a++;
        }
    }
    sort(Node,Node+a,cmp_node);
   /* cout<<"group:"<<endl;
    for (int i=0;i<a;i++)
        cout<<Node[i].s<<endl;*/
   for(int i=0;i<5;i++)
    {
        printf("Group of size %d:",Node[i].num);
        int p=Node[i].start;
        //cout<<p<<endl;
        for(int j=0;j<Node[i].num;j++)
        {
        	int k;
            for(k=j-1;k>=0;k--)
                if(strcmp(str[k].s,str[j].s)==0)
                    break;
            if(k<0||j==0)printf(" %s",str[p+j].s);
        }
        printf(" .\n");
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值