Anagram Groups(字典树)

Anagram Groups

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.

  • 输入
    undisplayed
    trace
    tea
    singleton
    eta
    eat
    displayed
    crate
    cater
    carte
    caret
    beta
    beat
    bate
    ate
    abet
  • 输出
    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 .

POJ2408

代码:
#include<cstdio>
#include<set>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstring>
#include<cstdlib>
using namespace std;

const int N = 1e5+10;
int son[N*4][30],cnt[N*4],idx;
struct node
{
    int num;
    set<string>ss;  //存放满足相同条件的字符串
}a[N*4];
int tot;
int vis[N];
char s[N];

void insert(char *s)  //插入操作可以完成两个操作:
//1、只将按字典序的串存进字典树里,然后跑字符串
//2、将满足相同条件的字符放在一起
{
    string str = s;
    int p = 0;
    int n = strlen(s);
    sort(s,s+n);  //操作1

    for(int i=0;i<n;i++)
    {
        int u = s[i]-'a';
        if(!son[p][u])  son[p][u] = ++idx;
        p = son[p][u];
    }

    cnt[p]++;
    if(!vis[p])
        vis[p]=tot++;  //优化结构体空间

    a[vis[p]].num++;
    
    a[vis[p]].ss.insert(str);  //操作2
    return ;
}

bool cmp(struct node a,struct node b)
{
    if(a.num==b.num)
    {
        return *(a.ss.begin)()<*(b.ss.begin());  //这是排序set的操作
    }
    return a.num>b.num;
}

int main()
{
    while(scanf("%s",s)!=EOF)
    {
        insert(s);
    }

    sort(a,a+tot,cmp);

    for(int i=0;i<5;i++)
    {
        printf("Group of size %d: ", a[i].num);
        //利用set的位置迭代器进行输出
        set<string>::iterator it;

        for (it = a[i].ss.begin(); it != a[i].ss.end(); ++it)
            printf("%s ", (*it).c_str());
        printf(".\n");                       

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

pig2687

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值