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可以在常数时间内查找,可以常数时间内判重, ...
随机推荐
- 使用 JQueryMobile 点击超链接提示“error loading page” 错误
使用jquery mobile创建dialog时出现加载错误,“Error Loading Page”. 原因是:jquery mobile页面默认采用ajax方式进行交互,而ajax方式下是不支持f ...
- PPPOE 模拟环境搭建
这段时间.包含我自己測试OTT盒子 PPPOE的时候比較痛苦.要不就是在别人的位置上測试.要不就是借用PPPOE的设备,认为还是自己搭建一个PPPOE真实拨号上网的环境多好! 可是坑爹的win7找不到 ...
- 收集经常使用的.net开源项目
Json.NET http://json.codeplex.com/ Json.Net是一个读写Json效率比較高的.Net框架.Json.Net 使得在.Net环境下使用Json更加简单.通过Lin ...
- Python-Tkinter的Entry详解
#Tkinter教程之Entry篇 #Entry用来输入单行文本 '''1.第一个Entry程序''' from Tkinter import * root = Tk() Entry(root,tex ...
- 同步github工程gitcafe
github固然好.仅仅是国内訪问有点慢. 为了提高博客訪问速度我决定把github上托管的博客同步到gitcafe上.最好能在DNS那里做CDN,可是貌似没有免费的服务.那直接指向gitcafe好了 ...
- A Game of Thrones(2) - Catelyn
Catelyn had never liked this godswood(神木林). She had been born a Tully, at Riverrun far to the south, ...
- Android - 通过Intent启动Activity
通过Intent启动Activity 本文地址: http://blog.csdn.net/caroline_wendy 为了动态关联Activity界面,使用Intent启动.能够灵活绑定. 在In ...
- 怎样在屏幕上显示多个alv
本文解说怎样在屏幕上显示多个alv. 实现这种需求关键是下面几点(举例:在屏幕上显示4个alv): 1.须要定义4个alv control 2.由于有4个alv control,于是就须要定义4个容器 ...
- Android画图监听接口OnPreDrawListener具体解释
public static interface ViewTreeObserver.OnPreDrawListener 我们先看下API中的定义: 类概述: 为即将绘制视图树时运行的回调函数定义的接口. ...
- 大规模集群管理工具Borg
Google的大规模集群管理工具Borg 概述 Google的Borg系统是一个集群管理工具,在它上面运行着成千上万的job,这些job来自许许多多不同的应用,并且跨越多个集群,而每个集群又由大量的机 ...