POJ 2408 - Anagram Groups - [字典树]
题目链接: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 - [字典树]的更多相关文章
- poj 2408 Anagram Groups(hash)
id=2408" target="_blank" style="">题目链接:poj 2408 Anagram Groups 题目大意:给定若干 ...
- poj 2408 Anagram Groups
Description World-renowned Prof. A. N. Agram's current research deals with large anagram groups. He ...
- POJ 2001 Shortest Prefixes(字典树)
题目地址:POJ 2001 考察的字典树,利用的是建树时将每个点仅仅要走过就累加.最后从根节点開始遍历,当遍历到仅仅有1次走过的时候,就说明这个地方是最短的独立前缀.然后记录下长度,输出就可以. 代码 ...
- poj 1204 Word Puzzles(字典树)
题目链接:http://poj.org/problem?id=1204 思路分析:由于题目数据较弱,使用暴力搜索:对于所有查找的单词建立一棵字典树,在图中的每个坐标,往8个方向搜索查找即可: 需要注意 ...
- poj 1056 IMMEDIATE DECODABILITY 字典树
题目链接:http://poj.org/problem?id=1056 思路: 字典树的简单应用,就是判断当前所有的单词中有木有一个是另一个的前缀,直接套用模板再在Tire定义中加一个bool类型的变 ...
- POJ 1816 - Wild Words - [字典树+DFS]
题目链接: http://poj.org/problem?id=1816 http://bailian.openjudge.cn/practice/1816?lang=en_US Time Limit ...
- 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 ...
- poj 2503:Babelfish(字典树,经典题,字典翻译)
Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 30816 Accepted: 13283 Descr ...
- poj 2513 连接火柴 字典树+欧拉通路 好题
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27134 Accepted: 7186 ...
随机推荐
- 【LeetCode】240. Search a 2D Matrix II
Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix. Thi ...
- linux删除软连接
#mkdir test_chk #touch test_chk/test.txt #vim test_chk/test.txt (这一步随便在这个test.txt里写点东东即可) 下面我们来创建tes ...
- Atitit web remote远程调试的原理attilax总结
Atitit web remote远程调试的原理attilax总结 Jvm是vm打开一个debug port,然后ide先连接..然后执行url,就会vm会与ide沟通.. Php的xdebug po ...
- 每日英语:Dashing the China Dream
Much has been said about what the 'China Dream' really means to many Chinese -- whether it is nation ...
- RNN,LSTM,GRU简单图解:
一篇经典的讲解RNN的,大部分网络图都来源于此:http://colah.github.io/posts/2015-08-Understanding-LSTMs/ 每一层每一时刻的输入输出:https ...
- 在webpack中使用postcss-px2rem的
经过一番折腾重要搞定了. 首先需要安装postcss-plugin-px2rem. npm install --save-dev postcss-plugin-px2rem 我的webpack工程中没 ...
- <王川自选集第一卷电子书 >读书笔记
什么叫限定自己能力的边界?就是接受承认一个事实,世界上大多数东西你并不懂,或者一知半解,懂得很肤浅.只有少数东西,由于你可以天天深入的接触,你才有深刻的理解. 购买一个公司的股票,却从未使用其核心产品 ...
- (实用)CentOS 6.3更新内置Python2.6
在安装Kilo版的OpenStack时,我们发现社区已经将Python升到2.7,而CentOS 6.3上仍然在使用2.6版的Python.本文记录将CentOS 6.3内置的Python2.6更新为 ...
- centos7 防火墙
1.firewalld的基本使用 启动: systemctl start firewalld 关闭: systemctl stop firewalld 查看状态: systemctl status f ...
- [GAN] How to use GAN - Meow Generator
一篇介绍GAN应用的文章.今后GAN模型学习的主要内容. 中文链接:萌物生成器:如何使用四种GAN制造猫图 原文链接:https://ajolicoeur.wordpress.com/cats/ 项目 ...