poj 2408 Anagram Groups(hash)
id=2408" target="_blank" style="">题目链接:poj 2408 Anagram Groups
题目大意:给定若干个字符串,将其分组,依照组成元素同样为一组,输出数量最多的前5组,每组依照字典序输出所
有字符串。数量同样的输出字典序较小的一组。
解题思路:将全部的字符串统计字符后hash。排序之后确定每组的个数而且确定一组中字典序最小的字符串。依据个数
以及字符串对组进行排序。
#include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
const int maxn = 30005;
const int maxm = 30;
const int X = 30;
typedef unsigned long long ll;
typedef pair<ll,int> pii;
int N, M, E;
vector<pii> vec, grop;
vector<int> g[maxn];
char word[maxn][maxm], st[maxn][maxm];
inline ll Hash(char* s) {
int len = strlen(s), c[maxm];
memset(c, 0, sizeof(c));
for (int i = 0; i < len; i++)
c[s[i]-'a']++;
ll ret = 0;
for (int i = 0; i < 26; i++)
ret = ret * X + c[i];
return ret;
}
inline bool cmp (const pii& a, const pii& b) {
if (a.second == b.second)
return strcmp(st[a.first], st[b.first]) < 0;
return a.second > b.second;
}
inline bool sort_by(const int& a, const int& b) {
return strcmp(word[a], word[b]) < 0;
}
int main () {
N = M = E = 0;
vec.clear();
grop.clear();
while (scanf("%s", word[N]) == 1) {
ll key = Hash(word[N]);
vec.push_back(make_pair(key, N));
N++;
}
sort(vec.begin(), vec.end());
int cnt = 0;
ll pre = -1;
for (int i = 0; i < vec.size(); i++) {
int idx = vec[i].second;
if (vec[i].first != pre) {
if (cnt)
grop.push_back(make_pair(M++, cnt));
cnt = 0;
g[M].clear();
pre = vec[i].first;
strcpy(st[M], word[idx]);
}
cnt++;
g[M].push_back(idx);
if (strcmp(word[idx], st[M]) < 0)
strcpy(st[M], word[idx]);
}
if (cnt)
grop.push_back(make_pair(M++, cnt));
sort(grop.begin(), grop.end(), cmp);
for (int i = 0; i < min(5, (int)grop.size()); i++) {
printf("Group of size %d: ", grop[i].second);
int x = grop[i].first;
sort(g[x].begin(), g[x].end(), sort_by);
for (int j = 0; j < g[x].size(); j++) {
if (j == 0 || strcmp(word[g[x][j-1]], word[g[x][j]]))
printf("%s ", word[g[x][j]]);
}
printf(".\n");
}
return 0;
}
版权声明:本文博客原创文章。博客,未经同意,不得转载。
poj 2408 Anagram Groups(hash)的更多相关文章
- POJ 2408 - Anagram Groups - [字典树]
题目链接:http://poj.org/problem?id=2408 World-renowned Prof. A. N. Agram's current research deals with l ...
- poj 2408 Anagram Groups
Description World-renowned Prof. A. N. Agram's current research deals with large anagram groups. He ...
- POJ 2002 统计正方形 HASH
题目链接:http://poj.org/problem?id=2002 题意:给定n个点,问有多少种方法可以组成正方形. 思路:我们可以根据两个点求出对应正方形[有2个一个在两点左边,一个在两点右边] ...
- POJ 1971 统计平行四边形 HASH
题目链接:http://poj.org/problem?id=1971 题意:给定n个坐标.问有多少种方法可以组成平行四边形.题目保证不会有4个点共线的情况. 思路:可以发现平行四边形的一个特点,就是 ...
- POJ 3320 (尺取法+Hash)
题目链接: http://poj.org/problem?id=3320 题目大意:一本书有P页,每页有个知识点,知识点可以重复.问至少连续读几页,使得覆盖全部知识点. 解题思路: 知识点是有重复的, ...
- poj 1840 Eqs (hash)
题目:http://poj.org/problem?id=1840 题解:http://blog.csdn.net/lyy289065406/article/details/6647387 小优姐讲的 ...
- POJ 3865 - Database 字符串hash
[题意] 给一个字符串组成的矩阵,规模为n*m(n<=10000,m<=10),如果某两列中存在两行完全相同,则输出NO和两行行号和两列列号,否则输出YES [题解] 因为m很小,所以对每 ...
- POJ 1256.Anagram
2015-06-04 问题简述: 输出一串字符的全排列,顺序不同于一般的字母序,而是 A<a<B<b......<Z<z.所以应该重写一个比较函数. 原题链接:http: ...
- poj 1200 Crazy Search(hash)
题目链接:http://poj.org/problem?id=1200 思路分析:从数据来看,该题目使用线性时间算法,可见子串的比较是不可能的:使用hash可以在常数时间内查找,可以常数时间内判重, ...
随机推荐
- POJ 3267-The Cow Lexicon(DP)
The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8252 Accepted: 3888 D ...
- Selenium 2.0 WebDriver 自动化测试 使用教程 实例教程 API快速参考
Selenium 2.0 WebDriver 自动化测试 使用教程 实例教程 API快速参考 //System.setProperty("webdriver.firefox.bin" ...
- 基于Hadoop的地震数据分析统计
源码下载地址:http://download.csdn.net/detail/huhui_bj/5645641 opencsv下载地址:http://download.csdn.net/detail/ ...
- hdu4679(树形dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4679 题意:给一棵树,每条边上都有一个权值,去掉树上任意一条边之后,分成两个子树,两个子树的最长路与这 ...
- Android判断应用程序从后台回到前台
MainActivity如下: package cc.testbackgroundtofront; import java.util.List; import android.app.Activity ...
- cocos2d-x 旅程開始--(实现瓦片地图中的碰撞检測)
转眼隔了一天了,昨天搞了整整一下午加一晚上,楞是没搞定小坦克跟砖头的碰撞检測,带着个问题睡觉甚是难受啊!还好今天弄成功了.只是感觉程序不怎么稳定啊.并且发现自己写的东西让我重写一遍的话我肯定写不出来. ...
- HDU 2451 Simple Addition Expression(组合数学)
主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2451 Problem Description A luxury yacht with 100 pass ...
- 集合中Set_List必须覆盖 hashCode()与 equals()
集合中Set_List必须覆盖 hashCode()与 equals() @Override public int hashCode() { System.out.println("==== ...
- codechef Little Elephant and Permutations题解
The Little Elephant likes permutations. This time he has a permutation A[1], A[2], ..., A[N] of numb ...
- java平台的常用资源
分离领域 翻译 from :akullpp | awesome-java 大家一起学习,共同进步. 如果大家觉得有用,就mark一下,赞一下,或评论一下,让更多的人知道.thanks. 构建 这里搜集 ...