您的位置:首页 > 其它

POJ-2408 Anagram Groups-字典序取最大前五

2015-08-20 16:00 405 查看
Anagram Groups
Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 4906Accepted: 1319
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

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

typedef struct word
{
    char s[100],ss[100];//存初始单词和按字典序排列过的单词
} Word;
Word w[30005];//最多30000个单词

typedef struct Cou
{
    int count;//相同单词的数目
    char a[100],aa[100];
} Count;
Count c[30005];
//下面是qsort排序要用的比较函数
int word_cmp(const void* x1,const void* y1)//整个原始单词表列排序用
{
    Word* x=(Word*)x1;
    Word* y=(Word*)y1;
    int m=strcmp(x->ss,y->ss);
    if(m!=0)
        return m;
    return strcmp(x->s,y->s);
}

int ccmp(const void* x1,const void* y1)//数目优先,单词次之以保证排序稳定
{
    Count* x=(Count*)x1;
    Count* y=(Count*)y1;
    int m=y->count-x->count;
    if(m!=0)
        return m;
    return strcmp(x->aa,y->aa);
}
int main()
{
    int i=0,j,m,n=0,len,cnt=1;
    while(scanf("%s",w[i].s)!=EOF)//输入单词
    {
        strcpy(w[i].ss,w[i].s);//先复制
        sort(w[i].ss,w[i].ss+strlen(w[i].ss));//再按字典序排列
        ++i;
    }
    len=i;//单词个数
    qsort(w,len,sizeof(Word),word_cmp);//整个单词表列按字典序排列
    char* str1=w[0].ss;//初始赋值
    char* str2=w[0].s;
    for(i=1; i<=len; ++i)
    {
        m=strcmp(str1,w[i].ss);//判断字典序排列后单词的是否相同
        if(m==0)
            cnt++;//相同则+1
        else//不相同
        {
            c
.count=cnt;//记录下当前相同的个数
            strcpy(c
.a,str1);//将字符串复制下来方便输出
            strcpy(c
.aa,str2);
            str1=w[i].ss;//再次比较
            str2=w[i].s;
            ++n;//不相同之后要重新开始比较
            cnt=1;
        }
    }
    qsort(c,n,sizeof(Count),ccmp);//排序来找出前5个最大的变形词组
    n=n>5?5:n;//只需要输出前5个最大的变形词组
    for(i=0; i<n; i++)//输出
    {
        printf("Group of size %d: ",c[i].count);//输出个数
        str1=" ";//清空str1
        for(j=0; j<len; j++)//遍历原始单词组
        {
            m=strcmp(w[j].ss,c[i].a);
            if(m>0)//超出范围
                break;
            if(m<0||strcmp(str1,w[j].s)==0)//不相同或者是重复出现就继续遍历
                continue;
            str1=w[j].s;//给str1赋值方便下次比较
            printf("%s ",w[j].s);//输出单词
        }
        printf(".\n");
    }
    return 0;
}


数据很多,用C语言输入输出流比较保险。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: