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 .

Source

 
把每一个字符串,升序排序,可以得到一个字符串,如果两个字符串的字典序最小的字符串相同,就属于一个组, 所以用字典树记录这个最小字典序的字符串,然后映射下标到group结构体(存各种字符串,及个数,由于字符串输出要去重,所以用set,这样函数传参数要引用传递,否则很慢),最后按照个数排序。
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <set>
#define MAX 30001
using namespace std;
struct group {
int num;
set<string> gr;
group() {
num = ;
}
}g[MAX];
int trie[MAX * ][],to[MAX * ];
int pos,num;
int snum[];
bool cmp(const group &a,const group &b) {///注意这里
if(a.num == b.num) return *(a.gr.begin()) < *(b.gr.begin());
return a.num > b.num;
}
void Insert(char *s) {
int len = strlen(s);
string s1 = s;
string s2 = "";
for(int i = ;s[i];i ++) {
snum[s[i] - 'a'] ++;
}
for(int i = ;i < ;i ++) {
while(snum[i]) {
s2 += 'a' + i;
snum[i] --;
}
}
int i = ,c = ;
while(i < len) {
int d = s2[i] - 'a';
if(!trie[c][d]) trie[c][d] = ++ pos;
c = trie[c][d];
i ++;
}
if(!to[c]) to[c] = ++ num;
g[to[c]].gr.insert(s1) ;
g[to[c]].num ++;
} int main() {
char str[];
while(~scanf("%s",str)) {
Insert(str);
}
sort(g + ,g + + num,cmp);
for(int i = ;i <= ;i ++) {
printf("Group of size %d:",g[i].num);
for(set<string>::iterator it = g[i].gr.begin();it != g[i].gr.end();it ++) {
printf(" %s",(*it).c_str());
}
puts(" .");
}
}

poj 2408 Anagram Groups的更多相关文章

  1. poj 2408 Anagram Groups(hash)

    id=2408" target="_blank" style="">题目链接:poj 2408 Anagram Groups 题目大意:给定若干 ...

  2. POJ 2408 - Anagram Groups - [字典树]

    题目链接:http://poj.org/problem?id=2408 World-renowned Prof. A. N. Agram's current research deals with l ...

  3. POJ 1256.Anagram

    2015-06-04 问题简述: 输出一串字符的全排列,顺序不同于一般的字母序,而是 A<a<B<b......<Z<z.所以应该重写一个比较函数. 原题链接:http: ...

  4. poj 2408 Apple Tree

    http://poj.org/problem?id=2486 典型的回溯题目:特别是状态方程用三维的来标记是否要走回路. 题意:一颗树,n个点(1-n),n-1条边,每个点上有一个权值,求从1出发,走 ...

  5. poj 1256 Anagram(dfs)

    题目链接:http://poj.org/problem?id=1256 思路分析:该题为含有重复元素的全排列问题:由于题目中字符长度较小,采用暴力法解决. 代码如下: #include <ios ...

  6. poj 1256 Anagram—next_permutation的神奇应用

    题意:给你一条字符串,让你输出字符串中字符的全排列,输出的顺序要按它给的奇葩的字典序. 题解:要输出全排列,暴力dfs可以过,但要注意题目的字典序以及相同字符的情况.如果用next_permutati ...

  7. Anagram Groups(字符串)

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2316 理解错一点题意就能WA到死...题中对于 ...

  8. HOJ题目分类

    各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...

  9. (转)ACM next_permutation函数

    转自 stven_king的博客 这是一个求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm>下面是以前的笔记  (1) int 类型的next_permuta ...

随机推荐

  1. 乐思启慧教学系列—Bootstrap布局规则

    1外层变化,内层相应变化规则 col-md-6 col-md-4 外层6变成12,扩大了2倍,里面就得缩小2倍(除以2), 只有这样才能保持外部变化了,内部依然对齐 col-md-12 col-md- ...

  2. Service degrade

    服务降级:主要是针对非正常情况下的应急服务措施;比如电商平台,在针对618.双11等高峰情形下采用部分服务不出现或者延时出现的情形. 为什么是非正常情况下呢,比如我要出差,如果经常出差的话,要带的衣服 ...

  3. android 7.0 (nougat)的编译优化-ninja

    http://blog.csdn.net/songjam/article/details/52640501 版权声明:本文为博主原创文章,未经博主允许不得转载. 从官方的定义,ninja大大缩短了an ...

  4. java配置好jdk-bash: /usr/bin/java: No such file or directory

    在 Linux 系统中安装 JDK 环境,配置好环境变量后,输入 java.javac 或者 java -version 等时,都提示如下错误: -bash: /usr/local/java/bin/ ...

  5. 大话设计模式之PHP篇 - 观察者模式

    定义观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象,这个主题对象在状态发生改变时,会通知所有观察者对象,使他们能够自动更新自己. <?php /*主题类或称为通知类 ...

  6. Docker 跨主机网络

    Docker提供两种原生的跨主机网络: Overlay  和  Macvlan libnetwork & CNM libnetwork 是 docker 容器网络库,最核心的内容是其定义的 C ...

  7. HTML5 上传图片 到ASP.NET MVC

    @{ ViewBag.Title = "Home Page"; } <!DOCTYPE HTML PUBLIC> <html> <head> & ...

  8. centos7 下安装eclipse

    1 在下面路径下载 eclipse-jee-neon-2-linux-gtk-x86_64.tar.gzhttp://eclipse.stu.edu.tw/technology/epp/downloa ...

  9. 【bzoj2118&洛谷P2371】墨墨的等式(最短路神仙题)

    题目传送门:bzoj2118 洛谷P2371 这道题看了题解后才会的..果然是国家集训队的神仙题,思维独特. 首先若方程$ \sum_{i=1}^{n}a_ix_i=k $有非负整数解,那么显然对于每 ...

  10. Spark性能优化指南--基础篇

    前言 开发调优 调优概述 原则一:避免创建重复的RDD 原则二:尽可能复用同一个RDD 原则三:对多次使用的RDD进行持久化 原则四:尽量避免使用shuffle类算子 原则五:使用map-side预聚 ...