poj 3320 Jessica's Reading Problem (哈希高级应用)

Jessica‘s Reading Problem

Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

Submit
Status

Description

Jessica‘s a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is coming, yet she has spent little time on it. If she wants to pass it, she has to master all ideas included in a very thick text book. The author of that text
book, like other authors, is extremely fussy about the ideas, thus some ideas are covered more than once. Jessica think if she managed to read each idea at least once, she can pass the exam. She decides to read only one contiguous part of the book which contains
all ideas covered by the entire book. And of course, the sub-book should be as thin as possible.

A very hard-working boy had manually indexed for her each page of Jessica‘s text-book with what idea each page is about and thus made a big progress for his courtship. Here you come in to save your skin: given the index, help Jessica decide which contiguous
part she should read. For convenience, each idea has been coded with an ID, which is a non-negative integer.

Input

The first line of input is an integer P (1 ≤ P ≤ 1000000), which is the number of pages of Jessica‘s text-book. The second line contains
P non-negative integers describing what idea each page is about. The first integer is what the first page is about, the second integer is what the second page is about, and so on. You may assume all integers that appear can fit well in the signed 32-bit
integer type.

Output

Output one line: the number of pages of the shortest contiguous part of the book which contains all ideals covered in the book.

Sample Input

5
1 8 8 8 1

Sample Output

2

Hint

Source

POJ Monthly--2007.08.05, Jerry

看了好几个小时 算法原理终于把这个题目搞掉了@!! 下面经哥哥详细注释:

#include<iostream>
#include<sstream>
#include<algorithm>
#include<cstdio>
#include<string.h>
#include<cctype>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
using namespace std;

const int PRIME = 99991;

struct node   //hash表结点元素包含三个属性 key表示结点元素值,num相当于hash表下标,
{
    //next表示指向的下一个结点hash表下标
    int key,num;
    int next ;
} p[10000005];

int s[1000006];
int len ,n,ans;

int ELFhash(int k)
{
    int i = k% PRIME;
    while(p[i].next != -1)
    {
        //i下标位置不为单一元素值,即以此元素为头结点有一条“拉链”里面
        //可能包含和头结点相同key值也可能不同,注意到此拉链是是按照递减顺序
        //插入的,所以如果k>p[p[i].next].key 即找到插入位置。且此时仍没有发现key
        // 值相同,所以需要为此k值在hash表中安排新的下标位
        if(k>p[p[i].next].key)break;
        else if(k == p[p[i].next].key)
            return p[i].next;
        // 如果发现相同返回hash表下标位
        i=p[i].next;// 沿着“拉链”继续查找
    }
    p[len].key=k;
    p[len].next = -1;
    p[len].num = 0;
    p[len].next = p[i].next;
    p[i].next = len;
    len++;//此六行是插入新元素值,注意是按照拉链递减规则插入
    return  len -1 ;
}

int main()
{
    int left,temp;
    while(scanf("%d",&n)!=EOF)
    {
        memset(s,0,sizeof(s));
        for(int i=0; i<PRIME; i++)
            p[i].next = -1;
        len=PRIME;
        left = 0;
        //以上为初始化,且left用于标记右端点下标的极值
        for(int i=0; i<n; i++)
        {
            scanf("%d",&s[i]);
            temp = ELFhash(s[i]);
            p[temp].num++;
            if(p[temp].num<=1)
            {
                //此条件下表示新元素插入ans代表最小串,其值必为下条语句
                ans = i-left+1;
                continue;
            }
            // 也就是说如果发现和前面有重复元素的时候
            temp = ELFhash(s[left]);
            while(left<n-1&&p[temp].num>1)
            {
                //如果是和left位置元素相同left完全可以右移一个
                //往后只需研究left和i之间的元素,看看是否可以找到比
                //ans更小的值。
                p[temp].num--;
                left++;
                temp=ELFhash(s[left]);
            }
            if(ans>i-left+1)ans=i-left+1;
            //如果是和left和i之间不包含端点元素相同
            //左端点不能变,右端点i此时拉长一位 ,ans值不变
        }
        printf("%d\n",ans);
    }

    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

poj 3320 Jessica's Reading Problem (哈希高级应用)

时间: 2024-08-26 10:43:31

poj 3320 Jessica's Reading Problem (哈希高级应用)的相关文章

poj 3320 Jessica&#39;s Reading Problem(尺取法+map/hash)

题目:http://poj.org/problem?id=3320 题意:给定N个元素的数组,找出最短的一段区间使得区间里面的元素种类等于整个数组的元素种类. 分析:暴力枚举区间的起点x,然后找到最小的y,使得区间[x,y]满足条件,x向有移位后变成x',现在的y'肯定不至于在y的左边.存状态的话map和hash都可以. map代码: #include <iostream> #include <set> #include <map> #include <cstdi

poj 3320 Jessica&#39;s Reading Problem

题意:有n页书,每页有个编号为ci的知识点,求最小看连续的页数,其中包括所有的知识点 分析:n<=1e6,只能搞O(n)的解法,无非就是枚举起点和终点,尺取法正好适合这个想法,枚举一个起点,然后往后扫描到区间内包括所有的知识点,然后每次起点都往右移动一次,直到扫描到右边界也没有答案了,就跳出 程序跑了469ms,应该是map太慢,可以hash搞一波,也过了,但是可以离散化知识点的编号,最多有1e6个,空间换时间 #include<iostream> #include<cstdio&

poj 3320 Jessica&#39;s Reading Problem (尺取法)

Jessica's Reading Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8787   Accepted: 2824 Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is coming, yet she has spent littl

尺取法 POJ 3320 Jessica&#39;s Reading Problem

题目传送门 1 /* 2 尺取法:先求出不同知识点的总个数tot,然后以获得知识点的个数作为界限, 更新最小值 3 */ 4 #include <cstdio> 5 #include <cmath> 6 #include <cstring> 7 #include <algorithm> 8 #include <set> 9 #include <map> 10 using namespace std; 11 12 const int M

POJ 3320 Jessica&#39;s Reading Problem 尺取法/map

Jessica's Reading Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7467   Accepted: 2369 Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is coming, yet she has spent littl

POJ 3320 Jessica&#39;s Reading Problem (滑动窗口)

题意:给定一个序列,求一个最短区间,使得这个区间包含所有的种类数. 析:最近刚做了几个滑动窗口的题,这个很明显也是,肯定不能暴力啊,时间承受不了啊,所以 我们使用滑动窗口来解决,要算出所有的种数,我用set来计算的,当然也可以用别的, 由于要记录种类数,所以使用map来记录,删除和查找方便,说到这,这不就是水题了么. 代码如下: #include <iostream> #include <cstdio> #include <algorithm> #include <

POJ 3320 Jessica&#39;s Reading Problem (尺取法)

Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is coming, yet she has spent little time on it. If she wants to pass it, she has to master all ideas included in a very thick text book. The author of that

poj 3320 Jessica&#39;s Reading Problem(尺取法)

Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is coming, yet she has spent little time on it. If she wants to pass it, she has to master all ideas included in a very thick text book. The au

(尺取法)POJ 3320 Jessica&#39;s Reading Problem

Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is coming, yet she has spent little time on it. If she wants to pass it, she has to master all ideas included in a very thick text book. The author of that