题目链接: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. MySQL 5.6新特性 -- crash-safe replication

    在slave上有两个线程:io线程和sql线程io线程接收master的二进制日志信息并写入到本地的relay log中:sql线程执行本地relay log中的信息.io线程读取到的二进制日志当前位 ...

  2. nginx与apache的参考配置

    nginx与apache是两大最主流的服务器,功能强大,但配置起来也比较麻烦,对于初学者来讲可能有些地方并不完全清楚其作用,这里搜集了一些配置的作用及其使用方法.其中nginx提供了推荐配置,而apa ...

  3. 物联网架构成长之路(13)-SpringBoot入门

    1. 前言 下载最新版的JavaEE eclipse-jee-oxygen-2-win32-x86_64.zip 安装STS插件 Window->Eclipse Marketplace -> ...

  4. Android开发(十二)——头部、中部、底部布局

    参考: [1] http://www.thinksaas.cn/group/topic/82898/ [2] http://***/Article/12399 其实RadioGroup不好使,不能图片 ...

  5. 教你一招:Microsoft Office Word已停止工作

    1/按组合键WIN+R打开运行对话框 2/在打开框中键入%USERPROFILE%\AppData\Roaming\Microsoft\Templates,单击“确定”按钮 3/在打开的窗口鼠标右键删 ...

  6. 【css】zSass - 用 sass 编写 css

    zSass 是自己整理的一个 sass 库,参考了 sassCore. 目录结构 variables.scss 默认值设置. reset.scss 重置浏览器样式.(参考:normalize) com ...

  7. SmileyCount.java笑脸加法程序代写(QQ:928900200)

    SmileyCount.java 1/4Java Programming 2014Course Code: EBU4201Mini ProjectTask 1 [30 marks]SmileyCoun ...

  8. 【转】iframe页面跳转时,导致父页面滚动!该怎么解决?

    HTML code <body> <form id="form1" runat="server"> <iframe id=&quo ...

  9. Mysql EF 触发器生成主键id 存储区更新、插入或删除语句影响到了意外的行数(0)。实体在加载后可能被修改或删除。刷新 ObjectStateManager 项 ;System.Data.Entity.Infrastructure.DbUpdateConcurrencyException

    http://stackoverflow.com/questions/24725261/how-to-use-a-custom-identity-column-in-sql-with-entity-f ...

  10. @Transactional(readOnly=true) in Spring

    http://www.skill-guru.com/blog/2010/12/19/transactionalreadonlytrue-in-spring/ @Transactional(readOn ...