问题 G: 【哈希和哈希表】Beads

时间限制: 1 Sec  内存限制: 128 MB
提交: 6  解决: 2
[提交] [状态] [讨论版] [命题人:admin]

题目描述

Byteasar once decided to start manufacturing necklaces. He subsequently bought a very long string of colourful coral beads for a bargain price. Byteasar now also has a machine that, for a given k(k>0), can cut the string into pieces (or substrings) of k coral beads (i.e., the first piece consists of the beads no.1,...,k, the second of k+1,...,2k etc.). If the length of the string (measured in coral beads) is not a multiple of , then the last piece is not used, as it has length smaller than k. From now on we denote the colours of the beads with positive integers.

Byteasar, always praising diversity, wonders how he should choose the number k in order to get as many different substrings as possible. The ends of the long string that will be cut are different: there are specific beginning and ending (rather than two interchangeable endpoints), and the machine of course starts cutting at the beginning. On the other hand, in the substrings obtained from cutting the endpoints are interchangeable, (1,2,3)and (3,2,1)so the substrings can be reversed. In other words, the substrings  and are identical to us. Write a program that determines the optimum value of  for Byteasar.

For example, for the following string of beads:(1,1,1,2,2,2,3,3,3,1,2,3,3,1,2,2,1,3,3,2,1),
using k=1, we would get 3 different substrings: (1),(2),(3),
using k=2, we would get 6 different substrings:  (1,1),(1,2),(2,2),(3,3),(3,1),(2,3)
using k=3, we would get 5 different substrings:  (1,1,1),(2,2,2,),(3,3,3),(1,2,3),(3,1,2),
using k=4, we would get 5 different substrings:  (1,1,1,2),(2,2,3,3),(3,1,2,3),(3,1,2,2),(1,3,3,2),
using larger values of  k would give at most 3 different substrings.

输入

In the first line of the standard input there is an integer n(1≤n≤200000) denoting the length of the string to cut. In the second line there are  positive integers  ai(1≤ai≤n), separated by single spaces, that denote the colours of successive beads in Byteasar's string.

输出

Two integers, separated by a single space, should be printed out to the first line of the standard ouput: the (maximum) number of different substrings that can be obtained with an optimal choice of parameter k, and the number l of ksuch optimal values of . The second line should contain  integers separated by single spaces: the values of parameter k that yield an optimum solution; these can be given in arbitrary order.

样例输入

复制样例数据

21
1 1 1 2 2 2 3 3 3 1 2 3 3 1 2 2 1 3 3 2 1

样例输出

6 1
2思路:裸的hash,但是不知道为什么hash的数不能是131(哪位大佬知道的话评论一下),于是乎取了1000000007,注意通过翻转获得的子串可能相同,判断一下即可。
#include<bits/stdc++.h>
#include<queue>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<iostream>
#define REP(i, a, b) for(int i = (a); i <= (b); ++ i)
#define REP(j, a, b) for(int j = (a); j <= (b); ++ j)
#define PER(i, a, b) for(int i = (a); i >= (b); -- i)
const int maxn = 2e5 + 5;
int n,p[maxn],hh[maxn],base[maxn],cur,gt,ans[maxn],cnt,tot,ss[maxn];
using namespace std;
map<int,int>mp;
void check(int l, int r) {int dir = hh[r] - hh[l - 1] * base[r - l + 1];int ver = ss[l] - ss[r + 1] * base[r - l + 1];if (!mp[dir] && !mp[ver]) {tot++;mp[dir]=1;mp[ver]=1;}
}
int main(){cin >> n;base[0]=1;for (int i = 1; i <= n; i++) {cin >> p[i];hh[i] = hh[i - 1] * 1000000007 + p[i];base[i] = base[i - 1] * 1000000007;}for (int i = n; i >= 1; i--)ss[i] = ss[i + 1] * 1000000007 + p[i];for (int len = 1; len <= n; len++) {mp.clear();tot=0;for (int i = 1; i + len - 1 <= n; i+=len) {check(i, i + len - 1);}if (cur < tot)cur = tot, cnt = 1, ans[1] = len;else if (cur == tot) {ans[++cnt] = len;}}cout << cur << ' ' << cnt<<endl<<ans[1];for (int i = 2; i <= cnt; i++)cout << ' '<< ans[i] ;cout<<endl;return 0;
}

 

转载于:https://www.cnblogs.com/czy-power/p/10357596.html

