题目链接:http://poj.org/problem?id=2408

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 .

题解:

用字典树把每个组都hash成一个数字 $x$,然后把组内的字符串全部存在编号为 $x$ 的vector内。

然后对vector进行排序(实际上是对vector的编号进行排序),再输出前五项就好了,注意相同的单词虽然计数,但是输出时只输出一次。

AC代码:

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
const int maxn=3e4+;
string str;
int idx[maxn];
vector<string> g[maxn];
bool cmp(int i,int j)
{
if(g[i].size()!=g[j].size())
return g[i].size()>g[j].size();
else if(g[i].size() && g[j].size())
return g[i][]<g[j][];
} namespace Trie
{
const int SIZE=maxn*;
int sz,tot;
struct TrieNode{
int ed;
int nxt[];
}trie[SIZE];
void init(){sz=, tot=;}
int insert(const string& s)
{
int p=;
for(int i=;i<s.size();i++)
{
int ch=s[i]-'a';
if(!trie[p].nxt[ch]) trie[p].nxt[ch]=++sz;
p=trie[p].nxt[ch];
}
return trie[p].ed?trie[p].ed:(trie[p].ed=++tot);
}
}; int main()
{
ios::sync_with_stdio();
cin.tie(), cout.tie(); Trie::init();
while(cin>>str)
{
string tmp=str;
sort(tmp.begin(),tmp.end());
int t=Trie::insert(tmp);
g[t].push_back(str);
} for(int i=;i<=Trie::tot;i++) idx[i]=i;
sort(idx+,idx+Trie::tot+,cmp); for(int i=;i<= && g[idx[i]].size()>;i++)
{
vector<string>& v=g[idx[i]];
cout<<"Group of size "<<v.size()<<": ";
sort(v.begin(),v.end());
v.erase(unique(v.begin(),v.end()),v.end());
for(int k=;k<v.size();k++) cout<<v[k]<<' ';
cout<<".\n";
}
}

POJ 2408 - Anagram Groups - [字典树]的更多相关文章

  1. poj 2408 Anagram Groups(hash)

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

  2. poj 2408 Anagram Groups

    Description World-renowned Prof. A. N. Agram's current research deals with large anagram groups. He ...

  3. POJ 2001 Shortest Prefixes(字典树)

    题目地址:POJ 2001 考察的字典树,利用的是建树时将每个点仅仅要走过就累加.最后从根节点開始遍历,当遍历到仅仅有1次走过的时候,就说明这个地方是最短的独立前缀.然后记录下长度,输出就可以. 代码 ...

  4. poj 1204 Word Puzzles(字典树)

    题目链接:http://poj.org/problem?id=1204 思路分析:由于题目数据较弱,使用暴力搜索:对于所有查找的单词建立一棵字典树,在图中的每个坐标,往8个方向搜索查找即可: 需要注意 ...

  5. poj 1056 IMMEDIATE DECODABILITY 字典树

    题目链接:http://poj.org/problem?id=1056 思路: 字典树的简单应用,就是判断当前所有的单词中有木有一个是另一个的前缀,直接套用模板再在Tire定义中加一个bool类型的变 ...

  6. POJ 1816 - Wild Words - [字典树+DFS]

    题目链接: http://poj.org/problem?id=1816 http://bailian.openjudge.cn/practice/1816?lang=en_US Time Limit ...

  7. nyoj 163 Phone List(动态字典树<trie>) poj Phone List (静态字典树<trie>)

    Phone List 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 Given a list of phone numbers, determine if it i ...

  8. poj 2503:Babelfish(字典树,经典题,字典翻译)

    Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 30816   Accepted: 13283 Descr ...

  9. poj 2513 连接火柴 字典树+欧拉通路 好题

    Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submissions: 27134   Accepted: 7186 ...

随机推荐

  1. Djnogo Web开发学习笔记(2)

    安   装 截止目前,https://www.djangoproject.com/download/提供的最新的Django的下载版本为1.6.4. Install Django You’ve got ...

  2. 最好的Python机器学习库

    参考链接:http://www.csdn.net/article/2015-12-10/2826435

  3. linux命令(53):useradd,区别于adduser

    adduser和useradd的区别: useradd是一个linux命令,但是它提供了很多参数在用户使用的时候根据自己的需要进行设置: 而adduser是一个perl 脚本,在使用的时候会 出现类似 ...

  4. 【ZooKeeper】ZooKeeper入门流水记

    单机模式 下载zookeeper的包 wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.9/zookeeper-3.4.9.ta ...

  5. openfire开发文档

    http://www.blogjava.net/yi88han/archive/2009/02/11/254203.html

  6. 【iCore1S 双核心板_FPGA】例程一:GPIO输出实验——点亮LED

    实验现象: 三色LED循环点亮. 核心源代码: //--------------------Module_LED-----------------------------// module LED( ...

  7. js 规范

    1.原型链的弊端是不支持多重继承.记住,原型链会用另一类型的对象重写类的 prototype 属性 2.注意:调用 ClassA 的构造函数,没有给它传递参数.这在原型链中是标准做法.要确保构造函数没 ...

  8. Git -- 自定义git样式

    在安装Git一节中,我们已经配置了user.name和user.email,实际上,Git还有很多可配置项. 比如,让Git显示颜色,会让命令输出看起来更醒目: $ git config --glob ...

  9. java-信息安全(十三)-数字签名,代码签名【Java证书体系实现】

    概述 信息安全基本概念 前置 java-信息安全(十二)-数字签名[Java证书体系实现] 过程 通过工具JarSigner可以完成代码签名.  这里我们对tools.jar做代码签名,命令如下: 进 ...

  10. css文件的MIME错误引发的Jquery Mobile绘制错误

    静态文件serve设置的MIME不对,引起的浏览器警告 Resource interpreted as Stylesheet but transferred with MIME type applic ...