poj 2408 Anagram Groups

Description

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 .

Source

Ulm Local 2000

把每一个字符串,升序排序,可以得到一个字符串,如果两个字符串的字典序最小的字符串相同,就属于一个组, 所以用字典树记录这个最小字典序的字符串,然后映射下标到group结构体(存各种字符串,及个数,由于字符串输出要去重,所以用set,这样函数传参数要引用传递,否则很慢),最后按照个数排序。

代码:

#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 = 0;
    }
}g[MAX];
int trie[MAX * 100][26],to[MAX * 100];
int pos,num;
int snum[26];
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 = 0;s[i];i ++) {
        snum[s[i] - ‘a‘] ++;
    }
    for(int i = 0;i < 26;i ++) {
        while(snum[i]) {
            s2 += ‘a‘ + i;
            snum[i] --;
        }
    }
    int i = 0,c = 0;
    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[100];
    while(~scanf("%s",str)) {
        Insert(str);
    }
    sort(g + 1,g + 1 + num,cmp);
    for(int i = 1;i <= 5;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(" .");
    }
}

原文地址:https://www.cnblogs.com/8023spz/p/9629787.html

时间: 2024-11-13 00:02:19

poj 2408 Anagram Groups的相关文章

poj 2408 Anagram Groups(hash)

题目链接:poj 2408 Anagram Groups 题目大意:给定若干个字符串,将其分组,按照组成元素相同为一组,输出数量最多的前5组,每组按照字典序输出所 有字符串.数量相同的输出字典序较小的一组. 解题思路:将所有的字符串统计字符后hash,排序之后确定每组的个数并且确定一组中字典序最小的字符串.根据个数 以及字符串对组进行排序. #include <cstdio> #include <cstring> #include <vector> #include &

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

POJ 2408 Anagram Groups 排序到极致

Anagram Groups Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4903   Accepted: 1316 Description 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 t

poj 1256 Anagram

题目链接:http://poj.org/problem?id=1256 思路:     该题为含有重复元素的全排列问题:由于题目中字符长度较小,采用暴力法解决. 代码如下: #include <iostream> #include <algorithm> using namespace std; const int MAX_N = 20; char P[MAX_N], A[MAX_N]; char * SortAlp( char P[], int n ) { int Low[MAX

7 POJ 1256 Anagram

给一个字符串包含大小写字符,规定'A'<'a'<'B'<'b'<...<'Z'<'z',求该字符串的全排列. 用裸的dfs+map判重 写了一遍超时了,那种机智的dfs方法没有怎么看懂.. 最开始用的set+next_permutation,太年轻,也超时了... 运用一个next_permutation()函数即可,<algorithm>头文件 注意要先将字符串sort一遍,然后next_permutation()也要把比较函数cmp传进去,原来都不知道可

POJ 1256 Anagram(输入可重集枚举排序)

[题意简述]:本题题意很好理解!题目给出的Hint,使我们对关键点有了更加清晰的认识 An upper case letter goes before the corresponding lower case letter. So the right order of letters is 'A'<'a'<'B'<'b'<...<'Z'<'z'. 就是给一个序列(序列可以有重复的元素),让我们输出它的所有排列,字母顺序规定给出! [分析]:这道题是我之前学习枚举排序和子

HOJ 题目分类

转自:http://blog.sina.com.cn/s/blog_65f3869301011a1o.html ******************************************************************************* 简单题(包括枚举,二分查找,(复杂)模拟,基础数据结构(栈.队列),杂题等 ****************************************************************************

next_permutation函数

转自此处 http://blog.sina.com.cn/s/blog_9f7ea4390101101u.html 这是一个求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm>下面是以前的笔记    与之完全相反的函数还有prev_permutation  (1) int 类型的next_permutation int main(){ int a[3];a[0]=1;a[1]=2;a[2]=3; do{cout<<a[0]<<" &

杭电ACM分类

杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDIATE DECODABILITY