zoukankan      html  css  js  c++  java
  • POJ 2408

    题目链接: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, 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 .

    题解:

    用字典树把每个组都hash成一个数字 $x$,然后把组内的字符串全部存在编号为 $x$ 的vector内。

    然后对vector进行排序(实际上是对vector的编号进行排序),再输出前五项就好了,注意相同的单词虽然计数,但是输出时只输出一次。

    AC代码:

    #include<iostream>
    #include<string>
    #include<algorithm>
    #include<vector>
    #include<cstring>
    using namespace std;
    const int maxn=3e4+5;
    string str;
    int idx[maxn];
    vector<string> g[maxn];
    bool cmp(int i,int j)
    {
        if(g[i].size()!=g[j].size())
            return g[i].size()>g[j].size();
        else if(g[i].size() && g[j].size())
            return g[i][0]<g[j][0];
    }
    
    namespace Trie
    {
        const int SIZE=maxn*2;
        int sz,tot;
        struct TrieNode{
            int ed;
            int nxt[26];
        }trie[SIZE];
        void init(){sz=1, tot=0;}
        int insert(const string& s)
        {
            int p=1;
            for(int i=0;i<s.size();i++)
            {
                int ch=s[i]-'a';
                if(!trie[p].nxt[ch]) trie[p].nxt[ch]=++sz;
                p=trie[p].nxt[ch];
            }
            return trie[p].ed?trie[p].ed:(trie[p].ed=++tot);
        }
    };
    
    int main()
    {
        ios::sync_with_stdio(0);
        cin.tie(0), cout.tie(0);
    
        Trie::init();
        while(cin>>str)
        {
            string tmp=str;
            sort(tmp.begin(),tmp.end());
            int t=Trie::insert(tmp);
            g[t].push_back(str);
        }
    
        for(int i=1;i<=Trie::tot;i++) idx[i]=i;
        sort(idx+1,idx+Trie::tot+1,cmp);
    
        for(int i=1;i<=5 && g[idx[i]].size()>0;i++)
        {
            vector<string>& v=g[idx[i]];
            cout<<"Group of size "<<v.size()<<": ";
            sort(v.begin(),v.end());
            v.erase(unique(v.begin(),v.end()),v.end());
            for(int k=0;k<v.size();k++) cout<<v[k]<<' ';
            cout<<".
    ";
        }
    }
  • 相关阅读:
    Linux 中直接读取 CPU 寄存器的方法
    UBUNTU SVN 服务器配置
    LinuxMint 12 install ATI driver
    Mplayer ARM平台下交叉编译
    VMware 中 linux 读取共享文件方法
    glib 交叉编译
    JavaScript中this指针指向的彻底理解
    专属自己的二维游戏引擎【九】
    专属自己的二维游戏引擎【三】
    专属自己的二维游戏引擎【八】
  • 原文地址:https://www.cnblogs.com/dilthey/p/9986550.html
Copyright © 2011-2022 走看看