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 ...
随机推荐
- SQL Server 数据库基础笔记分享(上)
前言 本文是个人学习SQL Server 数据库时的以往笔记的整理,内容主要是对数据库的基本增删改查的SQL语句操作和约束,视图,存储过程,触发器的基本了解. 注:内容比较基础,适合入门者对SQL S ...
- 云服务器 - 安装zookeeper单机环境
由于电脑中病毒,被人当做肉鸡挖矿了...所以很多东西都要重装, 之前没有写过zk如何安装,所以安装的时候顺便来记录一下分享出来 第一步上传zk安装包到服务器,如图: 解压: tar -zxvf zoo ...
- C++复数运算 重载
近期整理下很久前写的程序,这里就把它放在博文中了,有些比较简单,但是很有学习价值. 下面就是自己很久前实现的复数重载代码,这里没有考虑特殊情况,像除法中,分母不为零情况. #include <i ...
- 安装polyglot出错
安装polyglot出错 错误 Complete output from command python setup.py egg_info: Traceback (most recent call l ...
- Linux配置简单采集
#!/bin/bash #CPU逻辑个数 cpu_processor=$(cat /proc/cpuinfo | grep processor | wc -l) #CPU物理个数 cpu_num=$( ...
- 带cookie跨域问题的思路以及echo的解决方案
问题起因 前后端分离,前端要访问后端资源,而且需要携带cookie信息,这时碰到了跨域问题.一开始以为设置为允许跨域allow_origins为即可.可是浏览器还是拦截的请求,于是查看跨域规则,原来跨 ...
- SSM框架搭建最新教程(超详细)
个人认为使用框架并不是很难,关键要理解其思想,这对于我们提高编程水平很有帮助.不过,如果用都不会,谈思想就变成纸上谈兵了!!!先技术,再思想.实践出真知. 1.基本概念 1.1.Spring Spr ...
- 8 -- 深入使用Spring -- 6...2 Spring支持的事务策略
8.6.2 使用XML Schema配置事务策略 Spring 同时支持编程式事务策略和声明式事务策略,通常都推荐采用声明式事务策略. ⊙ 声明式事务能大大降低开发者的代码书写量,而且声明式事务几乎不 ...
- 利用media query让背景图适应不同分辨率的设备
随着上网方式的多样化,用户选择上网的工具不再仅是PC,而可以是手机,或者平板电脑.随之而来的问题是如何让网页适应不同分辨率,这给前端工程师们带来了新的挑战,其中重要的一点是如何让图片能在不同的分辨率下 ...
- 题目1015:还是A+B(简单判断)
题目链接:http://ac.jobdu.com/problem.php?pid=1015 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...