【哈希和哈希表】Beads相关推荐

  1. 提高篇 第二部分 字符串算法 第1章 哈希和哈希表

    浅谈字符串哈希_1264Ikaros的博客-CSDN博客_字符串哈希 图书管理-哈希表_handsome·wjc的博客-CSDN博客 字符串哈希 哈希表 - DTTTTTTT - 博客园 图书管理(L ...

  2. 不同表结构数据迁移_数据结构:哈希 哈希函数 哈希表

    写在前面 希望你们看了能够有所收获,同时觉得不错的朋友可以点赞和关注下我,以后还会有更多精选文章分享给大家!大家可以关注一下java提升专栏 java提升​zhuanlan.zhihu.com 什么是 ...

  3. Java集合—哈希(hash)表

    原文作者: 原文地址: 1.哈希表的定义 这里先说一下哈希(hash)表的定义:哈希表是一种根据关键码去寻找值的数据映射结构,该结构通过把关键码映射的位置去寻找存放值的地方,说起来可能感觉有点复杂,我 ...

  4. 高级数据结构与算法 | 哈希 :哈希冲突、负载因子、哈希函数、哈希表、哈希桶

    文章目录 哈希 哈希函数 常见的哈希函数 字符串哈希函数 哈希冲突 闭散列的解决方法 开散列的解决方法 负载因子以及增容 对于闭散列 对于开散列结构 具体实现 哈希表(闭散列) 插入 查找 删除 完整 ...

  5. 哈希值 哈希表_哈希杰森

    哈希值 哈希表 我最近写了一个简单的库,可预测地对json进行哈希处理 . 该实用程序基于出色的Jackson Json解析库构建 问题 我需要从相当大的基于json的内容生成的哈希值,以便稍后确定该 ...

  6. 哈希(哈希表与哈希函数)

    一.哈希 哈希函数是计算机领域特别是在密码学领域应用最广泛的算法之一,哈希表是数据结构中应用最广泛的结构之一,本博客将为大家介绍哈希函数,介绍哈希表. 1.哈希函数 2.哈希表 3.哈希函数在大数据中 ...

  7. 问题 I: 【哈希和哈希表】门票

    问题 I: [哈希和哈希表]门票 时间限制: 1 Sec  内存限制: 128 MB 提交: 43  解决: 6 [提交] [状态] [讨论版] [命题人:admin] 题目描述 RPK要带MSH去一 ...

  8. 哈希 :哈希冲突、负载因子、哈希函数、哈希表、哈希桶

    文章目录 哈希 哈希(散列)函数 常见的哈希函数 字符串哈希函数 哈希冲突 闭散列(开放地址法) 开散列(链地址法/拉链法) 负载因子以及增容 对于闭散列 对于开散列结构 具体实现 哈希表(闭散列) ...

  9. 一本通提高篇 哈希和哈希表 (二)哈希表

    哈希表 哈希表是一种搞笑的数奆结垢.它的优点同字符串哈希一样,查找的算法时间效率几乎就是常数时间,同时也很容易实现,多产生的代价仅仅是消耗内存. 那么什么是哈希表呢 ,我的理解是:按一种分类方式将所有 ...

最新文章

  1. C语言中printf是不是关键字,C语言中printf是什么意思
  2. 计算机系职教周方案,琼软院软件〔2018〕14 号:关于印发《软件工程系2018年“职业教育 活动周”活动方案》的通知...
  3. mybatis关联查询
  4. python爬虫的技能_关于 Python 爬虫可能涉及到的技能点
  5. Modelarts与无感识别技术生态总结(浅出版)
  6. linux定时执行python脚本_ubuntu定时执行python脚本实例代码
  7. 关于Patch应用和举例
  8. SelfUpdate 树不起作用
  9. 系统签名文件pk8x509.pem 转成jks或者keystore签名文件
  10. Less系列之混合(Mixins)
  11. python爬千图网高清图片
  12. 为什么影子会相互吸引? - 《像乌鸦一样思考》
  13. python五大模块_python-5-常用模块
  14. 大数据平台回归SQL
  15. Xcode14 build WebDriverAgent提示“Cannot link directly with dylib/framework“的解决方法
  16. UML是什么?UML不是什么?
  17. LocalDate、LocalTime、LocalDateTime介绍
  18. python访问陌生人qq空间_python程序记录QQ空间条说说的访问详情
  19. 如何安装numpy库
  20. 解魔方机器人系列(三)软件控制及整机测试

热门文章

  1. 关于Zipalign的介绍和使用方法
  2. Android自定义控件之仿汽车之家下拉刷新
  3. extjs 表单验证实例
  4. android studio 快速删除无用的import包
  5. Baidu_Location_SDK
  6. Tensorflow 2.0的新特性
  7. Android 通知栏Notification
  8. 性能测试工具系列(一):性能测试工具对比分析
  9. 浅谈:MyBatis-Plus的CRUD与乐观锁,分页插件,逻辑删除
  10. Android Studio运行报错:无法访问XXX......请删除该文件或确保该文件位于正确的类路径子目录中