zoukankan      html  css  js  c++  java
  • 【哈希和哈希表】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;
    }
     
  • 相关阅读:
    mysql存储过程及拼接字符串的用法
    SpringMVC的工作原理
    3年java工作经验必备技能
    HashMap的源码,实现原理,底层结构
    十年软件测试感悟,写给想要转行的测试新人。
    软件自动化测试有了测试工程师就等于有了质量?
    为何你的简历石沉大海?这份新鲜出炉的测试用人需求分析报告揭示了原因。
    【软件测试】Python自动化软件测试算是程序员吗?
    达内教育培训怎么样,值得去吗?
    软件测试员最核心的竞争力究竟是什么? 爱码小哥
  • 原文地址:https://www.cnblogs.com/czy-power/p/10357596.html
Copyright © 2011-2022 走看看