【哈希和哈希表】Beads

问题 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

时间: 2024-11-19 02:46:07

【哈希和哈希表】Beads的相关文章

【模版】简单哈希和哈希表处理冲突

哈希(Hash)算法就是单向散列算法,它把某个较大的集合P映射到另一个较小的集合Q中.数学原理听起来很抽象,在网上找到一个很生动的描述.我们有很多的小猪,每个的体重都不一样,假设体重分布比较平均(我们考虑到公斤级别),我们按照体重来分,划分成100个小猪圈. 然后把每个小猪,按照体重赶进各自的猪圈里,记录档案.如果我们要精确找到某个小猪怎么办呢?我们需要每个猪圈,每个小猪的比对吗? 当然不需要了. 我们先看看要找的这个小猪的体重,然后就找到了对应的猪圈了. 在这个猪圈里的小猪的数量就相对很少了.

哈希算法-快速查表的原理

在实际问题中,按照给定的值进行数据查询是经常遇到的,比如,在电话号码簿中查询某个人的电话号码:在图书馆中按照ISBN 编号查找某本书的位置:在地图中按照坐标查找某个地点的地名等等. 字典的定义 我们都使用过字典,如英汉字典.成语字典,图书的检索目录.电话簿等也可以看作广义上的字典.在计算机科学中,把字典也当成一种数据结构. 我们把字典定义为“键-值对”(Key-Value Pair) 的集合.根据不同的问题,我们为名字和值赋予不同的含义,比如,在英汉字典中,英文单词是名字,此单词的中文解释条目是

【C/C++学院】0904-boost智能指针/boost多线程锁定/哈希库/正則表達式

boost_array_bind_fun_ref Array.cpp #include<boost/array.hpp> #include <iostream> #include <string> using namespace std; using namespace boost; void mainA () { array <int, 5> barray = { 1, 2, 3, 4, 5 }; barray[0] = 10; barray.at(4)

九. 常用类库、向量与哈希6.哈希表及其应用

哈希表也称为散列表,是用来存储群体对象的集合类结构. 什么是哈希表 数组和向量都可以存储对象,但对象的存储位置是随机的,也就是说对象本身与其存储位置之间没有必然的联系.当要查找一个对象时,只能以某种顺序(如顺序查找或二分查找)与各个元素进行比较,当数组或向量中的元素数量很多时,查找的效率会明显的降低. 一种有效的存储方式,是不与其他元素进行比较,一次存取便能得到所需要的记录.这就需要在对象的存储位置和对象的关键属性(设为 k)之间建立一个特定的对应关系(设为 f),使每个对象与一个唯一的存储位置

蓝书《哈希与哈希表》——知识整理

一.前言: 有些数据不经处理是难以利用的.所谓哈希,就是通过哈希函数将这种难以简单利用的数据(比如矩阵.字符串等等)转化为可以用一个变量表示甚至可以作为数组下标的哈希值.有了哈希值,就可以实现时间复杂度近乎为常数的快速查找与匹配,更简单有效地利用一些复杂数据. 二.字符串哈希: 即对象为字符串的哈希.常将字符串看做一个不严格的b进制的数(有时数位上的数有可能要比b还要大),转换为10机制后再模一个数p(以便存储)得到哈希值.即定义哈希函数: H(C)=(c1*b^(m-1) + c2*b^(m-

#10034.「一本通 2.1 例 2」图书管理-哈希(双哈希思想)+邻接表

样例 样例输入 4 add Inside C# find Effective Java add Effective Java find Effective Java 样例输出 no yes 数据范围与提示 n<=30000 1 #include<stdio.h> 2 #include<algorithm> 3 #include<iostream> 4 #include<string.h> 5 using namespace std; 6 typedef

『一本通』哈希和哈希表

Oulipo 1 #include<bits/stdc++.h> 2 #define N 1000000+5 3 using namespace std; 4 typedef unsigned long long ULL; 5 const int b=55; 6 ULL n,m,s,ans,p[N],sum[N]; 7 char s1[N],s2[N]; 8 9 int main() { 10 scanf("%s",s1+1),scanf("%s",s2

学习哈希及哈希在大数据检索和挖掘中的应用

http://cs.nju.edu.cn/lwj/conf/CIKM14Hash.htm Learning to Hash with its Application to Big Data Retrieval and Mining Overview Nearest neighbor (NN) search plays a fundamental role in machine learning and related areas, such as information retrieval an

哈希表

哈希表支持的一种最有效的检索方法:散列. 由于计算哈希值和在数组中进行索引都只消耗固定时间,因此哈希表的最大亮点在于他是一种运行时间在常量级的检索方法. 哈希表主要有两种: 1.链式哈希表:将数据存储在桶中的哈希表,每个桶里面都是一个链表,且链表的容量随着冲突的增大而增大.(换句话说就是如果有冲突,会在桶中的链表加上一个存储的值) 2.开地址哈希表:将数据存在表本身,而不是在桶中,他通过各种探查方法来避免冲突. 解决冲突: 不管在以上那种哈希表中,我们的目标是尽可能均匀的分配表中的元素.所以我们