码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-07-19 16:34:47      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:poj   算法   acm   hash   哈希   

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

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 (哈希高级应用)

标签:poj   算法   acm   hash   哈希   

原文地址:http://blog.csdn.net/lsgqjh/article/details/46954997

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!