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 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

对strcmp();认识不到位WA一天了 哎

AC:代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
struct my{
    char w1[30];
    char w2[30];
    int size;
    int len;
    void put(){
    printf("w1: %s  w2: %s  len: %d  size:  %d\n",w1,w2,len,size);
    }
};
struct on{
    int isize;
    int start;
    int end;
    void put(){
    printf("start: %d end: %d isize: %d\n",start,end,isize);
    }
};
on z[30010];
my me[30010];
bool cmp1(my a,my b){
    if(a.len==b.len){
        if(strcmp(a.w2,b.w2)==0)
            return strcmp(a.w1,b.w1)<0?true:false;
        else return strcmp(a.w2,b.w2)<0?true:false;
    }
    return a.len>b.len;
}
bool cmp2(on a,on b){
    if(a.isize==b.isize){
        return strcmp(me[a.start].w1,me[b.start].w1)<0?true:false;
    }
    return a.isize>b.isize;
}
int main(){
    char temp[30];
    int n=0;
    while(scanf("%s",temp)==1){
        int l=strlen(temp);
        strcpy(me[n].w1,temp);
        me[n].len=l;
        me[n].size=1;
        sort(temp,temp+l);
        strcpy(me[n].w2,temp);
        n++;
    }
    ///for(int i=0;i<n;++i)me[i].put();
    sort(me,me+n,cmp1);
    ///cout<<"\n\n";
   /// for(int i=0;i<n;++i)me[i].put();
    int nz=0;
    for(int i=0,add,t;i<n;++i){
        add=0;t=i;
        while(!strcmp(me[i].w2,me[i+1].w2)&&i<n){
            add++;i++;
        }
        for(int j=t;j<=i;j++)me[j].size+=add;
        z[nz].start=t;
        z[nz].end=i;
        z[nz].isize=add+1;
        nz++;
    }
    sort(z,z+nz,cmp2);
    ///cout<<"\n\n";
    ///for(int i=0;i<n;++i)me[i].put();
    ///cout<<"\n\n";
   /// for(int i=0;i<nz;++i)z[i].put();
    int loop=5;
    int i=0;
    while(loop--&&i<n&&i<nz){
        printf("Group of size %d: ",z[i].isize);
        printf("%s ",me[z[i].start].w1);
        for(int j=z[i].start+1;j<=z[i].end;++j){
            if(strcmp(me[j].w1,me[j-1].w1)!=0)
                printf("%s ",me[j].w1);
        }
        printf(".\n");
        i++;
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-07 02:54:45

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

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 la

POJ 2398 计算几何+二分+排序

Toy Storage Time Limit: 1000MS  Memory Limit: 65536K Total Submissions: 3953  Accepted: 2334 Description Mom and dad have a problem: their child, Reza, never puts his toys away when he is finished playing with them. They gave Reza a rectangular box t

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'. 就是给一个序列(序列可以有重复的元素),让我们输出它的所有排列,字母顺序规定给出! [分析]:这道题是我之前学习枚举排序和子

Genealogical tree POJ 2367【拓扑排序】

http://poj.org/problem?id=2367 Genealogical tree Special Judge Problem Description The system of Martians' blood relations is confusing enough. Actually, Martians bud when they want and where they want. They gather together in different groups, so th

POJ 3616 Milking Time (排序+dp)

题目链接:http://poj.org/problem?id=3616 有头牛产奶n小时(n<=1000000),但必须在m个时间段内取奶,给定每个时间段的起始时间和结束时间以及取奶质量 且两次取奶之间须间隔r-1个小时,求最大取奶质量 也就是说r = 2时 3分结束取奶,至少在5分才能取. 按照时间排序,dp[i]表示i时段的最大产奶量 1 //#pragma comment(linker, "/STACK:102400000, 102400000") 2 #include &

[POJ] #1003# 487-3279 : 桶排序/字典树(Trie树)/快速排序

一. 题目 487-3279 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 274040   Accepted: 48891 Description Businesses like to have memorable telephone numbers. One way to make a telephone number memorable is to have it spell a memorable word or

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