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<<".
    ";
        }
    }
  • 相关阅读:
    asp.net在线压缩和解压缩的实现 VS2005
    Office组件配置
    您无权查看或编辑目前 F:\XXX 的权限设置;但是,您可以取得所有权或更改审核设置
    23条心灵寄语献给在创业一线的兄弟姐妹
    IIS 环境下 w3wp.exe 进程 CPU 占用过高的解决方案
    SQLServer修改表所有者
    走出软件作坊推荐
    Asp.net 的 服务器推技术 (Server Push)
    上海火车站售票点
    Newtonsoft.Json.dll 本地调用正常,服务器调用不正常
  • 原文地址:https://www.cnblogs.com/dilthey/p/9986550.html
Copyright © 2011-2022 走看看