poj 2408 Anagram Groups
Description
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
Output
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
#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的更多相关文章
- poj 2408 Anagram Groups(hash)
id=2408" target="_blank" style="">题目链接:poj 2408 Anagram Groups 题目大意:给定若干 ...
- POJ 2408 - Anagram Groups - [字典树]
题目链接:http://poj.org/problem?id=2408 World-renowned Prof. A. N. Agram's current research deals with l ...
- POJ 1256.Anagram
2015-06-04 问题简述: 输出一串字符的全排列,顺序不同于一般的字母序,而是 A<a<B<b......<Z<z.所以应该重写一个比较函数. 原题链接:http: ...
- poj 2408 Apple Tree
http://poj.org/problem?id=2486 典型的回溯题目:特别是状态方程用三维的来标记是否要走回路. 题意:一颗树,n个点(1-n),n-1条边,每个点上有一个权值,求从1出发,走 ...
- poj 1256 Anagram(dfs)
题目链接:http://poj.org/problem?id=1256 思路分析:该题为含有重复元素的全排列问题:由于题目中字符长度较小,采用暴力法解决. 代码如下: #include <ios ...
- poj 1256 Anagram—next_permutation的神奇应用
题意:给你一条字符串,让你输出字符串中字符的全排列,输出的顺序要按它给的奇葩的字典序. 题解:要输出全排列,暴力dfs可以过,但要注意题目的字典序以及相同字符的情况.如果用next_permutati ...
- Anagram Groups(字符串)
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2316 理解错一点题意就能WA到死...题中对于 ...
- HOJ题目分类
各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...
- (转)ACM next_permutation函数
转自 stven_king的博客 这是一个求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm>下面是以前的笔记 (1) int 类型的next_permuta ...
随机推荐
- Django 之models进阶操作
到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MySQLdb 来连接数据库,并编写数据访问层代码 业务逻辑层去调用数据访问层执行数据库操作 ...
- 每天一个Linux命令(56)yum命令
用于添加/删除/更新RPM包,自动解决包的依赖问题以及系统更新升级. (1)用法: 用法: yum [参数] [软件名] (2)功能: 功能: yum ...
- 关于Class.getResourceAsStream
Properties properties = new Properties(); properties.load(new InputStreamReader(CharactorTest.cl ...
- [POI2009]Wie
题目 BZOJ 虽然是解压题但也学到了简洁的码风 做法 \(dijkstra\)跑动规 My complete code #include<bits/stdc++.h> #include& ...
- iOS_SDWebImage框架分析
SDWebImage 支持异步的图片下载+缓存,提供了 UIImageView+WebCacha 的 category,方便使用.使用SDWebImage首先了解它加载图片的流程. 入口 setIma ...
- nginx的理解
1.静态HTTP服务器 首先,Nginx是一个HTTP服务器,可以将服务器上的静态文件(如HTML.图片)通过HTTP协议展现给客户端. 配置: 2.反向代理服务器 什么是反向代理? 客户端本来可以直 ...
- Python面向对象的编程注意细节
和前文一样,这了也是学习过程中,来源于网上各种资料的一个整合记录,希望能够帮到自己和大家: 主要的关注点是在使用class的时候,应该注意的一些细节: 1.在class里面,有了 __init__(s ...
- php二维数组自定义排序
$arr = array( '0' => array('id' =>1,'price'=>200), '1' => array('id' =>2,'price'=> ...
- numpy array或matrix的交换两行
A[j,:] = A[maxindex,:] # 注意这样是一个很低级的错误!这样只是赋值 我们很容易想起python中的两个值交换一句搞定不用引入中间变量 a, b = b, a 但在numpy的a ...
- js简单工厂
我以计算器为例写一个简单工厂模式,只完成加减乘除4个计算功能,考虑到其他功能方便日后扩展,遵循开放-封闭原则. 简单工厂类图: 先看一下C#的简单工厂是如何实现的: 定义抽象类Operation,加减 ...