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 ...
随机推荐
- Yii2 自定义独立验证器
新建一个文件: ?php /** * author : forecho <caizhenghai@gmail.com> * createTime : 2015/7/1 14:54 * de ...
- Yii2 高级模板 多域名管理问题
现在在网站中有这种情况,比如有一个 http://frontend.com/tv 需要根据判断用户的 User Agent ,如果用户是手机浏览器的话,则跳转到 http://mobile.com/t ...
- MSDN使用
比如我想查一下fopen这个函数怎么用,在索引里搜索一下fopen,很容易找到了. 但是如果我想横向扩展一下,查看一些与fopen相关的函数,应该怎么找呢? 很简单,点击定位: 你就能把fopen定位 ...
- 吐槽 坑爹的MySQL安装路径选择
一般再windows下面安装MySQL我们都会选择msi安装模式,然而安装最新版的MySQL(mysql-installer-community-5.7.11.0.msi 下载地址)发现MySQL默认 ...
- 建议13:使用Python模块re实现解析小工具
# -*- coding:utf-8 -*- # ''' Python re 的主要功能: re.compile(pattern[, flags]) 把正则表达式的模式和标识转化成正则表达式对象,供 ...
- DNS 缓存机制原理
DNS 缓存机制原理 简单来说,一条域名的DNS记录会在本地有两种缓存:浏览器缓存和操作系统(OS)缓存.在浏览器中访问的时候,会优先访问浏览器缓存, 如果未命中则访问OS缓存,最后再访问DNS服务器 ...
- SOA 面向服务架构 阅读笔记(一)
Service Oriented Architecture 面向服务架构 学习笔记(一) 1.业务自由 1.1 在很多企业中,业务和IT技术是各自独立的,无法使用通用的统一语言进行管理. 1.2 ...
- HDFS-文件读取API
package com.zhen.hdfs; import java.io.IOException; import java.io.InputStream; import java.net.URI; ...
- 汇编笔记 RET
assume cs:code,ss:stack stack segment db dup() stack ends code segment mov ax,4c00h int 21h start: m ...
- codeforces707B:Bakery
Description Masha wants to open her own bakery and bake muffins in one of the n cities numbered from ...