id=2408" target="_blank" style="">题目链接:poj 2408 Anagram Groups

题目大意:给定若干个字符串,将其分组,依照组成元素同样为一组,输出数量最多的前5组,每组依照字典序输出所

有字符串。数量同样的输出字典序较小的一组。

解题思路:将全部的字符串统计字符后hash。排序之后确定每组的个数而且确定一组中字典序最小的字符串。依据个数

以及字符串对组进行排序。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <vector>
  4. #include <map>
  5. #include <algorithm>
  6. using namespace std;
  7. const int maxn = 30005;
  8. const int maxm = 30;
  9. const int X = 30;
  10. typedef unsigned long long ll;
  11. typedef pair<ll,int> pii;
  12. int N, M, E;
  13. vector<pii> vec, grop;
  14. vector<int> g[maxn];
  15. char word[maxn][maxm], st[maxn][maxm];
  16. inline ll Hash(char* s) {
  17. int len = strlen(s), c[maxm];
  18. memset(c, 0, sizeof(c));
  19. for (int i = 0; i < len; i++)
  20. c[s[i]-'a']++;
  21. ll ret = 0;
  22. for (int i = 0; i < 26; i++)
  23. ret = ret * X + c[i];
  24. return ret;
  25. }
  26. inline bool cmp (const pii& a, const pii& b) {
  27. if (a.second == b.second)
  28. return strcmp(st[a.first], st[b.first]) < 0;
  29. return a.second > b.second;
  30. }
  31. inline bool sort_by(const int& a, const int& b) {
  32. return strcmp(word[a], word[b]) < 0;
  33. }
  34. int main () {
  35. N = M = E = 0;
  36. vec.clear();
  37. grop.clear();
  38. while (scanf("%s", word[N]) == 1) {
  39. ll key = Hash(word[N]);
  40. vec.push_back(make_pair(key, N));
  41. N++;
  42. }
  43. sort(vec.begin(), vec.end());
  44. int cnt = 0;
  45. ll pre = -1;
  46. for (int i = 0; i < vec.size(); i++) {
  47. int idx = vec[i].second;
  48. if (vec[i].first != pre) {
  49. if (cnt)
  50. grop.push_back(make_pair(M++, cnt));
  51. cnt = 0;
  52. g[M].clear();
  53. pre = vec[i].first;
  54. strcpy(st[M], word[idx]);
  55. }
  56. cnt++;
  57. g[M].push_back(idx);
  58. if (strcmp(word[idx], st[M]) < 0)
  59. strcpy(st[M], word[idx]);
  60. }
  61. if (cnt)
  62. grop.push_back(make_pair(M++, cnt));
  63. sort(grop.begin(), grop.end(), cmp);
  64. for (int i = 0; i < min(5, (int)grop.size()); i++) {
  65. printf("Group of size %d: ", grop[i].second);
  66. int x = grop[i].first;
  67. sort(g[x].begin(), g[x].end(), sort_by);
  68. for (int j = 0; j < g[x].size(); j++) {
  69. if (j == 0 || strcmp(word[g[x][j-1]], word[g[x][j]]))
  70. printf("%s ", word[g[x][j]]);
  71. }
  72. printf(".\n");
  73. }
  74. return 0;
  75. }

版权声明:本文博客原创文章。博客,未经同意,不得转载。

poj 2408 Anagram Groups(hash)的更多相关文章

  1. POJ 2408 - Anagram Groups - [字典树]

    题目链接:http://poj.org/problem?id=2408 World-renowned Prof. A. N. Agram's current research deals with l ...

  2. poj 2408 Anagram Groups

    Description World-renowned Prof. A. N. Agram's current research deals with large anagram groups. He ...

  3. POJ 2002 统计正方形 HASH

    题目链接:http://poj.org/problem?id=2002 题意:给定n个点,问有多少种方法可以组成正方形. 思路:我们可以根据两个点求出对应正方形[有2个一个在两点左边,一个在两点右边] ...

  4. POJ 1971 统计平行四边形 HASH

    题目链接:http://poj.org/problem?id=1971 题意:给定n个坐标.问有多少种方法可以组成平行四边形.题目保证不会有4个点共线的情况. 思路:可以发现平行四边形的一个特点,就是 ...

  5. POJ 3320 (尺取法+Hash)

    题目链接: http://poj.org/problem?id=3320 题目大意:一本书有P页,每页有个知识点,知识点可以重复.问至少连续读几页,使得覆盖全部知识点. 解题思路: 知识点是有重复的, ...

  6. poj 1840 Eqs (hash)

    题目:http://poj.org/problem?id=1840 题解:http://blog.csdn.net/lyy289065406/article/details/6647387 小优姐讲的 ...

  7. POJ 3865 - Database 字符串hash

    [题意] 给一个字符串组成的矩阵,规模为n*m(n<=10000,m<=10),如果某两列中存在两行完全相同,则输出NO和两行行号和两列列号,否则输出YES [题解] 因为m很小,所以对每 ...

  8. POJ 1256.Anagram

    2015-06-04 问题简述: 输出一串字符的全排列,顺序不同于一般的字母序,而是 A<a<B<b......<Z<z.所以应该重写一个比较函数. 原题链接:http: ...

  9. poj 1200 Crazy Search(hash)

    题目链接:http://poj.org/problem?id=1200 思路分析:从数据来看,该题目使用线性时间算法,可见子串的比较是不可能的:使用hash可以在常数时间内查找,可以常数时间内判重, ...

随机推荐

  1. POJ 3267-The Cow Lexicon(DP)

    The Cow Lexicon Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8252   Accepted: 3888 D ...

  2. Selenium 2.0 WebDriver 自动化测试 使用教程 实例教程 API快速参考

    Selenium 2.0 WebDriver 自动化测试 使用教程 实例教程 API快速参考 //System.setProperty("webdriver.firefox.bin" ...

  3. 基于Hadoop的地震数据分析统计

    源码下载地址:http://download.csdn.net/detail/huhui_bj/5645641 opencsv下载地址:http://download.csdn.net/detail/ ...

  4. hdu4679(树形dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4679 题意:给一棵树,每条边上都有一个权值,去掉树上任意一条边之后,分成两个子树,两个子树的最长路与这 ...

  5. Android判断应用程序从后台回到前台

    MainActivity如下: package cc.testbackgroundtofront; import java.util.List; import android.app.Activity ...

  6. cocos2d-x 旅程開始--(实现瓦片地图中的碰撞检測)

    转眼隔了一天了,昨天搞了整整一下午加一晚上,楞是没搞定小坦克跟砖头的碰撞检測,带着个问题睡觉甚是难受啊!还好今天弄成功了.只是感觉程序不怎么稳定啊.并且发现自己写的东西让我重写一遍的话我肯定写不出来. ...

  7. HDU 2451 Simple Addition Expression(组合数学)

    主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2451 Problem Description A luxury yacht with 100 pass ...

  8. 集合中Set_List必须覆盖 hashCode()与 equals()

    集合中Set_List必须覆盖 hashCode()与 equals() @Override public int hashCode() { System.out.println("==== ...

  9. codechef Little Elephant and Permutations题解

    The Little Elephant likes permutations. This time he has a permutation A[1], A[2], ..., A[N] of numb ...

  10. java平台的常用资源

    分离领域 翻译 from :akullpp | awesome-java 大家一起学习,共同进步. 如果大家觉得有用,就mark一下,赞一下,或评论一下,让更多的人知道.thanks. 构建 这里搜集 ...